diff --git a/Source/CentralManagerRestoredState.swift b/Source/CentralManagerRestoredState.swift index 81d6e62c..7e40adb4 100644 --- a/Source/CentralManagerRestoredState.swift +++ b/Source/CentralManagerRestoredState.swift @@ -60,7 +60,12 @@ public struct CentralManagerRestoredState: CentralManagerRestoredStateType { let cbServices = arrayOfAnyObjects.flatMap { $0 as? CBService } #endif - return cbServices.map { Service(peripheral: centralManager.retrievePeripheral(for: $0.peripheral), + return cbServices.compactMap { + guard let peripheral = $0.peripheral else { + return nil + } + + return Service(peripheral: centralManager.retrievePeripheral(for: peripheral), service: $0) } } } diff --git a/Source/Characteristic.swift b/Source/Characteristic.swift index f4f2edec..e7e726dd 100644 --- a/Source/Characteristic.swift +++ b/Source/Characteristic.swift @@ -40,8 +40,11 @@ public class Characteristic { self.service = service } - convenience init(characteristic: CBCharacteristic, peripheral: Peripheral) { - let service = Service(peripheral: peripheral, service: characteristic.service) + convenience init?(characteristic: CBCharacteristic, peripheral: Peripheral) { + guard let bluetoothService = characteristic.service else { + return nil + } + let service = Service(peripheral: peripheral, service: bluetoothService) self.init(characteristic: characteristic, service: service) } diff --git a/Source/Descriptor.swift b/Source/Descriptor.swift index dff8f3f3..472b3020 100644 --- a/Source/Descriptor.swift +++ b/Source/Descriptor.swift @@ -28,9 +28,14 @@ public class Descriptor { self.characteristic = characteristic } - convenience init(descriptor: CBDescriptor, peripheral: Peripheral) { - let service = Service(peripheral: peripheral, service: descriptor.characteristic.service) - let characteristic = Characteristic(characteristic: descriptor.characteristic, service: service) + convenience init?(descriptor: CBDescriptor, peripheral: Peripheral) { + guard let bluetoothService = descriptor.characteristic?.service, + let bluetoothCharacteristic = descriptor.characteristic else { + return nil + } + let service = Service(peripheral: peripheral, service: bluetoothService) + let characteristic = Characteristic(characteristic: bluetoothCharacteristic, + service: service) self.init(descriptor: descriptor, characteristic: characteristic) } diff --git a/Source/Peripheral.swift b/Source/Peripheral.swift index 9a783c74..4a2c8319 100644 --- a/Source/Peripheral.swift +++ b/Source/Peripheral.swift @@ -403,6 +403,10 @@ public class Peripheral { .map { [weak self] (cbCharacteristic, error) -> Characteristic in guard let strongSelf = self else { throw BluetoothError.destroyed } let characteristic = characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf) + guard let characteristic = characteristic else { + throw BluetoothError.unknownWriteType + } + if let error = error { throw BluetoothError.characteristicWriteFailed(characteristic, error) } @@ -499,6 +503,9 @@ public class Peripheral { .map { [weak self] (cbCharacteristic, error) -> Characteristic in guard let strongSelf = self else { throw BluetoothError.destroyed } let characteristic = characteristic ?? Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf) + guard let characteristic = characteristic else { + throw BluetoothError.unknownWriteType + } if let error = error { throw BluetoothError.characteristicReadFailed(characteristic, error) } @@ -569,6 +576,9 @@ public class Peripheral { .map { [weak self] (cbCharacteristic, error) -> Characteristic in guard let strongSelf = self else { throw BluetoothError.destroyed } let characteristic = Characteristic(characteristic: cbCharacteristic, peripheral: strongSelf) + guard let characteristic = characteristic else { + throw BluetoothError.unknownWriteType + } if let error = error { throw BluetoothError.characteristicSetNotifyValueFailed(characteristic, error) } @@ -638,6 +648,9 @@ public class Peripheral { .map { [weak self] (cbDescriptor, error) -> Descriptor in guard let strongSelf = self else { throw BluetoothError.destroyed } let descriptor = descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf) + guard let descriptor = descriptor else { + throw BluetoothError.unknownWriteType + } if let error = error { throw BluetoothError.descriptorWriteFailed(descriptor, error) } @@ -667,6 +680,9 @@ public class Peripheral { .map { [weak self] (cbDescriptor, error) -> Descriptor in guard let strongSelf = self else { throw BluetoothError.destroyed } let descriptor = descriptor ?? Descriptor(descriptor: cbDescriptor, peripheral: strongSelf) + guard let descriptor = descriptor else { + throw BluetoothError.unknownWriteType + } if let error = error { throw BluetoothError.descriptorReadFailed(descriptor, error) }