TempTrack-iOS/TempTrack/Bluetooth/DeviceState.swift

83 lines
2.1 KiB
Swift
Raw Normal View History

2023-06-03 08:15:00 +02:00
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:
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"
}
}
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
}
}
2023-06-03 08:15:00 +02:00
}
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 {
}