Skip to content

Commit

Permalink
Remove fatal error and replace by error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Jul 16, 2024
1 parent 6c35747 commit 5cdb183
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Improv-iOS-Demo/Device/DeviceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ struct DeviceView<Manager>: View where Manager: ImprovManagerProtocol {
}
Section {
Button(action: {
improvManager.identifyDevice()
if let error = improvManager.identifyDevice() {
print("ERROR while identify command: \(error)")
}
}, label: {
Text("Identify")
})
Expand All @@ -50,7 +52,9 @@ struct DeviceView<Manager>: 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")
})
Expand Down
23 changes: 17 additions & 6 deletions Improv-iOS/BluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down
8 changes: 4 additions & 4 deletions Improv-iOS/ImprovManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 10 additions & 2 deletions Improv-iOSTests/Mocks/MockBluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CoreBluetooth
@testable import Improv_iOS

class MockBluetoothManager: BluetoothManagerProtocol {

weak var delegate: BluetoothManagerDelegate?
var state: CBManagerState = .unknown

Expand All @@ -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?
Expand All @@ -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
}
}

0 comments on commit 5cdb183

Please sign in to comment.