react-native-ble-nitro 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -285,6 +285,11 @@ await subscription.remove();
|
|
|
285
285
|
|
|
286
286
|
// Or unsubscribe directly
|
|
287
287
|
await ble.unsubscribeFromCharacteristic(deviceId, serviceUUID, characteristicUUID);
|
|
288
|
+
|
|
289
|
+
// Check if a characteristic is currently subscribed to notifications
|
|
290
|
+
const isSubscribed = ble.isSubscribedToCharacteristic(deviceId, serviceUUID, characteristicUUID);
|
|
291
|
+
// Returns: boolean — synchronous, no async overhead
|
|
292
|
+
// Returns false for disconnected or unknown devices without throwing
|
|
288
293
|
```
|
|
289
294
|
|
|
290
295
|
### Real-World Examples
|
|
@@ -266,18 +266,21 @@ class BleNitroBleManager : HybridNativeBleNitroSpec() {
|
|
|
266
266
|
private fun createGattCallback(deviceId: String): BluetoothGattCallback {
|
|
267
267
|
return object : BluetoothGattCallback() {
|
|
268
268
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
|
269
|
-
val callbacks = deviceCallbacks[deviceId]
|
|
270
|
-
|
|
271
269
|
when (newState) {
|
|
272
270
|
BluetoothProfile.STATE_CONNECTED -> {
|
|
273
|
-
|
|
271
|
+
var connectCb: ((Boolean, String, String) -> Unit)? = null
|
|
272
|
+
deviceCallbacks.compute(deviceId) { _, callbacks ->
|
|
273
|
+
connectCb = callbacks?.connectCallback
|
|
274
|
+
callbacks?.copy(connectCallback = null)
|
|
275
|
+
}
|
|
276
|
+
connectCb?.invoke(true, deviceId, "")
|
|
274
277
|
}
|
|
275
278
|
BluetoothProfile.STATE_DISCONNECTED -> {
|
|
276
279
|
// Clean up
|
|
277
280
|
connectedDevices.remove(deviceId)
|
|
278
281
|
val interrupted = status != BluetoothGatt.GATT_SUCCESS
|
|
279
|
-
|
|
280
|
-
|
|
282
|
+
val cb = deviceCallbacks.remove(deviceId)
|
|
283
|
+
cb?.disconnectCallback?.invoke(deviceId, interrupted, if (interrupted) "Connection lost" else "")
|
|
281
284
|
gatt.close()
|
|
282
285
|
}
|
|
283
286
|
}
|
|
@@ -657,6 +660,12 @@ class BleNitroBleManager : HybridNativeBleNitroSpec() {
|
|
|
657
660
|
try {
|
|
658
661
|
val gatt = connectedDevices[deviceId]
|
|
659
662
|
if (gatt != null) {
|
|
663
|
+
var pendingConnect: ((Boolean, String, String) -> Unit)? = null
|
|
664
|
+
deviceCallbacks.compute(deviceId) { _, callbacks ->
|
|
665
|
+
pendingConnect = callbacks?.connectCallback
|
|
666
|
+
callbacks?.copy(connectCallback = null)
|
|
667
|
+
}
|
|
668
|
+
pendingConnect?.invoke(false, deviceId, "Connection cancelled")
|
|
660
669
|
gatt.disconnect()
|
|
661
670
|
callback(true, "")
|
|
662
671
|
} else {
|
|
@@ -255,17 +255,22 @@ public class BleNitroBleManager: HybridNativeBleNitroSpec {
|
|
|
255
255
|
|
|
256
256
|
public func disconnect(deviceId: String, callback: @escaping (Bool, String) -> Void) throws {
|
|
257
257
|
ensureCentralManager()
|
|
258
|
-
guard let peripheral = connectedPeripherals[deviceId] else {
|
|
259
|
-
callback(false, "Peripheral not
|
|
258
|
+
guard let peripheral = connectedPeripherals[deviceId] ?? findPeripheral(by: deviceId) else {
|
|
259
|
+
callback(false, "Peripheral not found")
|
|
260
260
|
return
|
|
261
261
|
}
|
|
262
|
-
|
|
262
|
+
|
|
263
|
+
if peripheral.state == .connecting, let delegate = peripheralDelegates[deviceId] {
|
|
264
|
+
delegate.connectionCallback?(false, "", "Connection cancelled")
|
|
265
|
+
delegate.connectionCallback = nil
|
|
266
|
+
}
|
|
267
|
+
|
|
263
268
|
// Store disconnect callback in delegate
|
|
264
269
|
peripheralDelegates[deviceId]?.disconnectionCallback = callback
|
|
265
|
-
|
|
270
|
+
|
|
266
271
|
// Mark this as an intentional disconnection
|
|
267
272
|
intentionalDisconnections.insert(deviceId)
|
|
268
|
-
|
|
273
|
+
|
|
269
274
|
centralManager.cancelPeripheralConnection(peripheral)
|
|
270
275
|
}
|
|
271
276
|
|
|
@@ -714,7 +719,7 @@ public class BleNitroBleManager: HybridNativeBleNitroSpec {
|
|
|
714
719
|
rssi: Double
|
|
715
720
|
) -> BLEDevice {
|
|
716
721
|
let deviceId = peripheral.identifier.uuidString
|
|
717
|
-
let deviceName =
|
|
722
|
+
let deviceName = advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? peripheral.name ?? "Unknown"
|
|
718
723
|
|
|
719
724
|
// Extract service UUIDs
|
|
720
725
|
let serviceUUIDs = (advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID])?.map { $0.uuidString } ?? []
|