react-native-ble-manager 8.3.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
@@ -136,6 +136,7 @@ Returns a `Promise` object.
136
136
  - `scanMode` - `Number` - [Android only] corresponding to [`setScanMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setScanMode(int)>)
137
137
  - `reportDelay` - `Number` - [Android only] corresponding to [`setReportDelay`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setReportDelay(long)>)
138
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))
139
140
 
140
141
  **Examples**
141
142
 
@@ -663,6 +664,25 @@ BleManager.isPeripheralConnected(
663
664
  });
664
665
  ```
665
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
+
666
686
  ## Events
667
687
 
668
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) {
@@ -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
 
@@ -38,6 +38,10 @@ public class LollipopScanManager extends ScanManager {
38
38
  public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
39
39
  ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
40
40
  List<ScanFilter> filters = new ArrayList<>();
41
+
42
+ if (options.hasKey("legacy")) {
43
+ scanSettingsBuilder.setLegacy(options.getBoolean("legacy"));
44
+ }
41
45
 
42
46
  if (options.hasKey("scanMode")) {
43
47
  scanSettingsBuilder.setScanMode(options.getInt("scanMode"));
@@ -123,7 +127,7 @@ public class LollipopScanManager extends ScanManager {
123
127
  if (peripheral == null) {
124
128
  peripheral = new LollipopPeripheral(bleManager.getReactContext(), result);
125
129
  } else {
126
- peripheral.updateData(result.getScanRecord());
130
+ peripheral.updateData(result);
127
131
  peripheral.updateRssi(result.getRssi());
128
132
  }
129
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
@@ -29,6 +29,7 @@ declare module "react-native-ble-manager" {
29
29
  scanMode?: number;
30
30
  reportDelay?: number;
31
31
  phy?: number;
32
+ legacy?: boolean;
32
33
  }
33
34
 
34
35
  export function scan(
@@ -111,7 +112,7 @@ declare module "react-native-ble-manager" {
111
112
  // [Android only]
112
113
  export function refreshCache(peripheralID: string): Promise<void>;
113
114
  // [Android only API 21+]
114
- export function requestMTU(peripheralID: string, mtu: number): Promise<void>;
115
+ export function requestMTU(peripheralID: string, mtu: number): Promise<number>;
115
116
 
116
117
  export function createBond(
117
118
  peripheralID: string,
@@ -121,7 +122,9 @@ declare module "react-native-ble-manager" {
121
122
  export function getBondedPeripherals(): Promise<Peripheral[]>;
122
123
  export function removePeripheral(peripheralID: string): Promise<void>;
123
124
 
124
-
125
+ // [Android only]
126
+ export function setName(name: string): void;
127
+
125
128
  export interface Service {
126
129
  uuid: string;
127
130
  }
@@ -148,7 +151,7 @@ declare module "react-native-ble-manager" {
148
151
  characteristic: string;
149
152
  service: string;
150
153
  descriptors?: Descriptor[];
151
-
154
+
152
155
  }
153
156
 
154
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.3.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",