2023-06-03 08:15:00 +02:00
|
|
|
import Foundation
|
|
|
|
import CoreBluetooth
|
|
|
|
|
|
|
|
enum DeviceState {
|
|
|
|
|
|
|
|
case bluetoothDisabled
|
|
|
|
|
|
|
|
case bluetoothEnabled
|
|
|
|
|
|
|
|
case scanning
|
|
|
|
|
|
|
|
case connecting(device: CBPeripheral)
|
|
|
|
|
|
|
|
case discoveringServices(device: CBPeripheral)
|
|
|
|
|
|
|
|
case discoveringCharacteristic(device: CBPeripheral)
|
|
|
|
|
|
|
|
case configured(device: CBPeripheral, characteristic: CBCharacteristic)
|
|
|
|
|
|
|
|
case disconnected
|
|
|
|
|
|
|
|
var text: String {
|
|
|
|
switch self {
|
|
|
|
case .bluetoothDisabled:
|
|
|
|
return "Bluetooth is disabled"
|
|
|
|
case .bluetoothEnabled:
|
|
|
|
return "Bluetooth enabled"
|
|
|
|
case .scanning:
|
2023-06-13 17:14:57 +02:00
|
|
|
return "Scanning..."
|
2023-06-03 08:15:00 +02:00
|
|
|
case .connecting(let device):
|
|
|
|
guard let name = device.name else {
|
2023-06-13 17:14:57 +02:00
|
|
|
return "Connecting..."
|
2023-06-03 08:15:00 +02:00
|
|
|
}
|
|
|
|
return "Connecting to \(name)..."
|
2023-06-13 17:14:57 +02:00
|
|
|
case .discoveringServices:
|
|
|
|
return "Discovering service..."
|
|
|
|
case .discoveringCharacteristic:
|
|
|
|
return "Discovering characteristic..."
|
2023-06-03 08:15:00 +02:00
|
|
|
case .configured(let device, _):
|
|
|
|
guard let name = device.name else {
|
|
|
|
return "Connected"
|
|
|
|
}
|
2023-06-13 17:14:57 +02:00
|
|
|
return name
|
2023-06-03 08:15:00 +02:00
|
|
|
case .disconnected:
|
|
|
|
return "Not connected"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DeviceState: CustomStringConvertible {
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
switch self {
|
|
|
|
case .bluetoothDisabled:
|
|
|
|
return "Bluetooth disabled"
|
|
|
|
case .bluetoothEnabled:
|
|
|
|
return "Bluetooth enabled"
|
|
|
|
case .scanning:
|
|
|
|
return "Searching for device"
|
|
|
|
case .connecting:
|
|
|
|
return "Connecting to device"
|
|
|
|
case .discoveringServices:
|
|
|
|
return "Discovering services"
|
|
|
|
case .discoveringCharacteristic:
|
|
|
|
return "Discovering characteristics"
|
|
|
|
case .configured:
|
|
|
|
return "Connected"
|
|
|
|
case .disconnected:
|
|
|
|
return "Disconnected"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension DeviceState: Equatable {
|
|
|
|
|
|
|
|
}
|