import Foundation import CoreBluetooth enum DeviceState { case bluetoothDisabled 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 .scanning: return "Scanning..." case .connecting(let device): guard let name = device.name else { return "Connecting..." } return "Connecting to \(name)..." case .discoveringServices: return "Discovering service..." case .discoveringCharacteristic: return "Discovering characteristic..." case .configured(let device, _): guard let name = device.name else { return "Connected" } return name case .disconnected: return "Not connected" } } var device: CBPeripheral? { switch self { case .bluetoothDisabled, .disconnected, .scanning: return nil case .connecting(let device), .discoveringCharacteristic(let device), .discoveringServices(device: let device), .configured(let device, _): return device } } } extension DeviceState: CustomStringConvertible { var description: String { switch self { case .bluetoothDisabled: return "Bluetooth disabled" 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 { }