react-native-ble-manager 8.1.0 → 8.4.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/BleManager.js CHANGED
@@ -410,6 +410,10 @@ class BleManager {
410
410
  });
411
411
  });
412
412
  }
413
+
414
+ setName(name) {
415
+ bleManager.setName(name);
416
+ }
413
417
  }
414
418
 
415
419
  module.exports = new BleManager();
package/README.md CHANGED
@@ -131,10 +131,12 @@ Returns a `Promise` object.
131
131
  - `seconds` - `Integer` - the amount of seconds to scan.
132
132
  - `allowDuplicates` - `Boolean` - [iOS only] allow duplicates in device scanning
133
133
  - `scanningOptions` - `JSON` - [Android only] after Android 5.0, user can control specific ble scan behaviors:
134
- - `numberOfMatches` - `Number` - corresponding to [`setNumOfMatches`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setNumOfMatches(int)>)
135
- - `matchMode` - `Number` - corresponding to [`setMatchMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setMatchMode(int)>)
136
- - `scanMode` - `Number` - corresponding to [`setScanMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setScanMode(int)>)
137
- - `reportDelay` - `Number` - corresponding to [`setReportDelay`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setReportDelay(long)>)
134
+ - `numberOfMatches` - `Number` - [Android only] corresponding to [`setNumOfMatches`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setNumOfMatches(int)>)
135
+ - `matchMode` - `Number` - [Android only] corresponding to [`setMatchMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setMatchMode(int)>)
136
+ - `scanMode` - `Number` - [Android only] corresponding to [`setScanMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setScanMode(int)>)
137
+ - `reportDelay` - `Number` - [Android only] corresponding to [`setReportDelay`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setReportDelay(long)>)
138
+ - `phy` - `Number` - [Android only] corresponding to [`setPhy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setPhy(int))
139
+ - `legacy` - `Boolean` - [Android only] corresponding to [`setLegacy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setLegacy(boolean))
138
140
 
139
141
  **Examples**
140
142
 
@@ -662,6 +664,25 @@ BleManager.isPeripheralConnected(
662
664
  });
663
665
  ```
664
666
 
667
+ ### setName(name) [Android only]
668
+
669
+ Create the request to set the name of the bluetooth adapter. (https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#setName(java.lang.String))
670
+ Returns a `Promise` object.
671
+
672
+ **Examples**
673
+
674
+ ```js
675
+ BleManager.setName("INNOVEIT_CENTRAL")
676
+ .then(() => {
677
+ // Success code
678
+ console.log("Name set successfully");
679
+ })
680
+ .catch((error) => {
681
+ // Failure code
682
+ console.log("Name could not be set");
683
+ });
684
+ ```
685
+
665
686
  ## Events
666
687
 
667
688
  ### BleManagerStopScan
@@ -481,6 +481,12 @@ class BleManager extends ReactContextBaseJavaModule {
481
481
  sendEvent("BleManagerDidUpdateState", map);
482
482
  }
483
483
 
484
+ @ReactMethod
485
+ public void setName(String name) {
486
+ BluetoothAdapter adapter = getBluetoothAdapter();
487
+ adapter.setName(name);
488
+ }
489
+
484
490
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
485
491
  @Override
486
492
  public void onReceive(Context context, Intent intent) {
@@ -732,4 +738,19 @@ class BleManager extends ReactContextBaseJavaModule {
732
738
  // Keep: Required for RN built in Event Emitter Calls.
733
739
  }
734
740
 
741
+ @Override
742
+ public void onCatalystInstanceDestroy() {
743
+ try {
744
+ // Disconnect all known peripherals, otherwise android system will think we are still connected
745
+ // while we have lost the gatt instance
746
+ disconnectPeripherals();
747
+ }catch(Exception e) {
748
+ Log.d(LOG_TAG, "Could not disconnect peripherals", e);
749
+ }
750
+
751
+ if (scanManager != null) {
752
+ // Stop scan in case one was started to stop events from being emitted after destroy
753
+ scanManager.stopScan(args -> {});
754
+ }
755
+ }
735
756
  }
@@ -41,7 +41,9 @@ public class LollipopPeripheral extends Peripheral {
41
41
 
42
42
  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
43
43
  // We can check if peripheral is connectable using the scanresult
44
- advertising.putBoolean("isConnectable", scanResult.isConnectable());
44
+ if (this.scanResult != null) {
45
+ advertising.putBoolean("isConnectable", scanResult.isConnectable());
46
+ }
45
47
  } else{
46
48
  // We can't check if peripheral is connectable
47
49
  advertising.putBoolean("isConnectable", true);
@@ -82,9 +84,9 @@ public class LollipopPeripheral extends Peripheral {
82
84
  return map;
83
85
  }
84
86
 
85
- public void updateData(ScanRecord scanRecord) {
86
- advertisingData = scanRecord;
87
- advertisingDataBytes = scanRecord.getBytes();
87
+ public void updateData(ScanResult result) {
88
+ advertisingData = result.getScanRecord();
89
+ advertisingDataBytes = advertisingData.getBytes();
88
90
  }
89
91
 
90
92
 
@@ -2,6 +2,7 @@ package it.innove;
2
2
 
3
3
 
4
4
  import android.bluetooth.BluetoothAdapter;
5
+ import android.bluetooth.BluetoothDevice;
5
6
  import android.bluetooth.le.ScanCallback;
6
7
  import android.bluetooth.le.ScanFilter;
7
8
  import android.bluetooth.le.ScanResult;
@@ -37,6 +38,10 @@ public class LollipopScanManager extends ScanManager {
37
38
  public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
38
39
  ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
39
40
  List<ScanFilter> filters = new ArrayList<>();
41
+
42
+ if (options.hasKey("legacy")) {
43
+ scanSettingsBuilder.setLegacy(options.getBoolean("legacy"));
44
+ }
40
45
 
41
46
  if (options.hasKey("scanMode")) {
42
47
  scanSettingsBuilder.setScanMode(options.getInt("scanMode"));
@@ -54,6 +59,16 @@ public class LollipopScanManager extends ScanManager {
54
59
  if (options.hasKey("reportDelay")) {
55
60
  scanSettingsBuilder.setReportDelay(options.getInt("reportDelay"));
56
61
  }
62
+
63
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.hasKey("phy")) {
64
+ int phy = options.getInt("phy");
65
+ if (phy == BluetoothDevice.PHY_LE_CODED && getBluetoothAdapter().isLeCodedPhySupported()) {
66
+ scanSettingsBuilder.setPhy(BluetoothDevice.PHY_LE_CODED);
67
+ }
68
+ if (phy == BluetoothDevice.PHY_LE_2M && getBluetoothAdapter().isLe2MPhySupported()) {
69
+ scanSettingsBuilder.setPhy(BluetoothDevice.PHY_LE_2M);
70
+ }
71
+ }
57
72
 
58
73
  if (serviceUUIDs.size() > 0) {
59
74
  for(int i = 0; i < serviceUUIDs.size(); i++){
@@ -112,7 +127,7 @@ public class LollipopScanManager extends ScanManager {
112
127
  if (peripheral == null) {
113
128
  peripheral = new LollipopPeripheral(bleManager.getReactContext(), result);
114
129
  } else {
115
- peripheral.updateData(result.getScanRecord());
130
+ peripheral.updateData(result);
116
131
  peripheral.updateRssi(result.getRssi());
117
132
  }
118
133
  bleManager.savePeripheral(peripheral);
@@ -3,26 +3,33 @@ package it.innove;
3
3
  import java.nio.ByteBuffer;
4
4
 
5
5
  public class NotifyBufferContainer {
6
- public final String key;
7
- public final Integer maxCount;
6
+ public final Integer maxBufferSize;
8
7
  private Integer bufferCount;
9
8
  public ByteBuffer items;
10
9
 
11
- public NotifyBufferContainer(String key, Integer count) {
12
-
13
- this.key = key;
14
- this.maxCount = count;
10
+ public NotifyBufferContainer(Integer size) {
11
+ this.maxBufferSize = size;
15
12
  this.resetBuffer();
16
13
  }
17
14
  public void resetBuffer(){
18
- this.bufferCount =0;
19
- this.items = ByteBuffer.wrap(new byte[this.maxCount * 20]);
15
+ this.bufferCount = 0;
16
+ this.items = ByteBuffer.allocate(this.maxBufferSize);
20
17
  }
21
18
  public void put(byte[] value){
22
- this.bufferCount +=1;
19
+ this.bufferCount += value.length;
20
+ if (this.items.remaining() < value.length) {
21
+ return;
22
+ }
23
23
  this.items.put(value);
24
24
  }
25
- public Integer size(){
25
+ public boolean isBufferFull(){
26
+ return this.bufferCount >= this.maxBufferSize;
27
+ }
28
+ public Integer size() {
26
29
  return this.bufferCount;
27
30
  }
31
+ @Override
32
+ protected void finalize() throws Throwable {
33
+ this.items = ByteBuffer.allocate(0);
34
+ }
28
35
  }
@@ -378,10 +378,10 @@ public class Peripheral extends BluetoothGattCallback {
378
378
  byte[] dataValue = characteristic.getValue();
379
379
  if (buffer != null) {
380
380
  buffer.put(dataValue);
381
- // Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
382
- // buffer.size() + " from peripheral: " + device.getAddress());
381
+ Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
382
+ buffer.size() + " from peripheral: " + device.getAddress());
383
383
 
384
- if (buffer.size().equals(buffer.maxCount)) {
384
+ if (buffer.isBufferFull()) {
385
385
  Log.d(BleManager.LOG_TAG, "onCharacteristicChanged sending buffered data " + buffer.size());
386
386
 
387
387
  // send'm and reset
@@ -581,11 +581,21 @@ public class Peripheral extends BluetoothGattCallback {
581
581
 
582
582
  public void registerNotify(UUID serviceUUID, UUID characteristicUUID, Integer buffer, Callback callback) {
583
583
  Log.d(BleManager.LOG_TAG, "registerNotify");
584
+ if (buffer > 1) {
585
+ Log.d(BleManager.LOG_TAG, "registerNotify using buffer");
586
+ String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
587
+ this.bufferedCharacteristics.put(bufferKey, new NotifyBufferContainer(buffer));
588
+ }
584
589
  this.setNotify(serviceUUID, characteristicUUID, true, callback);
585
590
  }
586
591
 
587
592
  public void removeNotify(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
588
593
  Log.d(BleManager.LOG_TAG, "removeNotify");
594
+ String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
595
+ if (this.bufferedCharacteristics.containsKey(bufferKey)) {
596
+ NotifyBufferContainer buffer = this.bufferedCharacteristics.get(bufferKey);
597
+ this.bufferedCharacteristics.remove(bufferKey);
598
+ }
589
599
  this.setNotify(serviceUUID, characteristicUUID, false, callback);
590
600
  }
591
601
 
package/index.d.ts CHANGED
@@ -28,6 +28,8 @@ declare module "react-native-ble-manager" {
28
28
  matchMode?: number;
29
29
  scanMode?: number;
30
30
  reportDelay?: number;
31
+ phy?: number;
32
+ legacy?: boolean;
31
33
  }
32
34
 
33
35
  export function scan(
@@ -110,7 +112,7 @@ declare module "react-native-ble-manager" {
110
112
  // [Android only]
111
113
  export function refreshCache(peripheralID: string): Promise<void>;
112
114
  // [Android only API 21+]
113
- export function requestMTU(peripheralID: string, mtu: number): Promise<void>;
115
+ export function requestMTU(peripheralID: string, mtu: number): Promise<number>;
114
116
 
115
117
  export function createBond(
116
118
  peripheralID: string,
@@ -120,7 +122,9 @@ declare module "react-native-ble-manager" {
120
122
  export function getBondedPeripherals(): Promise<Peripheral[]>;
121
123
  export function removePeripheral(peripheralID: string): Promise<void>;
122
124
 
123
-
125
+ // [Android only]
126
+ export function setName(name: string): void;
127
+
124
128
  export interface Service {
125
129
  uuid: string;
126
130
  }
@@ -147,7 +151,7 @@ declare module "react-native-ble-manager" {
147
151
  characteristic: string;
148
152
  service: string;
149
153
  descriptors?: Descriptor[];
150
-
154
+
151
155
  }
152
156
 
153
157
  export interface PeripheralInfo extends Peripheral {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ble-manager",
3
- "version": "8.1.0",
3
+ "version": "8.4.0",
4
4
  "description": "A BLE module for react native.",
5
5
  "main": "BleManager",
6
6
  "types": "./index.d.ts",