react-native-ble-manager 7.6.3 → 8.0.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.
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Before open an issue**
11
+ - Check the closed issues, your question maybe is not new.
12
+ - We can't debug or reproduce hardware issue.
13
+ - If the library is not working 99% you did something wrong, in the code, installation or in the phone permissions.
14
+
15
+ **Describe the bug**
16
+ A clear and concise description of what the bug is.
17
+
18
+ **To Reproduce**
19
+ Steps to reproduce the behavior:
20
+ 1. Go to '...'
21
+ 2. Click on '....'
22
+ 3. Scroll down to '....'
23
+ 4. See error
24
+
25
+ **Expected behavior**
26
+ A clear and concise description of what you expected to happen.
27
+
28
+ **Screenshots**
29
+ If applicable, add screenshots to help explain your problem.
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - react-native-ble-manager version:
35
+ - react-native version:
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
package/README.md CHANGED
@@ -22,8 +22,8 @@ RN 0.30-0.39 supported until 2.4.3
22
22
 
23
23
  ```shell
24
24
  npm i --save react-native-ble-manager
25
- npx react-native link
26
25
  ```
26
+ The library support the react native autolink feature.
27
27
 
28
28
 
29
29
  ##### Android - Update Manifest
@@ -33,6 +33,8 @@ import java.util.Arrays;
33
33
  import java.util.Iterator;
34
34
  import java.util.List;
35
35
  import java.util.UUID;
36
+ import java.util.Queue;
37
+ import java.util.concurrent.ConcurrentLinkedQueue;
36
38
 
37
39
  import static android.os.Build.VERSION_CODES.LOLLIPOP;
38
40
  import static com.facebook.react.common.ReactConstants.TAG;
@@ -43,6 +45,8 @@ import static com.facebook.react.common.ReactConstants.TAG;
43
45
  public class Peripheral extends BluetoothGattCallback {
44
46
 
45
47
  private static final String CHARACTERISTIC_NOTIFICATION_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
48
+ public static final int GATT_INSUFFICIENT_AUTHENTICATION = 5;
49
+ public static final int GATT_AUTH_FAIL = 137;
46
50
 
47
51
  private final BluetoothDevice device;
48
52
  private final Map<String, NotifyBufferContainer> bufferedCharacteristics;
@@ -61,6 +65,11 @@ public class Peripheral extends BluetoothGattCallback {
61
65
  private Callback registerNotifyCallback;
62
66
  private Callback requestMTUCallback;
63
67
 
68
+ private final Queue<Runnable> commandQueue = new ConcurrentLinkedQueue<>();
69
+ private final Handler mainHandler = new Handler(Looper.getMainLooper());
70
+ private Runnable discoverServicesRunnable;
71
+ private boolean commandQueueBusy = false;
72
+
64
73
  private List<byte[]> writeQueue = new ArrayList<>();
65
74
 
66
75
  public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, ReactContext reactContext) {
@@ -127,6 +136,9 @@ public class Peripheral extends BluetoothGattCallback {
127
136
  connectCallback = null;
128
137
  connected = false;
129
138
  clearBuffers();
139
+ commandQueue.clear();
140
+ commandQueueBusy = false;
141
+
130
142
  if (gatt != null) {
131
143
  try {
132
144
  gatt.disconnect();
@@ -272,11 +284,27 @@ public class Peripheral extends BluetoothGattCallback {
272
284
 
273
285
  if (status != BluetoothGatt.GATT_SUCCESS) {
274
286
  gatt.close();
287
+ // change the state to ensure the connection is teared down properly in the code below
288
+ newState = BluetoothProfile.STATE_DISCONNECTED;
275
289
  }
276
290
 
277
291
  if (newState == BluetoothProfile.STATE_CONNECTED) {
278
292
  connected = true;
279
293
 
294
+ discoverServicesRunnable = new Runnable() {
295
+ @Override
296
+ public void run() {
297
+ try {
298
+ gatt.discoverServices();
299
+ } catch (NullPointerException e) {
300
+ Log.d(BleManager.LOG_TAG, "onConnectionStateChange connected but gatt of Run method was null");
301
+ }
302
+ discoverServicesRunnable = null;
303
+ }
304
+ };
305
+
306
+ mainHandler.post(discoverServicesRunnable);
307
+
280
308
  sendConnectionEvent(device, "BleManagerConnectPeripheral", status);
281
309
 
282
310
  if (connectCallback != null) {
@@ -289,6 +317,11 @@ public class Peripheral extends BluetoothGattCallback {
289
317
 
290
318
  this.disconnect(true);
291
319
 
320
+ if (discoverServicesRunnable != null) {
321
+ mainHandler.removeCallbacks(discoverServicesRunnable);
322
+ discoverServicesRunnable = null;
323
+ }
324
+
292
325
  sendConnectionEvent(device, "BleManagerDisconnectPeripheral", status);
293
326
  List<Callback> callbacks = Arrays.asList(writeCallback, retrieveServicesCallback, readRSSICallback,
294
327
  readCallback, registerNotifyCallback, requestMTUCallback);
@@ -308,6 +341,8 @@ public class Peripheral extends BluetoothGattCallback {
308
341
  readRSSICallback = null;
309
342
  registerNotifyCallback = null;
310
343
  requestMTUCallback = null;
344
+ commandQueue.clear();
345
+ commandQueueBusy = false;
311
346
  }
312
347
 
313
348
  }
@@ -367,54 +402,60 @@ public class Peripheral extends BluetoothGattCallback {
367
402
  @Override
368
403
  public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
369
404
  super.onCharacteristicRead(gatt, characteristic, status);
370
- Log.d(BleManager.LOG_TAG, "onCharacteristicRead " + characteristic);
371
-
372
- if (readCallback != null) {
373
-
374
- if (status == BluetoothGatt.GATT_SUCCESS) {
375
- byte[] dataValue = characteristic.getValue();
376
405
 
377
- if (readCallback != null) {
378
- readCallback.invoke(null, BleManager.bytesToWritableArray(dataValue));
379
- }
380
- } else {
381
- readCallback.invoke("Error reading " + characteristic.getUuid() + " status=" + status, null);
406
+ if (status != BluetoothGatt.GATT_SUCCESS) {
407
+ if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
408
+ Log.d(BleManager.LOG_TAG, "Read needs bonding");
409
+ // *not* doing completedCommand()
410
+ return;
382
411
  }
383
-
412
+ sendBackrError(readCallback, "Error reading " + characteristic.getUuid() + " status=" + status);
413
+ } else if (readCallback != null) {
414
+ final byte[] dataValue = copyOf(characteristic.getValue());
415
+ final Callback callback = readCallback;
384
416
  readCallback = null;
385
-
417
+ mainHandler.post(new Runnable() {
418
+ @Override
419
+ public void run() {
420
+ callback.invoke(null, BleManager.bytesToWritableArray(dataValue));
421
+ }
422
+ });
386
423
  }
424
+
425
+ completedCommand();
387
426
  }
388
427
 
389
428
  @Override
390
429
  public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
391
430
  super.onCharacteristicWrite(gatt, characteristic, status);
392
431
 
393
- if (writeCallback != null) {
394
-
395
- if (writeQueue.size() > 0) {
396
- byte[] data = writeQueue.get(0);
397
- writeQueue.remove(0);
398
- doWrite(characteristic, data);
399
- } else {
400
-
401
- if (status == BluetoothGatt.GATT_SUCCESS) {
402
- writeCallback.invoke();
403
- } else {
404
- Log.e(BleManager.LOG_TAG, "Error onCharacteristicWrite:" + status);
405
- writeCallback.invoke("Error writing status: " + status);
406
- }
407
-
408
- writeCallback = null;
432
+ if (writeQueue.size() > 0) {
433
+ byte[] data = writeQueue.get(0);
434
+ writeQueue.remove(0);
435
+ doWrite(characteristic, data, writeCallback);
436
+ } else if (status != BluetoothGatt.GATT_SUCCESS) {
437
+ if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
438
+ Log.d(BleManager.LOG_TAG, "Write needs bonding");
439
+ // *not* doing completedCommand()
440
+ return;
409
441
  }
410
- } else {
411
- Log.e(BleManager.LOG_TAG, "No callback on write");
442
+ sendBackrError(writeCallback, "Error writing " + characteristic.getUuid() + " status=" + status);
443
+ } else if (writeCallback != null) {
444
+ final Callback callback = writeCallback;
445
+ mainHandler.post(new Runnable() {
446
+ @Override
447
+ public void run() {
448
+ callback.invoke();
449
+ }
450
+ });
451
+ writeCallback = null;
412
452
  }
453
+
454
+ completedCommand();
413
455
  }
414
456
 
415
457
  @Override
416
458
  public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
417
- super.onDescriptorWrite(gatt, descriptor, status);
418
459
  if (registerNotifyCallback != null) {
419
460
  if (status == BluetoothGatt.GATT_SUCCESS) {
420
461
  registerNotifyCallback.invoke();
@@ -428,6 +469,8 @@ public class Peripheral extends BluetoothGattCallback {
428
469
  } else {
429
470
  Log.e(BleManager.LOG_TAG, "onDescriptorWrite with no callback");
430
471
  }
472
+
473
+ completedCommand();
431
474
  }
432
475
 
433
476
  @Override
@@ -454,85 +497,90 @@ public class Peripheral extends BluetoothGattCallback {
454
497
  entry.getValue().resetBuffer();
455
498
  }
456
499
 
457
- private void setNotify(UUID serviceUUID, UUID characteristicUUID, Boolean notify, Integer buffer,
458
- Callback callback) {
459
- if (!isConnected()) {
500
+ private void setNotify(UUID serviceUUID, UUID characteristicUUID, final Boolean notify, Callback callback) {
501
+ if (! isConnected() || gatt == null) {
460
502
  callback.invoke("Device is not connected", null);
461
503
  return;
462
504
  }
463
- Log.d(BleManager.LOG_TAG, "setNotify");
464
505
 
465
- if (gatt == null) {
466
- callback.invoke("BluetoothGatt is null");
506
+ BluetoothGattService service = gatt.getService(serviceUUID);
507
+ final BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
508
+
509
+ if (characteristic == null) {
510
+ callback.invoke("Characteristic " + characteristicUUID + " not found");
467
511
  return;
468
512
  }
469
- BluetoothGattService service = gatt.getService(serviceUUID);
470
- BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
471
513
 
472
- if (characteristic != null) {
473
- if (gatt.setCharacteristicNotification(characteristic, notify)) {
514
+ if (! gatt.setCharacteristicNotification(characteristic, notify)) {
515
+ callback.invoke("Failed to register notification for " + characteristicUUID);
516
+ return;
517
+ }
474
518
 
475
- if (buffer > 1) {
476
- Log.d(BleManager.LOG_TAG, "Characteristic buffering " + characteristicUUID + " count:" + buffer);
477
- String key = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
478
- this.bufferedCharacteristics.put(key, new NotifyBufferContainer(key, buffer));
479
- }
519
+ final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUIDHelper.uuidFromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
520
+ if (descriptor == null) {
521
+ callback.invoke("Set notification failed for " + characteristicUUID);
522
+ return;
523
+ }
524
+
525
+ // Prefer notify over indicate
526
+ byte[] value;
527
+ if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
528
+ Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set NOTIFY");
529
+ value = notify ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
530
+ } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
531
+ Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set INDICATE");
532
+ value = notify ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
533
+ } else {
534
+ String msg = "Characteristic " + characteristicUUID + " does not have NOTIFY or INDICATE property set";
535
+ Log.d(BleManager.LOG_TAG, msg);
536
+ callback.invoke(msg);
537
+ return;
538
+ }
539
+ final byte[] finalValue = notify ? value : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
540
+ final Callback finalCallback = callback;
480
541
 
481
- BluetoothGattDescriptor descriptor = characteristic
482
- .getDescriptor(UUIDHelper.uuidFromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
483
- if (descriptor != null) {
484
-
485
- // Prefer notify over indicate
486
- if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
487
- Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set NOTIFY");
488
- descriptor.setValue(notify ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
489
- : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
490
- } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
491
- Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " set INDICATE");
492
- descriptor.setValue(notify ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
493
- : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
494
- } else {
495
- Log.d(BleManager.LOG_TAG, "Characteristic " + characteristicUUID
496
- + " does not have NOTIFY or INDICATE property set");
542
+ boolean result = commandQueue.add(new Runnable() {
543
+ @Override
544
+ public void run() {
545
+ if (! isConnected()) {
546
+ completedCommand();
547
+ return;
497
548
  }
498
549
 
550
+ boolean result = false;
499
551
  try {
500
- registerNotifyCallback = callback;
501
- if (gatt.writeDescriptor(descriptor)) {
502
- Log.d(BleManager.LOG_TAG, "setNotify complete");
503
- } else {
504
- registerNotifyCallback = null;
505
- callback.invoke(
506
- "Failed to set client characteristic notification for " + characteristicUUID);
507
- }
508
- } catch (Exception e) {
509
- Log.d(BleManager.LOG_TAG, "Error on setNotify", e);
510
- callback.invoke("Failed to set client characteristic notification for " + characteristicUUID
511
- + ", error: " + e.getMessage());
552
+ result = gatt.setCharacteristicNotification(characteristic, notify);
553
+ // Then write to descriptor
554
+ descriptor.setValue(finalValue);
555
+ registerNotifyCallback = finalCallback;
556
+ result = gatt.writeDescriptor(descriptor);
557
+ } catch(Exception e) {
558
+ Log.d(BleManager.LOG_TAG, "Exception in setNotify", e);
512
559
  }
513
560
 
514
- } else {
515
- callback.invoke("Set notification failed for " + characteristicUUID);
561
+ if (! result) {
562
+ sendBackrError(registerNotifyCallback, "writeDescriptor failed for descriptor: " + descriptor.getUuid());
563
+ registerNotifyCallback = null;
564
+ completedCommand();
565
+ }
516
566
  }
567
+ });
517
568
 
518
- } else {
519
- callback.invoke("Failed to register notification for " + characteristicUUID);
520
- }
521
-
569
+ if (result) {
570
+ nextCommand();
522
571
  } else {
523
- callback.invoke("Characteristic " + characteristicUUID + " not found");
572
+ Log.e(BleManager.LOG_TAG, "Could not enqueue setNotify command");
524
573
  }
525
-
526
574
  }
527
575
 
528
576
  public void registerNotify(UUID serviceUUID, UUID characteristicUUID, Integer buffer, Callback callback) {
529
577
  Log.d(BleManager.LOG_TAG, "registerNotify");
530
- this.setNotify(serviceUUID, characteristicUUID, true, buffer, callback);
578
+ this.setNotify(serviceUUID, characteristicUUID, true, callback);
531
579
  }
532
580
 
533
581
  public void removeNotify(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
534
582
  Log.d(BleManager.LOG_TAG, "removeNotify");
535
- this.setNotify(serviceUUID, characteristicUUID, false, 1, callback);
583
+ this.setNotify(serviceUUID, characteristicUUID, false, callback);
536
584
  }
537
585
 
538
586
  // Some devices reuse UUIDs across characteristics, so we can't use
@@ -572,30 +620,100 @@ public class Peripheral extends BluetoothGattCallback {
572
620
 
573
621
  public void read(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
574
622
 
575
- if (!isConnected()) {
623
+ if (!isConnected() || gatt == null) {
576
624
  callback.invoke("Device is not connected", null);
577
625
  return;
578
626
  }
579
- if (gatt == null) {
580
- callback.invoke("BluetoothGatt is null", null);
581
- return;
582
- }
583
627
 
584
628
  BluetoothGattService service = gatt.getService(serviceUUID);
585
- BluetoothGattCharacteristic characteristic = findReadableCharacteristic(service, characteristicUUID);
629
+ final BluetoothGattCharacteristic characteristic = findReadableCharacteristic(service, characteristicUUID);
586
630
 
587
631
  if (characteristic == null) {
588
632
  callback.invoke("Characteristic " + characteristicUUID + " not found.", null);
633
+ return;
634
+ }
635
+
636
+ final Callback reactcb = callback;
637
+ boolean result = commandQueue.add(new Runnable() {
638
+ @Override
639
+ public void run() {
640
+ readCallback = reactcb;
641
+ if (! gatt.readCharacteristic(characteristic)) {
642
+ readCallback = null;
643
+ sendBackrError(reactcb, "Read failed");
644
+ completedCommand();
645
+ }
646
+ }
647
+ });
648
+
649
+ if (result) {
650
+ nextCommand();
589
651
  } else {
590
- readCallback = callback;
591
- if (!gatt.readCharacteristic(characteristic)) {
592
- readCallback = null;
593
- callback.invoke("Read failed", null);
652
+ Log.d(BleManager.LOG_TAG, "Could not queue read characteristic command");
653
+ }
654
+ }
655
+
656
+ private void sendBackrError(final Callback callback, final String message) {
657
+ new Handler(Looper.getMainLooper()).post(new Runnable() {
658
+ @Override
659
+ public void run() {
660
+ callback.invoke(message, null);
661
+ }
662
+ });
663
+ }
664
+
665
+ private byte[] copyOf(byte[] source) {
666
+ if (source == null) return new byte[0];
667
+ final int sourceLength = source.length;
668
+ final byte[] copy = new byte[sourceLength];
669
+ System.arraycopy(source, 0, copy, 0, sourceLength);
670
+ return copy;
671
+ }
672
+
673
+ private void completedCommand() {
674
+ commandQueue.poll();
675
+ commandQueueBusy = false;
676
+ nextCommand();
677
+ }
678
+
679
+ private void nextCommand() {
680
+ synchronized (this) {
681
+ if (commandQueueBusy) {
682
+ Log.d(BleManager.LOG_TAG, "Command queue busy");
683
+ return;
684
+ }
685
+
686
+ final Runnable nextCommand = commandQueue.peek();
687
+ if (nextCommand == null) {
688
+ Log.d(BleManager.LOG_TAG, "Command queue empty");
689
+ return;
690
+ }
691
+
692
+ // Check if we still have a valid gatt object
693
+ if (gatt == null) {
694
+ Log.d(BleManager.LOG_TAG, "Error, gatt is null");
695
+ commandQueue.clear();
696
+ commandQueueBusy = false;
697
+ return;
594
698
  }
699
+
700
+ // Execute the next command in the queue
701
+ commandQueueBusy = true;
702
+ mainHandler.post(new Runnable() {
703
+ @Override
704
+ public void run() {
705
+ try {
706
+ nextCommand.run();
707
+ } catch (Exception ex) {
708
+ Log.d(BleManager.LOG_TAG, "Error, command exception");
709
+ completedCommand();
710
+ }
711
+ }
712
+ });
595
713
  }
596
714
  }
597
715
 
598
- public void readRSSI(Callback callback) {
716
+ public void readRSSI(final Callback callback) {
599
717
  if (!isConnected()) {
600
718
  callback.invoke("Device is not connected", null);
601
719
  return;
@@ -605,11 +723,26 @@ public class Peripheral extends BluetoothGattCallback {
605
723
  return;
606
724
  }
607
725
 
608
- readRSSICallback = callback;
726
+ final boolean result = commandQueue.add(new Runnable() {
727
+ @Override
728
+ public void run() {
729
+ if (isConnected()) {
730
+ readRSSICallback = callback;
731
+ if (! gatt.readRemoteRssi()) {
732
+ readCallback = null;
733
+ sendBackrError(callback, "Read RSSI failed");
734
+ completedCommand();
735
+ }
736
+ } else {
737
+ completedCommand();
738
+ }
739
+ }
740
+ });
609
741
 
610
- if (!gatt.readRemoteRssi()) {
611
- readCallback = null;
612
- callback.invoke("Read RSSI failed", null);
742
+ if (result) {
743
+ nextCommand();
744
+ } else {
745
+ Log.d(BleManager.LOG_TAG, "Could not queue readRemoteRssi command");
613
746
  }
614
747
  }
615
748
 
@@ -667,112 +800,105 @@ public class Peripheral extends BluetoothGattCallback {
667
800
  return null;
668
801
  }
669
802
 
670
- public boolean doWrite(BluetoothGattCharacteristic characteristic, byte[] data) {
671
- characteristic.setValue(data);
803
+ public boolean doWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
804
+ final byte[] copyOfData = copyOf(data);
805
+ boolean result = commandQueue.add(new Runnable() {
806
+ @Override
807
+ public void run() {
808
+ characteristic.setValue(copyOfData);
809
+ if (characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
810
+ writeCallback = callback;
811
+ else
812
+ writeCallback = null;
813
+ if (!gatt.writeCharacteristic(characteristic)) {
814
+ sendBackrError(writeCallback, "Write failed");
815
+ writeCallback = null;
816
+ completedCommand();
817
+ }
818
+ }
819
+ });
672
820
 
673
- if (!gatt.writeCharacteristic(characteristic)) {
674
- Log.d(BleManager.LOG_TAG, "Error on doWrite");
675
- return false;
821
+ if (result) {
822
+ nextCommand();
823
+ } else {
824
+ Log.d(BleManager.LOG_TAG, "Could not queue write characteristic command");
676
825
  }
677
- return true;
826
+
827
+ return result;
678
828
  }
679
829
 
680
- public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Integer maxByteSize,
681
- Integer queueSleepTime, Callback callback, int writeType) {
682
- if (!isConnected()) {
830
+ public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Integer maxByteSize, Integer queueSleepTime, Callback callback, int writeType) {
831
+ if (!isConnected() || gatt == null) {
683
832
  callback.invoke("Device is not connected", null);
684
833
  return;
685
834
  }
686
- if (gatt == null) {
687
- callback.invoke("BluetoothGatt is null");
688
- } else {
689
- BluetoothGattService service = gatt.getService(serviceUUID);
690
- BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID,
691
- writeType);
692
835
 
693
- if (characteristic == null) {
694
- callback.invoke("Characteristic " + characteristicUUID + " not found.");
695
- } else {
696
- characteristic.setWriteType(writeType);
836
+ BluetoothGattService service = gatt.getService(serviceUUID);
837
+ BluetoothGattCharacteristic characteristic = findWritableCharacteristic(service, characteristicUUID, writeType);
697
838
 
698
- if (writeQueue.size() > 0) {
699
- callback.invoke("You have already an queued message");
700
- return;
701
- }
839
+ if (characteristic == null) {
840
+ callback.invoke("Characteristic " + characteristicUUID + " not found.");
841
+ return;
842
+ }
702
843
 
703
- if (writeCallback != null) {
704
- callback.invoke("You're already writing");
705
- return;
706
- }
844
+ characteristic.setWriteType(writeType);
707
845
 
708
- if (writeQueue.size() == 0 && writeCallback == null) {
846
+ if (data.length <= maxByteSize) {
847
+ if (! doWrite(characteristic, data, callback)) {
848
+ callback.invoke("Write failed");
849
+ writeCallback = null;
850
+ }
851
+ } else {
852
+ int dataLength = data.length;
853
+ int count = 0;
854
+ byte[] firstMessage = null;
855
+ List<byte[]> splittedMessage = new ArrayList<>();
856
+
857
+ while (count < dataLength && (dataLength - count > maxByteSize)) {
858
+ if (count == 0) {
859
+ firstMessage = Arrays.copyOfRange(data, count, count + maxByteSize);
860
+ } else {
861
+ byte[] splitMessage = Arrays.copyOfRange(data, count, count + maxByteSize);
862
+ splittedMessage.add(splitMessage);
863
+ }
864
+ count += maxByteSize;
865
+ }
866
+ if (count < dataLength) {
867
+ // Other bytes in queue
868
+ byte[] splitMessage = Arrays.copyOfRange(data, count, data.length);
869
+ splittedMessage.add(splitMessage);
870
+ }
709
871
 
710
- if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
711
- writeCallback = callback;
872
+ if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
873
+ writeQueue.addAll(splittedMessage);
874
+ if (! doWrite(characteristic, firstMessage, callback)) {
875
+ writeQueue.clear();
876
+ writeCallback = null;
877
+ callback.invoke("Write failed");
878
+ }
879
+ } else {
880
+ try {
881
+ boolean writeError = false;
882
+ if (! doWrite(characteristic, firstMessage, callback)) {
883
+ writeError = true;
884
+ callback.invoke("Write failed");
712
885
  }
713
-
714
- if (data.length > maxByteSize) {
715
- int dataLength = data.length;
716
- int count = 0;
717
- byte[] firstMessage = null;
718
- List<byte[]> splittedMessage = new ArrayList<>();
719
-
720
- while (count < dataLength && (dataLength - count > maxByteSize)) {
721
- if (count == 0) {
722
- firstMessage = Arrays.copyOfRange(data, count, count + maxByteSize);
723
- } else {
724
- byte[] splitMessage = Arrays.copyOfRange(data, count, count + maxByteSize);
725
- splittedMessage.add(splitMessage);
726
- }
727
- count += maxByteSize;
728
- }
729
- if (count < dataLength) {
730
- // Other bytes in queue
731
- byte[] splitMessage = Arrays.copyOfRange(data, count, data.length);
732
- splittedMessage.add(splitMessage);
733
- }
734
-
735
- if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
736
- writeQueue.addAll(splittedMessage);
737
- if (!doWrite(characteristic, firstMessage)) {
738
- writeQueue.clear();
739
- writeCallback = null;
886
+ if (! writeError) {
887
+ Thread.sleep(queueSleepTime);
888
+ for (byte[] message : splittedMessage) {
889
+ if (! doWrite(characteristic, message, callback)) {
890
+ writeError = true;
740
891
  callback.invoke("Write failed");
892
+ break;
741
893
  }
742
- } else {
743
- try {
744
- boolean writeError = false;
745
- if (!doWrite(characteristic, firstMessage)) {
746
- writeError = true;
747
- callback.invoke("Write failed");
748
- }
749
- if (!writeError) {
750
- Thread.sleep(queueSleepTime);
751
- for (byte[] message : splittedMessage) {
752
- if (!doWrite(characteristic, message)) {
753
- writeError = true;
754
- callback.invoke("Write failed");
755
- break;
756
- }
757
- Thread.sleep(queueSleepTime);
758
- }
759
- if (!writeError) {
760
- callback.invoke();
761
- }
762
- }
763
- } catch (InterruptedException e) {
764
- callback.invoke("Error during writing");
765
- }
894
+ Thread.sleep(queueSleepTime);
766
895
  }
767
- } else if (doWrite(characteristic, data)) {
768
- Log.d(BleManager.LOG_TAG, "Write completed");
769
- if (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE == writeType) {
896
+ if (! writeError) {
770
897
  callback.invoke();
771
898
  }
772
- } else {
773
- callback.invoke("Write failed");
774
- writeCallback = null;
775
899
  }
900
+ } catch (InterruptedException e) {
901
+ callback.invoke("Error during writing");
776
902
  }
777
903
  }
778
904
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ble-manager",
3
- "version": "7.6.3",
3
+ "version": "8.0.0",
4
4
  "description": "A BLE module for react native.",
5
5
  "main": "BleManager",
6
6
  "types": "./index.d.ts",
package/ISSUE_TEMPLATE.md DELETED
@@ -1,33 +0,0 @@
1
- ### Before open an issue
2
- - Check the closed issues, your question maybe is not new.
3
- - We can't debug your physical devices.
4
- - If the library is not working 99% you did something wrong, in the code or in the phone permissions.
5
-
6
- ### Version
7
-
8
- Tell us which versions you are using:
9
-
10
- - react-native-ble-manager v?.?.?
11
- - react-native v?.?
12
- - iOS/Android v.?
13
-
14
- ### Expected behaviour
15
-
16
- Tell us what should happen
17
-
18
- ### Actual behaviour
19
-
20
- Tell us what happens instead
21
-
22
- ### Steps to reproduce
23
-
24
- 1.
25
- 2.
26
- 3.
27
-
28
- ### Stack trace and console log
29
-
30
- Hint: it would help a lot if you enable the debugger ("Pause on exceptions" in the "Source" panel of Chrome dev tools) and spot the place where the error is thrown
31
-
32
- ```
33
- ```