react-native-ble-manager 8.3.0 → 8.4.3
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 +21 -1
- package/android/build.gradle +5 -0
- 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 +5 -1
- package/android/src/main/java/it/innove/NotifyBufferContainer.java +17 -10
- package/android/src/main/java/it/innove/Peripheral.java +21 -6
- package/index.d.ts +6 -3
- package/package.json +1 -1
package/BleManager.js
CHANGED
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
|
|
@@ -757,7 +777,7 @@ async function connectAndPrepare(peripheral, service, characteristic) {
|
|
|
757
777
|
({ value, peripheral, characteristic, service }) => {
|
|
758
778
|
// Convert bytes array to string
|
|
759
779
|
const data = bytesToString(value);
|
|
760
|
-
console.log(`
|
|
780
|
+
console.log(`Received ${data} for characteristic ${characteristic}`);
|
|
761
781
|
}
|
|
762
782
|
);
|
|
763
783
|
// Actions triggereng BleManagerDidUpdateValueForCharacteristic event
|
package/android/build.gradle
CHANGED
|
@@ -21,6 +21,11 @@ def safeExtGet(prop, fallback) {
|
|
|
21
21
|
|
|
22
22
|
android {
|
|
23
23
|
compileSdkVersion safeExtGet("compileSdkVersion", 30)
|
|
24
|
+
|
|
25
|
+
compileOptions {
|
|
26
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
27
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
28
|
+
}
|
|
24
29
|
|
|
25
30
|
defaultConfig {
|
|
26
31
|
minSdkVersion safeExtGet("minSdkVersion", 21)
|
|
@@ -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
|
|
|
@@ -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
|
|
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
|
|
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
|
}
|
|
@@ -331,8 +331,11 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
331
331
|
readCallback, registerNotifyCallback, requestMTUCallback);
|
|
332
332
|
for (Callback currentCallback : callbacks) {
|
|
333
333
|
if (currentCallback != null) {
|
|
334
|
-
|
|
335
|
-
|
|
334
|
+
try {
|
|
335
|
+
currentCallback.invoke("Device disconnected");
|
|
336
|
+
} catch (Exception e) {
|
|
337
|
+
e.printStackTrace();
|
|
338
|
+
} }
|
|
336
339
|
}
|
|
337
340
|
if (connectCallback != null) {
|
|
338
341
|
connectCallback.invoke("Connection error");
|
|
@@ -378,10 +381,10 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
378
381
|
byte[] dataValue = characteristic.getValue();
|
|
379
382
|
if (buffer != null) {
|
|
380
383
|
buffer.put(dataValue);
|
|
381
|
-
|
|
382
|
-
|
|
384
|
+
Log.d(BleManager.LOG_TAG, "onCharacteristicChanged-buffering: " +
|
|
385
|
+
buffer.size() + " from peripheral: " + device.getAddress());
|
|
383
386
|
|
|
384
|
-
if (buffer.
|
|
387
|
+
if (buffer.isBufferFull()) {
|
|
385
388
|
Log.d(BleManager.LOG_TAG, "onCharacteristicChanged sending buffered data " + buffer.size());
|
|
386
389
|
|
|
387
390
|
// send'm and reset
|
|
@@ -492,6 +495,8 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
492
495
|
|
|
493
496
|
readRSSICallback = null;
|
|
494
497
|
}
|
|
498
|
+
|
|
499
|
+
completedCommand();
|
|
495
500
|
}
|
|
496
501
|
|
|
497
502
|
private String bufferedCharacteristicsKey(String serviceUUID, String characteristicUUID) {
|
|
@@ -581,11 +586,21 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
581
586
|
|
|
582
587
|
public void registerNotify(UUID serviceUUID, UUID characteristicUUID, Integer buffer, Callback callback) {
|
|
583
588
|
Log.d(BleManager.LOG_TAG, "registerNotify");
|
|
589
|
+
if (buffer > 1) {
|
|
590
|
+
Log.d(BleManager.LOG_TAG, "registerNotify using buffer");
|
|
591
|
+
String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
|
|
592
|
+
this.bufferedCharacteristics.put(bufferKey, new NotifyBufferContainer(buffer));
|
|
593
|
+
}
|
|
584
594
|
this.setNotify(serviceUUID, characteristicUUID, true, callback);
|
|
585
595
|
}
|
|
586
596
|
|
|
587
597
|
public void removeNotify(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
|
|
588
598
|
Log.d(BleManager.LOG_TAG, "removeNotify");
|
|
599
|
+
String bufferKey = this.bufferedCharacteristicsKey(serviceUUID.toString(), characteristicUUID.toString());
|
|
600
|
+
if (this.bufferedCharacteristics.containsKey(bufferKey)) {
|
|
601
|
+
NotifyBufferContainer buffer = this.bufferedCharacteristics.get(bufferKey);
|
|
602
|
+
this.bufferedCharacteristics.remove(bufferKey);
|
|
603
|
+
}
|
|
589
604
|
this.setNotify(serviceUUID, characteristicUUID, false, callback);
|
|
590
605
|
}
|
|
591
606
|
|
|
@@ -735,7 +750,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
735
750
|
if (isConnected()) {
|
|
736
751
|
readRSSICallback = callback;
|
|
737
752
|
if (! gatt.readRemoteRssi()) {
|
|
738
|
-
|
|
753
|
+
readRSSICallback = null;
|
|
739
754
|
sendBackrError(callback, "Read RSSI failed");
|
|
740
755
|
completedCommand();
|
|
741
756
|
}
|
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<
|
|
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 {
|