diff --git a/Improv-iOS-Demo/Device/DeviceView.swift b/Improv-iOS-Demo/Device/DeviceView.swift index 97ca962..3283173 100644 --- a/Improv-iOS-Demo/Device/DeviceView.swift +++ b/Improv-iOS-Demo/Device/DeviceView.swift @@ -39,7 +39,9 @@ struct DeviceView: View where Manager: ImprovManagerProtocol { } Section { Button(action: { - improvManager.identifyDevice() + if let error = improvManager.identifyDevice() { + print("ERROR while identify command: \(error)") + } }, label: { Text("Identify") }) @@ -50,7 +52,9 @@ struct DeviceView: View where Manager: ImprovManagerProtocol { TextField("SSID", text: $ssid) TextField("Password", text: $password) Button(action: { - improvManager.sendWifi(ssid: ssid, password: password) + if let error = improvManager.sendWifi(ssid: ssid, password: password) { + print("ERROR while sendWifi command: \(error)") + } }, label: { Text("Connect to Wifi") }) diff --git a/Improv-iOS/BluetoothManager.swift b/Improv-iOS/BluetoothManager.swift index 33d22ac..1acc6fb 100644 --- a/Improv-iOS/BluetoothManager.swift +++ b/Improv-iOS/BluetoothManager.swift @@ -27,8 +27,13 @@ protocol BluetoothManagerProtocol { func stopScan() func connectToDevice(_ peripheral: CBPeripheral) func disconnectFromDevice(_ peripheral: CBPeripheral) - func identifyDevice() - func sendWifi(ssid: String, password: String) + func identifyDevice() -> BluetoothManagerError? + func sendWifi(ssid: String, password: String) -> BluetoothManagerError? +} + +public enum BluetoothManagerError: Error { + case deviceDisconnected + case serviceNotAvailable } final class BluetoothManager: NSObject, BluetoothManagerProtocol { @@ -69,24 +74,30 @@ final class BluetoothManager: NSObject, BluetoothManagerProtocol { centralManager.cancelPeripheralConnection(peripheral) } - func identifyDevice() { + func identifyDevice() -> BluetoothManagerError? { guard let gatt = bluetoothGatt else { - fatalError("Not Connected to a Device!") + return .deviceDisconnected } if let rpc = gatt.services?.first(where: { $0.uuid == BluetoothUUIDs.serviceProvision })?.characteristics?.first(where: { $0.uuid == BluetoothUUIDs.charRpc }) { sendRpc(rpc, command: .identify, data: []) + return nil + } else { + return .serviceNotAvailable } } - func sendWifi(ssid: String, password: String) { + func sendWifi(ssid: String, password: String) -> BluetoothManagerError? { guard let gatt = bluetoothGatt else { - fatalError("Not Connected to a Device!") + return .deviceDisconnected } if let rpc = gatt.services?.first(where: { $0.uuid == BluetoothUUIDs.serviceProvision })?.characteristics?.first(where: { $0.uuid == BluetoothUUIDs.charRpc }) { let encodedSsid = Array(ssid.utf8) let encodedPassword = Array(password.utf8) let data = [UInt8(encodedSsid.count)] + encodedSsid + [UInt8(encodedPassword.count)] + encodedPassword sendRpc(rpc, command: .sendWifi, data: data) + return nil + } else { + return .serviceNotAvailable } } diff --git a/Improv-iOS/ImprovManager.swift b/Improv-iOS/ImprovManager.swift index a91c335..186e919 100644 --- a/Improv-iOS/ImprovManager.swift +++ b/Improv-iOS/ImprovManager.swift @@ -17,8 +17,8 @@ public protocol ImprovManagerProtocol: ObservableObject { func stopScan() func connectToDevice(_ peripheral: CBPeripheral) func disconnectFromDevice(_ peripheral: CBPeripheral) - func identifyDevice() - func sendWifi(ssid: String, password: String) + func identifyDevice() -> BluetoothManagerError? + func sendWifi(ssid: String, password: String) -> BluetoothManagerError? func reset() } @@ -99,11 +99,11 @@ public final class ImprovManager: NSObject, ImprovManagerProtocol { bluetoothManager.disconnectFromDevice(peripheral) } - public func identifyDevice() { + public func identifyDevice() -> BluetoothManagerError? { bluetoothManager.identifyDevice() } - public func sendWifi(ssid: String, password: String) { + public func sendWifi(ssid: String, password: String) -> BluetoothManagerError? { bluetoothManager.sendWifi(ssid: ssid, password: password) } diff --git a/Improv-iOSTests/Mocks/MockBluetoothManager.swift b/Improv-iOSTests/Mocks/MockBluetoothManager.swift index cbd48c7..861df73 100644 --- a/Improv-iOSTests/Mocks/MockBluetoothManager.swift +++ b/Improv-iOSTests/Mocks/MockBluetoothManager.swift @@ -10,6 +10,7 @@ import CoreBluetooth @testable import Improv_iOS class MockBluetoothManager: BluetoothManagerProtocol { + weak var delegate: BluetoothManagerDelegate? var state: CBManagerState = .unknown @@ -18,6 +19,7 @@ class MockBluetoothManager: BluetoothManagerProtocol { var connectToDeviceCalled = false var identifyDeviceCalled = false var sendWifiCalled = false + var disconnectFromDeviceCalled = false var lastPeripheral: CBPeripheral? var lastSSID: String? @@ -36,13 +38,19 @@ class MockBluetoothManager: BluetoothManagerProtocol { lastPeripheral = peripheral } - func identifyDevice() { + func disconnectFromDevice(_ peripheral: CBPeripheral) { + disconnectFromDeviceCalled = true + } + + func identifyDevice() -> Improv_iOS.BluetoothManagerError? { identifyDeviceCalled = true + return nil } - func sendWifi(ssid: String, password: String) { + func sendWifi(ssid: String, password: String) -> Improv_iOS.BluetoothManagerError? { sendWifiCalled = true lastSSID = ssid lastPassword = password + return nil } }