83 lines
2.2 KiB
Swift
83 lines
2.2 KiB
Swift
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:
|
|
return "Scanning for devices..."
|
|
case .connecting(let device):
|
|
guard let name = device.name else {
|
|
return "Connecting to device..."
|
|
}
|
|
return "Connecting to \(name)..."
|
|
case .discoveringServices(let device):
|
|
guard let name = device.name else {
|
|
return "Setting up device..."
|
|
}
|
|
return "Setting up \(name)..."
|
|
case .discoveringCharacteristic(let device):
|
|
guard let name = device.name else {
|
|
return "Setting up device..."
|
|
}
|
|
return "Setting up \(name)..."
|
|
case .configured(let device, _):
|
|
guard let name = device.name else {
|
|
return "Connected"
|
|
}
|
|
return "Connected to \(name)"
|
|
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 {
|
|
|
|
}
|