Skip to content

Commit c26045a

Browse files
committed
fix(ios): Disconnect from peripheral if service discovery fails during connect #803
If service discovery fails, the connection reports an error but surprisingly remains connected by iOS and the plugin. Bring this behaviour into alignment with Android by disconnecting if the connect (including service discovery) fails. - Log additional information related to service/characteristic discovery - Upgrade package-lock to node v24
1 parent 0b2fe32 commit c26045a

File tree

4 files changed

+328
-6
lines changed

4 files changed

+328
-6
lines changed

ios/Sources/BluetoothLe/Device.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Device: NSObject, CBPeripheralDelegate {
6262
_ peripheral: CBPeripheral,
6363
didDiscoverServices error: Error?
6464
) {
65-
log("didDiscoverServices")
65+
log("didDiscoverServices", peripheral.services?.count ?? -1)
6666
if let error = error {
6767
log("Error", error.localizedDescription)
6868
return
@@ -81,10 +81,21 @@ class Device: NSObject, CBPeripheralDelegate {
8181
didDiscoverCharacteristicsFor service: CBService,
8282
error: Error?
8383
) {
84+
if let error = error {
85+
log("didDiscoverCharacteristicsFor",
86+
self.servicesDiscovered, self.servicesCount,
87+
self.characteristicsDiscovered, self.characteristicsCount)
88+
log("Error", error.localizedDescription)
89+
return
90+
}
91+
8492
self.servicesDiscovered += 1
85-
log("didDiscoverCharacteristicsFor", self.servicesDiscovered, self.servicesCount)
8693
self.characteristicsCount += service.characteristics?.count ?? 0
8794

95+
log("didDiscoverCharacteristicsFor",
96+
self.servicesDiscovered, self.servicesCount,
97+
self.characteristicsDiscovered, self.characteristicsCount)
98+
8899
if !self.skipDescriptorDiscovery {
89100
for characteristic in service.characteristics ?? [] {
90101
peripheral.discoverDescriptors(for: characteristic)

ios/Sources/BluetoothLe/DeviceManager.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ class DeviceManager: NSObject, CBCentralManagerDelegate {
313313
let key = "onDisconnected|\(device.getId())"
314314
self.callbackMap[key] = callback
315315
}
316+
317+
func cancelConnect(
318+
_ device: Device
319+
) {
320+
// do not call onDisconnnected, which is triggered by cancelPeripheralConnection
321+
let onDisconnectedKey = "onDisconnected|\(device.getId())"
322+
self.callbackMap[onDisconnectedKey] = nil
323+
self.centralManager.cancelPeripheralConnection(device.getPeripheral())
324+
}
316325

317326
func disconnect(
318327
_ device: Device,
@@ -397,10 +406,7 @@ class DeviceManager: NSObject, CBCentralManagerDelegate {
397406
_ connectionTimeout: Double
398407
) {
399408
let workItem = DispatchWorkItem {
400-
// do not call onDisconnnected, which is triggered by cancelPeripheralConnection
401-
let onDisconnectedKey = "onDisconnected|\(device.getId())"
402-
self.callbackMap[onDisconnectedKey] = nil
403-
self.centralManager.cancelPeripheralConnection(device.getPeripheral())
409+
self.cancelConnect(device)
404410
self.reject(connectionKey, message)
405411
}
406412
self.timeoutMap[connectionKey] = workItem

ios/Sources/BluetoothLe/Plugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public class BluetoothLe: CAPPlugin, CAPBridgedPlugin {
285285
// only resolve after service discovery
286286
call.resolve()
287287
} else {
288+
self.deviceManager?.cancelConnect(device)
288289
call.reject(message)
289290
}
290291
})

0 commit comments

Comments
 (0)