From 6acdf5432a1f9dd7af3d85bfccaa445247bc0d68 Mon Sep 17 00:00:00 2001 From: Marco Sinigaglia Date: Mon, 11 Dec 2023 16:21:28 +0100 Subject: [PATCH] Fix handling error in didUpdateNotificationStateFor --- ios/BleManager.swift | 54 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/ios/BleManager.swift b/ios/BleManager.swift index 0e255490..b1b9903d 100644 --- a/ios/BleManager.swift +++ b/ios/BleManager.swift @@ -686,7 +686,7 @@ class BleManager: RCTEventEmitter, CBCentralManagerDelegate, CBPeripheralDelegat } @objc func getMaximumWriteValueLengthForWithResponse(_ peripheralUUID: String, - callback: @escaping RCTResponseSenderBlock) { + callback: @escaping RCTResponseSenderBlock) { NSLog("getMaximumWriteValueLengthForWithResponse") guard let peripheral = peripherals[peripheralUUID] else { @@ -717,7 +717,7 @@ class BleManager: RCTEventEmitter, CBCentralManagerDelegate, CBPeripheralDelegat } } } - + func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { @@ -1072,41 +1072,43 @@ class BleManager: RCTEventEmitter, CBCentralManagerDelegate, CBPeripheralDelegat "code": error._code ]) } + } else { + if hasListeners { + sendEvent(withName: "BleManagerDidUpdateNotificationStateFor", body: [ + "peripheral": peripheral.uuidAsString(), + "characteristic": characteristic.uuid.uuidString.lowercased(), + "isNotifying": characteristic.isNotifying + ]) + } } let key = Helper.key(forPeripheral: peripheral, andCharacteristic: characteristic) - if characteristic.isNotifying { + if let error = error { if notificationCallbacks[key] != nil { - if let error = error { - invokeAndClearDictionary(¬ificationCallbacks, withKey: key, usingParameters: [error]) - } else { - if BleManager.verboseLogging { - NSLog("Notification began on \(characteristic.uuid)") - } - invokeAndClearDictionary(¬ificationCallbacks, withKey: key, usingParameters: []) - } + invokeAndClearDictionary(¬ificationCallbacks, withKey: key, usingParameters: [error]) } - } else { - // Notification has stopped if stopNotificationCallbacks[key] != nil { - if error != nil { - invokeAndClearDictionary(&stopNotificationCallbacks, withKey: key, usingParameters: [error as Any]) - } else { - if BleManager.verboseLogging { - NSLog("Notification ended on \(characteristic.uuid)") - } + invokeAndClearDictionary(&stopNotificationCallbacks, withKey: key, usingParameters: [error]) + } + } else { + if characteristic.isNotifying { + if BleManager.verboseLogging { + NSLog("Notification began on \(characteristic.uuid)") + } + if notificationCallbacks[key] != nil { + invokeAndClearDictionary(¬ificationCallbacks, withKey: key, usingParameters: []) + } + } else { + // Notification has stopped + if BleManager.verboseLogging { + NSLog("Notification ended on \(characteristic.uuid)") + } + if stopNotificationCallbacks[key] != nil { invokeAndClearDictionary(&stopNotificationCallbacks, withKey: key, usingParameters: []) } } } - if hasListeners { - sendEvent(withName: "BleManagerDidUpdateNotificationStateFor", body: [ - "peripheral": peripheral.uuidAsString(), - "characteristic": characteristic.uuid.uuidString.lowercased(), - "isNotifying": characteristic.isNotifying - ]) - } } func peripheral(_ peripheral: CBPeripheral,