react-native-ble-manager 10.0.0 → 10.0.2

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.
Files changed (30) hide show
  1. package/README.md +2 -2
  2. package/android/src/main/java/it/innove/NotifyBufferContainer.java +19 -15
  3. package/android/src/main/java/it/innove/Peripheral.java +286 -272
  4. package/package.json +1 -1
  5. package/android/.gradle/7.4/checksums/checksums.lock +0 -0
  6. package/android/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock +0 -0
  7. package/android/.gradle/7.4/dependencies-accessors/gc.properties +0 -0
  8. package/android/.gradle/7.4/executionHistory/executionHistory.bin +0 -0
  9. package/android/.gradle/7.4/executionHistory/executionHistory.lock +0 -0
  10. package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
  11. package/android/.gradle/7.4/fileHashes/fileHashes.bin +0 -0
  12. package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
  13. package/android/.gradle/7.4/fileHashes/resourceHashesCache.bin +0 -0
  14. package/android/.gradle/7.4/gc.properties +0 -0
  15. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  16. package/android/.gradle/buildOutputCleanup/cache.properties +0 -2
  17. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  18. package/android/.gradle/file-system.probe +0 -0
  19. package/android/.gradle/vcs-1/gc.properties +0 -0
  20. package/android/.idea/.name +0 -1
  21. package/android/.idea/compiler.xml +0 -6
  22. package/android/.idea/gradle.xml +0 -18
  23. package/android/.idea/jarRepositories.xml +0 -40
  24. package/android/.idea/misc.xml +0 -10
  25. package/android/.idea/vcs.xml +0 -6
  26. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  27. package/android/gradle/wrapper/gradle-wrapper.properties +0 -5
  28. package/android/gradlew +0 -234
  29. package/android/gradlew.bat +0 -89
  30. package/android/local.properties +0 -8
@@ -141,16 +141,16 @@ public class Peripheral extends BluetoothGattCallback {
141
141
  }
142
142
  // bt_btif : Register with GATT stack failed.
143
143
 
144
- public void disconnect(final Callback callback, final boolean force) {
145
- mainHandler.post(() -> {
146
- for (Callback connectCallback: connectCallbacks) {
147
- connectCallback.invoke("Disconnect called before connect callback invoked");
148
- }
149
- connectCallbacks.clear();
150
- connected = false;
151
- clearBuffers();
152
- commandQueue.clear();
153
- commandQueueBusy = false;
144
+ public void disconnect(final Callback callback, final boolean force) {
145
+ mainHandler.post(() -> {
146
+ for (Callback connectCallback : connectCallbacks) {
147
+ connectCallback.invoke("Disconnect called before connect callback invoked");
148
+ }
149
+ connectCallbacks.clear();
150
+ connected = false;
151
+ clearBuffers();
152
+ commandQueue.clear();
153
+ commandQueueBusy = false;
154
154
 
155
155
  if (gatt != null) {
156
156
  try {
@@ -206,13 +206,13 @@ public class Peripheral extends BluetoothGattCallback {
206
206
  WritableArray characteristicsArray = Arguments.createArray();
207
207
 
208
208
  if (connected && gatt != null) {
209
- for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext(); ) {
209
+ for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext();) {
210
210
  BluetoothGattService service = it.next();
211
211
  WritableMap serviceMap = Arguments.createMap();
212
212
  serviceMap.putString("uuid", UUIDHelper.uuidToString(service.getUuid()));
213
213
 
214
214
  for (Iterator<BluetoothGattCharacteristic> itCharacteristic = service.getCharacteristics()
215
- .iterator(); itCharacteristic.hasNext(); ) {
215
+ .iterator(); itCharacteristic.hasNext();) {
216
216
  BluetoothGattCharacteristic characteristic = itCharacteristic.next();
217
217
  WritableMap characteristicsMap = Arguments.createMap();
218
218
 
@@ -276,18 +276,18 @@ public class Peripheral extends BluetoothGattCallback {
276
276
  return device;
277
277
  }
278
278
 
279
- @Override
280
- public void onServicesDiscovered(BluetoothGatt gatt, int status) {
281
- super.onServicesDiscovered(gatt, status);
282
- mainHandler.post(() -> {
283
- WritableMap map = this.asWritableMap(gatt);
284
- for (Callback retrieveServicesCallback: retrieveServicesCallbacks) {
285
- retrieveServicesCallback.invoke(null, map);
286
- }
287
- retrieveServicesCallbacks.clear();
288
- completedCommand();
289
- });
290
- }
279
+ @Override
280
+ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
281
+ super.onServicesDiscovered(gatt, status);
282
+ mainHandler.post(() -> {
283
+ WritableMap map = this.asWritableMap(gatt);
284
+ for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
285
+ retrieveServicesCallback.invoke(null, map);
286
+ }
287
+ retrieveServicesCallbacks.clear();
288
+ completedCommand();
289
+ });
290
+ }
291
291
 
292
292
  @Override
293
293
  public void onConnectionStateChange(BluetoothGatt gatta, int status, final int newState) {
@@ -312,7 +312,8 @@ public class Peripheral extends BluetoothGattCallback {
312
312
  try {
313
313
  gatt.discoverServices();
314
314
  } catch (NullPointerException e) {
315
- Log.d(BleManager.LOG_TAG, "onConnectionStateChange connected but gatt of Run method was null");
315
+ Log.d(BleManager.LOG_TAG,
316
+ "onConnectionStateChange connected but gatt of Run method was null");
316
317
  }
317
318
  discoverServicesRunnable = null;
318
319
  }
@@ -322,11 +323,11 @@ public class Peripheral extends BluetoothGattCallback {
322
323
 
323
324
  sendConnectionEvent(device, "BleManagerConnectPeripheral", status);
324
325
 
325
- Log.d(BleManager.LOG_TAG, "Connected to: " + device.getAddress());
326
- for (Callback connectCallback: connectCallbacks) {
327
- connectCallback.invoke();
328
- }
329
- connectCallbacks.clear();
326
+ Log.d(BleManager.LOG_TAG, "Connected to: " + device.getAddress());
327
+ for (Callback connectCallback : connectCallbacks) {
328
+ connectCallback.invoke();
329
+ }
330
+ connectCallbacks.clear();
330
331
 
331
332
  } else if (newState == BluetoothProfile.STATE_DISCONNECTED || status != BluetoothGatt.GATT_SUCCESS) {
332
333
 
@@ -335,45 +336,45 @@ public class Peripheral extends BluetoothGattCallback {
335
336
  discoverServicesRunnable = null;
336
337
  }
337
338
 
338
- for (Callback writeCallback: writeCallbacks) {
339
- writeCallback.invoke("Device disconnected");
340
- }
341
- writeCallbacks.clear();
342
-
343
- for (Callback retrieveServicesCallback: retrieveServicesCallbacks) {
344
- retrieveServicesCallback.invoke("Device disconnected");
345
- }
346
- retrieveServicesCallbacks.clear();
347
-
348
- for (Callback readRSSICallback: readRSSICallbacks) {
349
- readRSSICallback.invoke("Device disconnected");
350
- }
351
- readRSSICallbacks.clear();
352
-
353
- for (Callback registerNotifyCallback: registerNotifyCallbacks) {
354
- registerNotifyCallback.invoke("Device disconnected");
355
- }
356
- registerNotifyCallbacks.clear();
357
-
358
- for (Callback requestMTUCallback: requestMTUCallbacks) {
359
- requestMTUCallback.invoke("Device disconnected");
360
- }
361
- requestMTUCallbacks.clear();
362
-
363
- for (Callback readCallback: readCallbacks) {
364
- readCallback.invoke("Device disconnected");
365
- }
366
- readCallbacks.clear();
367
-
368
- for (Callback readDescriptorCallback: readDescriptorCallbacks) {
339
+ for (Callback writeCallback : writeCallbacks) {
340
+ writeCallback.invoke("Device disconnected");
341
+ }
342
+ writeCallbacks.clear();
343
+
344
+ for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
345
+ retrieveServicesCallback.invoke("Device disconnected");
346
+ }
347
+ retrieveServicesCallbacks.clear();
348
+
349
+ for (Callback readRSSICallback : readRSSICallbacks) {
350
+ readRSSICallback.invoke("Device disconnected");
351
+ }
352
+ readRSSICallbacks.clear();
353
+
354
+ for (Callback registerNotifyCallback : registerNotifyCallbacks) {
355
+ registerNotifyCallback.invoke("Device disconnected");
356
+ }
357
+ registerNotifyCallbacks.clear();
358
+
359
+ for (Callback requestMTUCallback : requestMTUCallbacks) {
360
+ requestMTUCallback.invoke("Device disconnected");
361
+ }
362
+ requestMTUCallbacks.clear();
363
+
364
+ for (Callback readCallback : readCallbacks) {
365
+ readCallback.invoke("Device disconnected");
366
+ }
367
+ readCallbacks.clear();
368
+
369
+ for (Callback readDescriptorCallback : readDescriptorCallbacks) {
369
370
  readDescriptorCallback.invoke("Device disconnected");
370
371
  }
371
372
  readDescriptorCallbacks.clear();
372
373
 
373
- for (Callback connectCallback: connectCallbacks) {
374
- connectCallback.invoke("Connection error");
375
- }
376
- connectCallbacks.clear();
374
+ for (Callback connectCallback : connectCallbacks) {
375
+ connectCallback.invoke("Connection error");
376
+ }
377
+ connectCallbacks.clear();
377
378
 
378
379
  writeQueue.clear();
379
380
  commandQueue.clear();
@@ -417,29 +418,37 @@ public class Peripheral extends BluetoothGattCallback {
417
418
  NotifyBufferContainer buffer = this.bufferedCharacteristics
418
419
  .get(this.bufferedCharacteristicsKey(service, charString));
419
420
  byte[] dataValue = characteristic.getValue();
420
- if (buffer != null) {
421
- buffer.put(dataValue);
422
- Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
423
- buffer.size() + " from peripheral: " + device.getAddress());
424
-
425
- if (buffer.isBufferFull()) {
426
- Log.d(BleManager.LOG_TAG, "onCharacteristicChanged sending buffered data " + buffer.size());
427
-
428
- // send'm and reset
429
- dataValue = buffer.items.array();
430
- buffer.resetBuffer();
431
- } else {
432
- return;
421
+ // If for some reason the value's length >= 2*buffer size this will be able to
422
+ // handle it
423
+ while (dataValue != null) {
424
+ byte[] rest = null;
425
+ if (buffer != null) {
426
+ rest = buffer.put(dataValue);
427
+ Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
428
+ buffer.size() + " from peripheral: " + device.getAddress());
429
+
430
+ if (buffer.isBufferFull()) {
431
+ Log.d(BleManager.LOG_TAG, "onCharacteristicChanged sending buffered data " + buffer.size());
432
+
433
+ // fetch and reset
434
+ dataValue = buffer.items.array();
435
+ buffer.resetBuffer();
436
+ } else {
437
+ return;
438
+ }
433
439
  }
440
+ Log.d(BleManager.LOG_TAG, "onCharacteristicChanged: " + BleManager.bytesToHex(dataValue)
441
+ + " from peripheral: " + device.getAddress());
442
+ WritableMap map = Arguments.createMap();
443
+ map.putString("peripheral", device.getAddress());
444
+ map.putString("characteristic", charString);
445
+ map.putString("service", service);
446
+ map.putArray("value", BleManager.bytesToWritableArray(dataValue));
447
+ sendEvent("BleManagerDidUpdateValueForCharacteristic", map);
448
+
449
+ // Check if rest exists. If so it needs to be added to the clean buffer
450
+ dataValue = rest;
434
451
  }
435
- Log.d(BleManager.LOG_TAG, "onCharacteristicChanged: " + BleManager.bytesToHex(dataValue)
436
- + " from peripheral: " + device.getAddress());
437
- WritableMap map = Arguments.createMap();
438
- map.putString("peripheral", device.getAddress());
439
- map.putString("characteristic", charString);
440
- map.putString("service", service);
441
- map.putArray("value", BleManager.bytesToWritableArray(dataValue));
442
- sendEvent("BleManagerDidUpdateValueForCharacteristic", map);
443
452
 
444
453
  } catch (Exception e) {
445
454
  Log.d(BleManager.LOG_TAG, "onCharacteristicChanged ERROR: " + e);
@@ -456,23 +465,22 @@ public class Peripheral extends BluetoothGattCallback {
456
465
  Log.d(BleManager.LOG_TAG, "Read needs bonding");
457
466
  }
458
467
 
459
- for (Callback readCallback: readCallbacks) {
460
- readCallback.invoke(
461
- "Error reading " + characteristic.getUuid() + " status=" + status,
462
- null
463
- );
464
- }
465
- readCallbacks.clear();
466
- } else if (!readCallbacks.isEmpty()) {
467
- final byte[] dataValue = copyOf(characteristic.getValue());
468
-
469
- for (Callback readCallback: readCallbacks) {
470
- readCallback.invoke(null, BleManager.bytesToWritableArray(dataValue));
471
- }
472
- readCallbacks.clear();
473
- }
474
- completedCommand();
475
- });
468
+ for (Callback readCallback : readCallbacks) {
469
+ readCallback.invoke(
470
+ "Error reading " + characteristic.getUuid() + " status=" + status,
471
+ null);
472
+ }
473
+ readCallbacks.clear();
474
+ } else if (!readCallbacks.isEmpty()) {
475
+ final byte[] dataValue = copyOf(characteristic.getValue());
476
+
477
+ for (Callback readCallback : readCallbacks) {
478
+ readCallback.invoke(null, BleManager.bytesToWritableArray(dataValue));
479
+ }
480
+ readCallbacks.clear();
481
+ }
482
+ completedCommand();
483
+ });
476
484
 
477
485
  }
478
486
 
@@ -480,46 +488,46 @@ public class Peripheral extends BluetoothGattCallback {
480
488
  public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
481
489
  super.onCharacteristicWrite(gatt, characteristic, status);
482
490
 
483
- mainHandler.post(() -> {
484
- if (writeQueue.size() > 0) {
485
- byte[] data = writeQueue.get(0);
486
- writeQueue.remove(0);
487
- doWrite(characteristic, data, null);
488
- } else if (status != BluetoothGatt.GATT_SUCCESS) {
489
- if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
490
- Log.d(BleManager.LOG_TAG, "Write needs bonding");
491
- // *not* doing completedCommand()
492
- return;
493
- }
494
- for (Callback writeCallback: writeCallbacks) {
495
- writeCallback.invoke("Error writing " + characteristic.getUuid() + " status=" + status, null);
496
- }
497
- writeCallbacks.clear();
498
- } else if (!writeCallbacks.isEmpty()) {
499
- for (Callback writeCallback: writeCallbacks) {
500
- writeCallback.invoke();
501
- }
502
- writeCallbacks.clear();
503
- }
504
- completedCommand();
505
- });
506
- }
507
-
508
- @Override
509
- public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
510
- mainHandler.post(() -> {
511
- if (!registerNotifyCallbacks.isEmpty()) {
512
- if (status == BluetoothGatt.GATT_SUCCESS) {
513
- for (Callback registerNotifyCallback: registerNotifyCallbacks) {
514
- registerNotifyCallback.invoke();
515
- }
516
- Log.d(BleManager.LOG_TAG, "onDescriptorWrite success");
517
- } else {
518
- for (Callback registerNotifyCallback: registerNotifyCallbacks) {
519
- registerNotifyCallback.invoke("Error writing descriptor status=" + status, null);
520
- }
521
- Log.e(BleManager.LOG_TAG, "Error writing descriptor status=" + status);
522
- }
491
+ mainHandler.post(() -> {
492
+ if (writeQueue.size() > 0) {
493
+ byte[] data = writeQueue.get(0);
494
+ writeQueue.remove(0);
495
+ doWrite(characteristic, data, null);
496
+ } else if (status != BluetoothGatt.GATT_SUCCESS) {
497
+ if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
498
+ Log.d(BleManager.LOG_TAG, "Write needs bonding");
499
+ // *not* doing completedCommand()
500
+ return;
501
+ }
502
+ for (Callback writeCallback : writeCallbacks) {
503
+ writeCallback.invoke("Error writing " + characteristic.getUuid() + " status=" + status, null);
504
+ }
505
+ writeCallbacks.clear();
506
+ } else if (!writeCallbacks.isEmpty()) {
507
+ for (Callback writeCallback : writeCallbacks) {
508
+ writeCallback.invoke();
509
+ }
510
+ writeCallbacks.clear();
511
+ }
512
+ completedCommand();
513
+ });
514
+ }
515
+
516
+ @Override
517
+ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
518
+ mainHandler.post(() -> {
519
+ if (!registerNotifyCallbacks.isEmpty()) {
520
+ if (status == BluetoothGatt.GATT_SUCCESS) {
521
+ for (Callback registerNotifyCallback : registerNotifyCallbacks) {
522
+ registerNotifyCallback.invoke();
523
+ }
524
+ Log.d(BleManager.LOG_TAG, "onDescriptorWrite success");
525
+ } else {
526
+ for (Callback registerNotifyCallback : registerNotifyCallbacks) {
527
+ registerNotifyCallback.invoke("Error writing descriptor status=" + status, null);
528
+ }
529
+ Log.e(BleManager.LOG_TAG, "Error writing descriptor status=" + status);
530
+ }
523
531
 
524
532
  registerNotifyCallbacks.clear();
525
533
  } else {
@@ -543,8 +551,7 @@ public class Peripheral extends BluetoothGattCallback {
543
551
  for (Callback readDescriptorCallback : readDescriptorCallbacks) {
544
552
  readDescriptorCallback.invoke(
545
553
  "Error reading descriptor " + descriptor.getUuid() + " status=" + status,
546
- null
547
- );
554
+ null);
548
555
  }
549
556
  readDescriptorCallbacks.clear();
550
557
  } else if (!readDescriptorCallbacks.isEmpty()) {
@@ -553,8 +560,7 @@ public class Peripheral extends BluetoothGattCallback {
553
560
  for (Callback readDescriptorCallback : readDescriptorCallbacks) {
554
561
  readDescriptorCallback.invoke(
555
562
  null,
556
- BleManager.bytesToWritableArray(dataValue)
557
- );
563
+ BleManager.bytesToWritableArray(dataValue));
558
564
  }
559
565
 
560
566
  readDescriptorCallbacks.clear();
@@ -567,18 +573,18 @@ public class Peripheral extends BluetoothGattCallback {
567
573
  public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
568
574
  super.onReadRemoteRssi(gatt, rssi, status);
569
575
 
570
- mainHandler.post(() -> {
571
- if (!readRSSICallbacks.isEmpty()) {
572
- if (status == BluetoothGatt.GATT_SUCCESS) {
573
- updateRssi(rssi);
574
- for (Callback readRSSICallback: readRSSICallbacks) {
575
- readRSSICallback.invoke(null, rssi);
576
- }
577
- } else {
578
- for (Callback readRSSICallback: readRSSICallbacks) {
579
- readRSSICallback.invoke("Error reading RSSI status=" + status, null);
580
- }
581
- }
576
+ mainHandler.post(() -> {
577
+ if (!readRSSICallbacks.isEmpty()) {
578
+ if (status == BluetoothGatt.GATT_SUCCESS) {
579
+ updateRssi(rssi);
580
+ for (Callback readRSSICallback : readRSSICallbacks) {
581
+ readRSSICallback.invoke(null, rssi);
582
+ }
583
+ } else {
584
+ for (Callback readRSSICallback : readRSSICallbacks) {
585
+ readRSSICallback.invoke("Error reading RSSI status=" + status, null);
586
+ }
587
+ }
582
588
 
583
589
  readRSSICallbacks.clear();
584
590
  }
@@ -618,7 +624,8 @@ public class Peripheral extends BluetoothGattCallback {
618
624
  return;
619
625
  }
620
626
 
621
- final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUIDHelper.uuidFromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
627
+ final BluetoothGattDescriptor descriptor = characteristic
628
+ .getDescriptor(UUIDHelper.uuidFromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
622
629
  if (descriptor == null) {
623
630
  callback.invoke("Set notification failed for " + characteristicUUID);
624
631
  completedCommand();
@@ -629,10 +636,12 @@ public class Peripheral extends BluetoothGattCallback {
629
636
  byte[] value;
630
637
  if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
631
638
  Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set NOTIFY");
632
- value = notify ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
639
+ value = notify ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
640
+ : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
633
641
  } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
634
642
  Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set INDICATE");
635
- value = notify ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
643
+ value = notify ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
644
+ : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
636
645
  } else {
637
646
  String msg = "Characteristic " + characteristicUUID + " does not have NOTIFY or INDICATE property set";
638
647
  Log.d(BleManager.LOG_TAG, msg);
@@ -653,21 +662,22 @@ public class Peripheral extends BluetoothGattCallback {
653
662
  Log.d(BleManager.LOG_TAG, "Exception in setNotify", e);
654
663
  }
655
664
 
656
- if (! result) {
657
- for (Callback registerNotifyCallback: registerNotifyCallbacks) {
658
- registerNotifyCallback.invoke("writeDescriptor failed for descriptor: " + descriptor.getUuid(), null);
659
- }
660
- registerNotifyCallbacks.clear();
661
- completedCommand();
662
- }
663
- }
665
+ if (!result) {
666
+ for (Callback registerNotifyCallback : registerNotifyCallbacks) {
667
+ registerNotifyCallback.invoke("writeDescriptor failed for descriptor: " + descriptor.getUuid(), null);
668
+ }
669
+ registerNotifyCallbacks.clear();
670
+ completedCommand();
671
+ }
672
+ }
664
673
 
665
674
  public void registerNotify(UUID serviceUUID, UUID characteristicUUID, Integer buffer, Callback callback) {
666
675
  if (!enqueue(() -> {
667
676
  Log.d(BleManager.LOG_TAG, "registerNotify");
668
677
  if (buffer > 1) {
669
678
  Log.d(BleManager.LOG_TAG, "registerNotify using buffer");
670
- String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
679
+ String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(),
680
+ characteristicUUID.toString());
671
681
  this.bufferedCharacteristics.put(bufferKey, new NotifyBufferContainer(buffer));
672
682
  }
673
683
  this.setNotify(serviceUUID, characteristicUUID, true, callback);
@@ -696,7 +706,7 @@ public class Peripheral extends BluetoothGattCallback {
696
706
  // until we find the best match
697
707
  // This function prefers Notify over Indicate
698
708
  private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service,
699
- UUID characteristicUUID) {
709
+ UUID characteristicUUID) {
700
710
 
701
711
  try {
702
712
  // Check for Notify first
@@ -742,18 +752,19 @@ public class Peripheral extends BluetoothGattCallback {
742
752
  return;
743
753
  }
744
754
 
745
- this.readCallbacks.addLast(callback);
746
- if (!gatt.readCharacteristic(characteristic)) {
747
- for (Callback readCallback: readCallbacks) {
748
- readCallback.invoke("Read failed", null);
749
- }
750
- readCallbacks.clear();
751
- completedCommand();
752
- }
753
- });
754
- }
755
-
756
- public void readDescriptor(UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID, final Callback callback) {
755
+ this.readCallbacks.addLast(callback);
756
+ if (!gatt.readCharacteristic(characteristic)) {
757
+ for (Callback readCallback : readCallbacks) {
758
+ readCallback.invoke("Read failed", null);
759
+ }
760
+ readCallbacks.clear();
761
+ completedCommand();
762
+ }
763
+ });
764
+ }
765
+
766
+ public void readDescriptor(UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID,
767
+ final Callback callback) {
757
768
  enqueue(() -> {
758
769
  if (!isConnected() || gatt == null) {
759
770
  callback.invoke("Device is not connected", null);
@@ -778,17 +789,19 @@ public class Peripheral extends BluetoothGattCallback {
778
789
  }
779
790
 
780
791
  final int readPermissionBitMask = BluetoothGattDescriptor.PERMISSION_READ
781
- | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED
782
- | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM;
792
+ | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED
793
+ | BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM;
783
794
  if ((descriptor.getPermissions() & readPermissionBitMask) != 0) {
784
- callback.invoke("Read descriptor failed for " + descriptorUUID + ": Descriptor is missing read permission", null);
795
+ callback.invoke(
796
+ "Read descriptor failed for " + descriptorUUID + ": Descriptor is missing read permission",
797
+ null);
785
798
  completedCommand();
786
799
  return;
787
800
  }
788
801
 
789
802
  this.readDescriptorCallbacks.addLast(callback);
790
803
  if (!gatt.readDescriptor(descriptor)) {
791
- for (Callback readDescriptorCallback: readDescriptorCallbacks) {
804
+ for (Callback readDescriptorCallback : readDescriptorCallbacks) {
792
805
  readDescriptorCallback.invoke("Reading descriptor failed", null);
793
806
  }
794
807
  readDescriptorCallbacks.clear();
@@ -798,7 +811,8 @@ public class Peripheral extends BluetoothGattCallback {
798
811
  }
799
812
 
800
813
  private byte[] copyOf(byte[] source) {
801
- if (source == null) return new byte[0];
814
+ if (source == null)
815
+ return new byte[0];
802
816
  final int sourceLength = source.length;
803
817
  final byte[] copy = new byte[sourceLength];
804
818
  System.arraycopy(source, 0, copy, 0, sourceLength);
@@ -858,35 +872,35 @@ public class Peripheral extends BluetoothGattCallback {
858
872
  }
859
873
  }
860
874
 
861
- public void readRSSI(final Callback callback) {
862
- if (!enqueue(() -> {
863
- if (!isConnected()) {
864
- callback.invoke("Device is not connected", null);
865
- completedCommand();
866
- return;
867
- } else if (gatt == null) {
868
- callback.invoke("BluetoothGatt is null", null);
869
- completedCommand();
870
- return;
871
- } else {
872
- readRSSICallbacks.addLast(callback);
873
- if (!gatt.readRemoteRssi()) {
874
- for (Callback readRSSICallback: readRSSICallbacks) {
875
- readRSSICallback.invoke("Read RSSI failed", null);
876
- }
877
- readRSSICallbacks.clear();
878
- completedCommand();
879
- }
880
- }
881
- })) {
882
- Log.d(BleManager.LOG_TAG, "Could not queue readRemoteRssi command");
883
- }
884
- }
875
+ public void readRSSI(final Callback callback) {
876
+ if (!enqueue(() -> {
877
+ if (!isConnected()) {
878
+ callback.invoke("Device is not connected", null);
879
+ completedCommand();
880
+ return;
881
+ } else if (gatt == null) {
882
+ callback.invoke("BluetoothGatt is null", null);
883
+ completedCommand();
884
+ return;
885
+ } else {
886
+ readRSSICallbacks.addLast(callback);
887
+ if (!gatt.readRemoteRssi()) {
888
+ for (Callback readRSSICallback : readRSSICallbacks) {
889
+ readRSSICallback.invoke("Read RSSI failed", null);
890
+ }
891
+ readRSSICallbacks.clear();
892
+ completedCommand();
893
+ }
894
+ }
895
+ })) {
896
+ Log.d(BleManager.LOG_TAG, "Could not queue readRemoteRssi command");
897
+ }
898
+ }
885
899
 
886
900
  public void refreshCache(Callback callback) {
887
901
  enqueue(() -> {
888
902
  try {
889
- Method localMethod = gatt.getClass().getMethod("refresh", new Class[0]);
903
+ Method localMethod = gatt.getClass().getMethod("refresh", new Class[0]);
890
904
  if (localMethod != null) {
891
905
  boolean res = ((Boolean) localMethod.invoke(gatt, new Object[0])).booleanValue();
892
906
  callback.invoke(null, res);
@@ -924,7 +938,7 @@ public class Peripheral extends BluetoothGattCallback {
924
938
  // and UUID of all characteristics instead of using
925
939
  // service.getCharacteristic(characteristicUUID)
926
940
  private BluetoothGattCharacteristic findReadableCharacteristic(BluetoothGattService service,
927
- UUID characteristicUUID) {
941
+ UUID characteristicUUID) {
928
942
 
929
943
  if (service != null) {
930
944
  int read = BluetoothGattCharacteristic.PROPERTY_READ;
@@ -945,31 +959,30 @@ public class Peripheral extends BluetoothGattCallback {
945
959
  return null;
946
960
  }
947
961
 
948
- public boolean doWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
949
- final byte[] copyOfData = copyOf(data);
950
- return enqueue(new Runnable() {
951
- @Override
952
- public void run() {
953
- characteristic.setValue(copyOfData);
954
- if (
955
- characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
956
- && callback != null
957
- ) {
958
- writeCallbacks.addLast(callback);
959
- }
960
- if (!gatt.writeCharacteristic(characteristic)) {
961
- // write without response, caller will handle the callback
962
- for (Callback writeCallback: writeCallbacks) {
963
- writeCallback.invoke("Write failed", writeCallback);
964
- }
965
- writeCallbacks.clear();
966
- completedCommand();
967
- }
968
- }
969
- });
970
- }
971
-
972
- public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Integer maxByteSize, Integer queueSleepTime, Callback callback, int writeType) {
962
+ public boolean doWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
963
+ final byte[] copyOfData = copyOf(data);
964
+ return enqueue(new Runnable() {
965
+ @Override
966
+ public void run() {
967
+ characteristic.setValue(copyOfData);
968
+ if (characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
969
+ && callback != null) {
970
+ writeCallbacks.addLast(callback);
971
+ }
972
+ if (!gatt.writeCharacteristic(characteristic)) {
973
+ // write without response, caller will handle the callback
974
+ for (Callback writeCallback : writeCallbacks) {
975
+ writeCallback.invoke("Write failed", writeCallback);
976
+ }
977
+ writeCallbacks.clear();
978
+ completedCommand();
979
+ }
980
+ }
981
+ });
982
+ }
983
+
984
+ public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Integer maxByteSize,
985
+ Integer queueSleepTime, Callback callback, int writeType) {
973
986
  enqueue(() -> {
974
987
  if (!isConnected() || gatt == null) {
975
988
  callback.invoke("Device is not connected", null);
@@ -978,7 +991,8 @@ public class Peripheral extends BluetoothGattCallback {
978
991
  }
979
992
 
980
993
  BluetoothGattService service = gatt.getService(serviceUUID);
981
- BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID, writeType);
994
+ BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID,
995
+ writeType);
982
996
 
983
997
  if (characteristic == null) {
984
998
  callback.invoke("Characteristic " + characteristicUUID + " not found.");
@@ -1085,36 +1099,36 @@ public class Peripheral extends BluetoothGattCallback {
1085
1099
  return;
1086
1100
  }
1087
1101
 
1088
- if (Build.VERSION.SDK_INT >= LOLLIPOP) {
1089
- requestMTUCallbacks.addLast(callback);
1090
- if (!gatt.requestMtu(mtu)) {
1091
- for (Callback requestMTUCallback: requestMTUCallbacks) {
1092
- requestMTUCallback.invoke("Request MTU failed", null);
1093
- }
1094
- requestMTUCallbacks.clear();
1095
- completedCommand();
1096
- }
1097
- } else {
1098
- callback.invoke("Requesting MTU requires at least API level 21", null);
1099
- completedCommand();
1100
- }
1101
- });
1102
- }
1103
-
1104
- @Override
1105
- public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
1106
- super.onMtuChanged(gatt, mtu, status);
1107
- mainHandler.post(() -> {
1108
- if (!requestMTUCallbacks.isEmpty()) {
1109
- if (status == BluetoothGatt.GATT_SUCCESS) {
1110
- for (Callback requestMTUCallback: requestMTUCallbacks) {
1111
- requestMTUCallback.invoke(null, mtu);
1112
- }
1113
- } else {
1114
- for (Callback requestMTUCallback: requestMTUCallbacks) {
1115
- requestMTUCallback.invoke("Error requesting MTU status = " + status, null);
1116
- }
1117
- }
1102
+ if (Build.VERSION.SDK_INT >= LOLLIPOP) {
1103
+ requestMTUCallbacks.addLast(callback);
1104
+ if (!gatt.requestMtu(mtu)) {
1105
+ for (Callback requestMTUCallback : requestMTUCallbacks) {
1106
+ requestMTUCallback.invoke("Request MTU failed", null);
1107
+ }
1108
+ requestMTUCallbacks.clear();
1109
+ completedCommand();
1110
+ }
1111
+ } else {
1112
+ callback.invoke("Requesting MTU requires at least API level 21", null);
1113
+ completedCommand();
1114
+ }
1115
+ });
1116
+ }
1117
+
1118
+ @Override
1119
+ public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
1120
+ super.onMtuChanged(gatt, mtu, status);
1121
+ mainHandler.post(() -> {
1122
+ if (!requestMTUCallbacks.isEmpty()) {
1123
+ if (status == BluetoothGatt.GATT_SUCCESS) {
1124
+ for (Callback requestMTUCallback : requestMTUCallbacks) {
1125
+ requestMTUCallback.invoke(null, mtu);
1126
+ }
1127
+ } else {
1128
+ for (Callback requestMTUCallback : requestMTUCallbacks) {
1129
+ requestMTUCallback.invoke("Error requesting MTU status = " + status, null);
1130
+ }
1131
+ }
1118
1132
 
1119
1133
  requestMTUCallbacks.clear();
1120
1134
  }
@@ -1128,7 +1142,7 @@ public class Peripheral extends BluetoothGattCallback {
1128
1142
  // and UUID of all characteristics instead of using
1129
1143
  // service.getCharacteristic(characteristicUUID)
1130
1144
  private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service,
1131
- UUID characteristicUUID, int writeType) {
1145
+ UUID characteristicUUID, int writeType) {
1132
1146
  try {
1133
1147
  // get write property
1134
1148
  int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;