react-native-ble-manager 8.2.0 → 8.4.1
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 +4 -0
- package/README.md +26 -5
- package/android/src/main/java/it/innove/BleManager.java +6 -0
- package/android/src/main/java/it/innove/LollipopPeripheral.java +6 -4
- package/android/src/main/java/it/innove/LollipopScanManager.java +16 -1
- package/android/src/main/java/it/innove/NotifyBufferContainer.java +17 -10
- package/android/src/main/java/it/innove/Peripheral.java +16 -4
- package/index.d.ts +7 -3
- package/package.json +1 -1
package/BleManager.js
CHANGED
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
|
|
@@ -756,7 +777,7 @@ async function connectAndPrepare(peripheral, service, characteristic) {
|
|
|
756
777
|
({ value, peripheral, characteristic, service }) => {
|
|
757
778
|
// Convert bytes array to string
|
|
758
779
|
const data = bytesToString(value);
|
|
759
|
-
console.log(`
|
|
780
|
+
console.log(`Received ${data} for characteristic ${characteristic}`);
|
|
760
781
|
}
|
|
761
782
|
);
|
|
762
783
|
// Actions triggereng BleManagerDidUpdateValueForCharacteristic event
|
|
@@ -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
|
-
|
|
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(
|
|
86
|
-
advertisingData =
|
|
87
|
-
advertisingDataBytes =
|
|
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
|
|
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
|
|
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(
|
|
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.
|
|
15
|
+
this.bufferCount = 0;
|
|
16
|
+
this.items = ByteBuffer.allocate(this.maxBufferSize);
|
|
20
17
|
}
|
|
21
18
|
public void put(byte[] value){
|
|
22
|
-
this.bufferCount +=
|
|
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
|
|
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
|
-
|
|
382
|
-
|
|
381
|
+
Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
|
|
382
|
+
buffer.size() + " from peripheral: " + device.getAddress());
|
|
383
383
|
|
|
384
|
-
if (buffer.
|
|
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
|
|
@@ -492,6 +492,8 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
492
492
|
|
|
493
493
|
readRSSICallback = null;
|
|
494
494
|
}
|
|
495
|
+
|
|
496
|
+
completedCommand();
|
|
495
497
|
}
|
|
496
498
|
|
|
497
499
|
private String bufferedCharacteristicsKey(String serviceUUID, String characteristicUUID) {
|
|
@@ -581,11 +583,21 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
581
583
|
|
|
582
584
|
public void registerNotify(UUID serviceUUID, UUID characteristicUUID, Integer buffer, Callback callback) {
|
|
583
585
|
Log.d(BleManager.LOG_TAG, "registerNotify");
|
|
586
|
+
if (buffer > 1) {
|
|
587
|
+
Log.d(BleManager.LOG_TAG, "registerNotify using buffer");
|
|
588
|
+
String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
|
|
589
|
+
this.bufferedCharacteristics.put(bufferKey, new NotifyBufferContainer(buffer));
|
|
590
|
+
}
|
|
584
591
|
this.setNotify(serviceUUID, characteristicUUID, true, callback);
|
|
585
592
|
}
|
|
586
593
|
|
|
587
594
|
public void removeNotify(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
|
|
588
595
|
Log.d(BleManager.LOG_TAG, "removeNotify");
|
|
596
|
+
String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
|
|
597
|
+
if (this.bufferedCharacteristics.containsKey(bufferKey)) {
|
|
598
|
+
NotifyBufferContainer buffer = this.bufferedCharacteristics.get(bufferKey);
|
|
599
|
+
this.bufferedCharacteristics.remove(bufferKey);
|
|
600
|
+
}
|
|
589
601
|
this.setNotify(serviceUUID, characteristicUUID, false, callback);
|
|
590
602
|
}
|
|
591
603
|
|
|
@@ -735,7 +747,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
735
747
|
if (isConnected()) {
|
|
736
748
|
readRSSICallback = callback;
|
|
737
749
|
if (! gatt.readRemoteRssi()) {
|
|
738
|
-
|
|
750
|
+
readRSSICallback = null;
|
|
739
751
|
sendBackrError(callback, "Read RSSI failed");
|
|
740
752
|
completedCommand();
|
|
741
753
|
}
|
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<
|
|
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 {
|