Skip to content

Commit 5c940eb

Browse files
authored
Fix compile error on Xcode 13 / iOS 15 SDK cause by an optionality change in one of the CBCharacteristic properties. (#260)
Also updates Info.plist to have the correct privacy notice for using Bluetooth
1 parent 68042d3 commit 5c940eb

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Bluejay/Bluejay/CharacteristicIdentifier.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ public struct CharacteristicIdentifier {
1717
/// The `CBUUID` of this characteristic.
1818
public let uuid: CBUUID
1919

20-
/// Create a `CharacteristicIdentifier` using a `CBCharacterstic`.
21-
public init(_ cbCharacteristic: CBCharacteristic) {
22-
self.service = ServiceIdentifier(uuid: cbCharacteristic.service.uuid)
20+
/// Create a `CharacteristicIdentifier` using a `CBCharacterstic`. Creation will fail if the "service" property of the CBCharacteristic is nil.
21+
/// Note: It isn't documented in CoreBluetooth under what circumstances that property might be nil, but it seems like it should almost never happen.
22+
public init?(_ cbCharacteristic: CBCharacteristic) {
23+
let optionalService: CBService? = cbCharacteristic.service // became optional with iOS 15 SDK, do a little dance to make it always optional so code below should compile on Xcode 12 or 13
24+
25+
guard let service = optionalService else {
26+
return nil
27+
}
28+
29+
self.service = ServiceIdentifier(uuid: service.uuid)
2330
self.uuid = cbCharacteristic.uuid
2431
}
2532

@@ -46,7 +53,8 @@ public struct CharacteristicIdentifier {
4653

4754
/// Check equality between a `CharacteristicIdentifier` and a `CBCharacterstic`.
4855
public static func == (lhs: CharacteristicIdentifier, rhs: CBCharacteristic) -> Bool {
49-
return (lhs.uuid == rhs.uuid) && (lhs.service.uuid == rhs.service.uuid)
56+
let optionalService: CBService? = rhs.service // became optional with iOS 15 SDK, do a little dance to make it always optional so code below should compile on Xcode 12 or 13
57+
return (lhs.uuid == rhs.uuid) && (lhs.service.uuid == optionalService?.uuid)
5058
}
5159
}
5260

Bluejay/Bluejay/Peripheral.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,15 @@ extension Peripheral: CBPeripheralDelegate {
298298

299299
/// Captures CoreBluetooth's did receive a notification/value from a characteristic event and pass it to Bluejay's queue for processing.
300300
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
301-
let characteristicIdentifier = CharacteristicIdentifier(characteristic)
301+
guard let characteristicIdentifier = CharacteristicIdentifier(characteristic) else {
302+
debugLog("Received value update for characteristic (\(characteristic.uuid.uuidString) without a valid service. Update will be ignored")
303+
return
304+
}
302305

303306
guard let listener = listeners[characteristicIdentifier], let listenCallback = listener.0 else {
304307
if delegate.isReading(characteristic: characteristicIdentifier) {
305308
handle(event: .didReadCharacteristic(characteristic, characteristic.value ?? Data()), error: error as NSError?)
306-
} else if delegate.willEndListen(on: CharacteristicIdentifier(characteristic)) {
309+
} else if delegate.willEndListen(on: characteristicIdentifier) {
307310
debugLog("""
308311
Received read event with value \(String(data: characteristic.value ?? Data(), encoding: .utf8) ?? "") \
309312
on characteristic \(characteristic.debugDescription), \

Bluejay/BluejayHeartSensorDemo/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@
4343
<string>UIInterfaceOrientationLandscapeLeft</string>
4444
<string>UIInterfaceOrientationLandscapeRight</string>
4545
</array>
46+
<key>NSBluetoothAlwaysUsageDescription</key>
47+
<string>Connect to the test device</string>
4648
</dict>
4749
</plist>

0 commit comments

Comments
 (0)