react-native-ble-manager 12.2.2 → 12.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/android/src/main/java/it/innove/BleManager.java +10 -4
- package/android/src/main/java/it/innove/DefaultPeripheral.java +14 -9
- package/android/src/main/java/it/innove/DefaultScanManager.java +174 -28
- package/android/src/main/java/it/innove/Peripheral.java +121 -80
- package/android/src/main/java/it/innove/ScanManager.java +1 -1
- package/dist/cjs/NativeBleManager.d.ts +27 -26
- package/dist/cjs/NativeBleManager.js +1 -0
- package/dist/cjs/NativeBleManager.js.map +1 -1
- package/dist/cjs/index.d.ts +6 -1
- package/dist/cjs/index.js +26 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types.d.ts +10 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/NativeBleManager.d.ts +27 -26
- package/dist/esm/NativeBleManager.js +1 -0
- package/dist/esm/NativeBleManager.js.map +1 -1
- package/dist/esm/index.d.ts +6 -1
- package/dist/esm/index.js +27 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +10 -0
- package/dist/esm/types.js.map +1 -1
- package/ios/BleManager.mm +7 -11
- package/ios/Helper.swift +166 -110
- package/ios/NotifyBufferContainer.swift +11 -12
- package/ios/SwiftBleManager.swift +1224 -586
- package/package.json +12 -18
- package/src/NativeBleManager.ts +42 -82
- package/src/index.ts +33 -24
- package/src/types.ts +10 -0
|
@@ -2,6 +2,7 @@ package it.innove;
|
|
|
2
2
|
|
|
3
3
|
import static com.facebook.react.common.ReactConstants.TAG;
|
|
4
4
|
|
|
5
|
+
import android.Manifest;
|
|
5
6
|
import android.annotation.SuppressLint;
|
|
6
7
|
import android.app.Activity;
|
|
7
8
|
import android.bluetooth.BluetoothDevice;
|
|
@@ -12,7 +13,7 @@ import android.bluetooth.BluetoothGattDescriptor;
|
|
|
12
13
|
import android.bluetooth.BluetoothGattService;
|
|
13
14
|
import android.bluetooth.BluetoothProfile;
|
|
14
15
|
import android.bluetooth.BluetoothStatusCodes;
|
|
15
|
-
|
|
16
|
+
import android.content.pm.PackageManager;
|
|
16
17
|
import android.content.Context;
|
|
17
18
|
import android.os.Build;
|
|
18
19
|
import android.os.Handler;
|
|
@@ -22,6 +23,7 @@ import android.util.Log;
|
|
|
22
23
|
|
|
23
24
|
import androidx.annotation.NonNull;
|
|
24
25
|
import androidx.annotation.Nullable;
|
|
26
|
+
import androidx.core.content.ContextCompat;
|
|
25
27
|
|
|
26
28
|
import com.facebook.react.bridge.Arguments;
|
|
27
29
|
import com.facebook.react.bridge.Callback;
|
|
@@ -79,7 +81,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
79
81
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
80
82
|
private boolean commandQueueBusy = false;
|
|
81
83
|
|
|
82
|
-
private
|
|
84
|
+
private final Queue<byte[]> writeQueue = new LinkedList<>();
|
|
83
85
|
|
|
84
86
|
public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, BleManager bleManager) {
|
|
85
87
|
this.device = device;
|
|
@@ -160,10 +162,10 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
160
162
|
// bt_btif : Register with GATT stack failed.
|
|
161
163
|
|
|
162
164
|
public void disconnect(final Callback callback, final boolean force) {
|
|
165
|
+
connected = false;
|
|
163
166
|
mainHandler.post(() -> {
|
|
164
167
|
errorAndClearAllCallbacks("Disconnect called before the command completed");
|
|
165
168
|
resetQueuesAndBuffers();
|
|
166
|
-
connected = false;
|
|
167
169
|
|
|
168
170
|
if (gatt != null) {
|
|
169
171
|
try {
|
|
@@ -190,11 +192,11 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
190
192
|
WritableMap advertising = Arguments.createMap();
|
|
191
193
|
|
|
192
194
|
try {
|
|
193
|
-
|
|
195
|
+
String name = getSafeDeviceName();
|
|
196
|
+
map.putString("name", name);
|
|
194
197
|
map.putString("id", device.getAddress()); // mac address
|
|
195
198
|
map.putInt("rssi", advertisingRSSI);
|
|
196
199
|
|
|
197
|
-
String name = device.getName();
|
|
198
200
|
if (name != null)
|
|
199
201
|
advertising.putString("localName", name);
|
|
200
202
|
|
|
@@ -211,6 +213,23 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
211
213
|
return map;
|
|
212
214
|
}
|
|
213
215
|
|
|
216
|
+
@Nullable
|
|
217
|
+
protected String getSafeDeviceName() {
|
|
218
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
219
|
+
Context context = bleManager != null ? bleManager.getReactContext() : null;
|
|
220
|
+
if (context == null) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT)
|
|
225
|
+
!= PackageManager.PERMISSION_GRANTED) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return device.getName();
|
|
231
|
+
}
|
|
232
|
+
|
|
214
233
|
public WritableMap asWritableMap(BluetoothGatt gatt) {
|
|
215
234
|
|
|
216
235
|
WritableMap map = asWritableMap();
|
|
@@ -219,13 +238,13 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
219
238
|
WritableArray characteristicsArray = Arguments.createArray();
|
|
220
239
|
|
|
221
240
|
if (connected && gatt != null) {
|
|
222
|
-
for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext();) {
|
|
241
|
+
for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext(); ) {
|
|
223
242
|
BluetoothGattService service = it.next();
|
|
224
243
|
WritableMap serviceMap = Arguments.createMap();
|
|
225
244
|
serviceMap.putString("uuid", UUIDHelper.uuidToString(service.getUuid()));
|
|
226
245
|
|
|
227
246
|
for (Iterator<BluetoothGattCharacteristic> itCharacteristic = service.getCharacteristics()
|
|
228
|
-
.iterator(); itCharacteristic.hasNext();) {
|
|
247
|
+
.iterator(); itCharacteristic.hasNext(); ) {
|
|
229
248
|
BluetoothGattCharacteristic characteristic = itCharacteristic.next();
|
|
230
249
|
WritableMap characteristicsMap = Arguments.createMap();
|
|
231
250
|
|
|
@@ -297,15 +316,12 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
297
316
|
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
298
317
|
retrieveServicesCallback.invoke("Error during service retrieval: gatt is null");
|
|
299
318
|
}
|
|
300
|
-
}
|
|
301
|
-
else if (status == BluetoothGatt.GATT_SUCCESS)
|
|
302
|
-
{
|
|
319
|
+
} else if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
303
320
|
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
304
321
|
WritableMap map = this.asWritableMap(gatt);
|
|
305
322
|
retrieveServicesCallback.invoke(null, map);
|
|
306
323
|
}
|
|
307
|
-
}
|
|
308
|
-
else {
|
|
324
|
+
} else {
|
|
309
325
|
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
310
326
|
retrieveServicesCallback.invoke("Error during service retrieval.");
|
|
311
327
|
}
|
|
@@ -370,12 +386,20 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
370
386
|
connected = false;
|
|
371
387
|
clearBuffers();
|
|
372
388
|
}
|
|
389
|
+
|
|
373
390
|
@Override
|
|
374
391
|
public void onConnectionStateChange(BluetoothGatt gatta, int status, final int newState) {
|
|
375
392
|
|
|
376
393
|
Log.d(BleManager.LOG_TAG, "onConnectionStateChange to " + newState + " on peripheral: " + device.getAddress()
|
|
377
394
|
+ " with status " + status);
|
|
378
395
|
|
|
396
|
+
// We immediately update the internal connection status
|
|
397
|
+
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
|
|
398
|
+
connected = true;
|
|
399
|
+
} else if (newState == BluetoothProfile.STATE_DISCONNECTED || status != BluetoothGatt.GATT_SUCCESS) {
|
|
400
|
+
connected = false;
|
|
401
|
+
}
|
|
402
|
+
|
|
379
403
|
mainHandler.post(() -> {
|
|
380
404
|
gatt = gatta;
|
|
381
405
|
|
|
@@ -385,8 +409,6 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
385
409
|
|
|
386
410
|
connecting = false;
|
|
387
411
|
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
|
|
388
|
-
connected = true;
|
|
389
|
-
|
|
390
412
|
sendConnectionEvent(device, status);
|
|
391
413
|
|
|
392
414
|
Log.d(BleManager.LOG_TAG, "Connected to: " + device.getAddress());
|
|
@@ -420,7 +442,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
420
442
|
advertisingDataBytes = data;
|
|
421
443
|
}
|
|
422
444
|
|
|
423
|
-
|
|
445
|
+
/// ///
|
|
424
446
|
|
|
425
447
|
@Override
|
|
426
448
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
@@ -456,7 +478,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
456
478
|
return;
|
|
457
479
|
}
|
|
458
480
|
}
|
|
459
|
-
|
|
481
|
+
|
|
460
482
|
WritableMap map = Arguments.createMap();
|
|
461
483
|
map.putString("peripheral", device.getAddress());
|
|
462
484
|
map.putString("characteristic", charString);
|
|
@@ -480,6 +502,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
480
502
|
onCharacteristicRead(gatt, characteristic, characteristic.getValue(), status);
|
|
481
503
|
}
|
|
482
504
|
}
|
|
505
|
+
|
|
483
506
|
@Override
|
|
484
507
|
public void onCharacteristicRead(@NonNull final BluetoothGatt gatt,
|
|
485
508
|
@NonNull final BluetoothGattCharacteristic characteristic,
|
|
@@ -517,27 +540,28 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
517
540
|
super.onCharacteristicWrite(gatt, characteristic, status);
|
|
518
541
|
|
|
519
542
|
mainHandler.post(() -> {
|
|
520
|
-
if (writeQueue.
|
|
521
|
-
byte[] data = writeQueue.
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
543
|
+
if (!writeQueue.isEmpty()) {
|
|
544
|
+
byte[] data = writeQueue.poll();
|
|
545
|
+
doWrite(characteristic, data);
|
|
546
|
+
} else {
|
|
547
|
+
if (status != BluetoothGatt.GATT_SUCCESS) {
|
|
548
|
+
if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
|
|
549
|
+
Log.d(BleManager.LOG_TAG, "Write needs bonding");
|
|
550
|
+
// *not* doing completedCommand()
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
for (Callback writeCallback : writeCallbacks) {
|
|
554
|
+
writeCallback.invoke("Error writing " + characteristic.getUuid() + " status=" + status, null);
|
|
555
|
+
}
|
|
556
|
+
writeCallbacks.clear();
|
|
557
|
+
} else if (!writeCallbacks.isEmpty()) {
|
|
558
|
+
for (Callback writeCallback : writeCallbacks) {
|
|
559
|
+
writeCallback.invoke();
|
|
560
|
+
}
|
|
561
|
+
writeCallbacks.clear();
|
|
537
562
|
}
|
|
538
|
-
|
|
563
|
+
completedCommand();
|
|
539
564
|
}
|
|
540
|
-
completedCommand();
|
|
541
565
|
});
|
|
542
566
|
}
|
|
543
567
|
|
|
@@ -699,7 +723,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
699
723
|
// Then write to descriptor
|
|
700
724
|
registerNotifyCallbacks.addLast(callback);
|
|
701
725
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
702
|
-
result &=
|
|
726
|
+
result &= BluetoothStatusCodes.SUCCESS == gatt.writeDescriptor(descriptor, finalValue);
|
|
703
727
|
} else {
|
|
704
728
|
descriptor.setValue(finalValue);
|
|
705
729
|
result &= gatt.writeDescriptor(descriptor);
|
|
@@ -752,7 +776,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
752
776
|
// until we find the best match
|
|
753
777
|
// This function prefers Notify over Indicate
|
|
754
778
|
private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service,
|
|
755
|
-
|
|
779
|
+
UUID characteristicUUID) {
|
|
756
780
|
|
|
757
781
|
try {
|
|
758
782
|
// Check for Notify first
|
|
@@ -810,7 +834,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
810
834
|
}
|
|
811
835
|
|
|
812
836
|
public void readDescriptor(UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID,
|
|
813
|
-
|
|
837
|
+
final Callback callback) {
|
|
814
838
|
enqueue(() -> {
|
|
815
839
|
if (!isConnected() || gatt == null) {
|
|
816
840
|
callback.invoke("Device is not connected", null);
|
|
@@ -893,9 +917,8 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
893
917
|
writeCallback.invoke("writeDescriptor failed for descriptor: " + descriptor.getUuid(), null);
|
|
894
918
|
}
|
|
895
919
|
writeDescriptorCallbacks.clear();
|
|
920
|
+
completedCommand();
|
|
896
921
|
}
|
|
897
|
-
|
|
898
|
-
completedCommand();
|
|
899
922
|
});
|
|
900
923
|
}
|
|
901
924
|
|
|
@@ -939,14 +962,6 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
939
962
|
return;
|
|
940
963
|
}
|
|
941
964
|
|
|
942
|
-
// Check if we still have a valid gatt object
|
|
943
|
-
if (gatt == null) {
|
|
944
|
-
Log.d(BleManager.LOG_TAG, "Error, gatt is null. Fill all callbacks with an error");
|
|
945
|
-
errorAndClearAllCallbacks("Gatt is null");
|
|
946
|
-
resetQueuesAndBuffers();
|
|
947
|
-
return;
|
|
948
|
-
}
|
|
949
|
-
|
|
950
965
|
// Execute the next command in the queue
|
|
951
966
|
commandQueueBusy = true;
|
|
952
967
|
mainHandler.post(new Runnable() {
|
|
@@ -1019,7 +1034,12 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1019
1034
|
return;
|
|
1020
1035
|
} else {
|
|
1021
1036
|
this.retrieveServicesCallbacks.addLast(callback);
|
|
1022
|
-
gatt.discoverServices();
|
|
1037
|
+
boolean started = gatt.discoverServices();
|
|
1038
|
+
if (!started) {
|
|
1039
|
+
this.retrieveServicesCallbacks.removeLastOccurrence(callback);
|
|
1040
|
+
callback.invoke("Failed to start service discovery", null);
|
|
1041
|
+
completedCommand();
|
|
1042
|
+
}
|
|
1023
1043
|
}
|
|
1024
1044
|
});
|
|
1025
1045
|
}
|
|
@@ -1029,7 +1049,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1029
1049
|
// and UUID of all characteristics instead of using
|
|
1030
1050
|
// service.getCharacteristic(characteristicUUID)
|
|
1031
1051
|
private BluetoothGattCharacteristic findReadableCharacteristic(BluetoothGattService service,
|
|
1032
|
-
|
|
1052
|
+
UUID characteristicUUID) {
|
|
1033
1053
|
|
|
1034
1054
|
if (service != null) {
|
|
1035
1055
|
int read = BluetoothGattCharacteristic.PROPERTY_READ;
|
|
@@ -1051,7 +1071,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1051
1071
|
}
|
|
1052
1072
|
|
|
1053
1073
|
private BluetoothGattCharacteristic findCharacteristic(BluetoothGattService service,
|
|
1054
|
-
|
|
1074
|
+
UUID characteristicUUID) {
|
|
1055
1075
|
|
|
1056
1076
|
if (service != null) {
|
|
1057
1077
|
return service.getCharacteristic(characteristicUUID);
|
|
@@ -1060,30 +1080,39 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1060
1080
|
return null;
|
|
1061
1081
|
}
|
|
1062
1082
|
|
|
1063
|
-
|
|
1083
|
+
private void doWrite(final BluetoothGattCharacteristic characteristic, final byte[] data) {
|
|
1084
|
+
characteristic.setValue(data);
|
|
1085
|
+
if (!gatt.writeCharacteristic(characteristic)) {
|
|
1086
|
+
// write without response, caller will handle the callback
|
|
1087
|
+
for (Callback writeCallback : writeCallbacks) {
|
|
1088
|
+
writeCallback.invoke("Write failed", null);
|
|
1089
|
+
}
|
|
1090
|
+
writeCallbacks.clear();
|
|
1091
|
+
completedCommand();
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
private boolean enqueueWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
|
|
1064
1096
|
final byte[] copyOfData = copyOf(data);
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
// write without response, caller will handle the callback
|
|
1075
|
-
for (Callback writeCallback : writeCallbacks) {
|
|
1076
|
-
writeCallback.invoke("Write failed", null);
|
|
1077
|
-
}
|
|
1078
|
-
writeCallbacks.clear();
|
|
1079
|
-
completedCommand();
|
|
1097
|
+
final boolean withResponse = characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT;
|
|
1098
|
+
if (withResponse && callback != null) {
|
|
1099
|
+
writeCallbacks.addLast(callback);
|
|
1100
|
+
}
|
|
1101
|
+
return enqueue(() -> {
|
|
1102
|
+
if (!isConnected() || gatt == null) {
|
|
1103
|
+
if (withResponse && callback != null) {
|
|
1104
|
+
writeCallbacks.removeLastOccurrence(callback);
|
|
1105
|
+
callback.invoke("Device is not connected", null);
|
|
1080
1106
|
}
|
|
1107
|
+
completedCommand();
|
|
1108
|
+
return;
|
|
1081
1109
|
}
|
|
1110
|
+
doWrite(characteristic, copyOfData);
|
|
1082
1111
|
});
|
|
1083
1112
|
}
|
|
1084
1113
|
|
|
1085
1114
|
public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Integer maxByteSize,
|
|
1086
|
-
|
|
1115
|
+
Integer queueSleepTime, Callback callback, int writeType) {
|
|
1087
1116
|
enqueue(() -> {
|
|
1088
1117
|
if (!isConnected() || gatt == null) {
|
|
1089
1118
|
callback.invoke("Device is not connected", null);
|
|
@@ -1104,12 +1133,15 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1104
1133
|
characteristic.setWriteType(writeType);
|
|
1105
1134
|
|
|
1106
1135
|
if (data.length <= maxByteSize) {
|
|
1107
|
-
if (!
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
if (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE == writeType) {
|
|
1111
|
-
callback.invoke();
|
|
1136
|
+
if (!enqueueWrite(characteristic, data, callback)) {
|
|
1137
|
+
if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
|
|
1138
|
+
writeCallbacks.removeLastOccurrence(callback);
|
|
1112
1139
|
}
|
|
1140
|
+
callback.invoke("Write failed");
|
|
1141
|
+
completedCommand();
|
|
1142
|
+
return;
|
|
1143
|
+
} else if (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE == writeType) {
|
|
1144
|
+
callback.invoke();
|
|
1113
1145
|
}
|
|
1114
1146
|
} else {
|
|
1115
1147
|
int dataLength = data.length;
|
|
@@ -1134,21 +1166,24 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1134
1166
|
|
|
1135
1167
|
if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
|
|
1136
1168
|
writeQueue.addAll(splittedMessage);
|
|
1137
|
-
if (!
|
|
1169
|
+
if (!enqueueWrite(characteristic, firstMessage, callback)) {
|
|
1138
1170
|
writeQueue.clear();
|
|
1171
|
+
writeCallbacks.remove(callback);
|
|
1139
1172
|
callback.invoke("Write failed");
|
|
1173
|
+
completedCommand();
|
|
1174
|
+
return;
|
|
1140
1175
|
}
|
|
1141
1176
|
} else {
|
|
1142
1177
|
try {
|
|
1143
1178
|
boolean writeError = false;
|
|
1144
|
-
if (!
|
|
1179
|
+
if (!enqueueWrite(characteristic, firstMessage, callback)) {
|
|
1145
1180
|
writeError = true;
|
|
1146
1181
|
callback.invoke("Write failed");
|
|
1147
1182
|
}
|
|
1148
1183
|
if (!writeError) {
|
|
1149
1184
|
Thread.sleep(queueSleepTime);
|
|
1150
1185
|
for (byte[] message : splittedMessage) {
|
|
1151
|
-
if (!
|
|
1186
|
+
if (!enqueueWrite(characteristic, message, callback)) {
|
|
1152
1187
|
writeError = true;
|
|
1153
1188
|
callback.invoke("Write failed");
|
|
1154
1189
|
break;
|
|
@@ -1159,8 +1194,14 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1159
1194
|
callback.invoke();
|
|
1160
1195
|
}
|
|
1161
1196
|
}
|
|
1197
|
+
if (writeError) {
|
|
1198
|
+
completedCommand();
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1162
1201
|
} catch (InterruptedException e) {
|
|
1163
1202
|
callback.invoke("Error during writing");
|
|
1203
|
+
completedCommand();
|
|
1204
|
+
return;
|
|
1164
1205
|
}
|
|
1165
1206
|
}
|
|
1166
1207
|
}
|
|
@@ -1234,7 +1275,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1234
1275
|
// and UUID of all characteristics instead of using
|
|
1235
1276
|
// service.getCharacteristic(characteristicUUID)
|
|
1236
1277
|
private BluetoothGattCharacteristic findWritableCharacteristic(BluetoothGattService service,
|
|
1237
|
-
|
|
1278
|
+
UUID characteristicUUID, int writeType) {
|
|
1238
1279
|
try {
|
|
1239
1280
|
// get write property
|
|
1240
1281
|
int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;
|
|
@@ -1242,9 +1283,9 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1242
1283
|
writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
|
|
1243
1284
|
}
|
|
1244
1285
|
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1286
|
+
if (service == null) {
|
|
1287
|
+
throw new Exception("Service is null.");
|
|
1288
|
+
}
|
|
1248
1289
|
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
|
|
1249
1290
|
for (BluetoothGattCharacteristic characteristic : characteristics) {
|
|
1250
1291
|
if ((characteristic.getProperties() & writeProperty) != 0
|
|
@@ -36,7 +36,7 @@ public abstract class ScanManager {
|
|
|
36
36
|
|
|
37
37
|
public abstract void stopScan(Callback callback);
|
|
38
38
|
|
|
39
|
-
public abstract void scan(
|
|
39
|
+
public abstract void scan(ReadableMap options, Callback callback);
|
|
40
40
|
|
|
41
41
|
public abstract boolean isScanning();
|
|
42
42
|
|
|
@@ -9,40 +9,41 @@ import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes';
|
|
|
9
9
|
*/
|
|
10
10
|
export interface Spec extends TurboModule {
|
|
11
11
|
start(options: Object, callback: (error: CallbackError) => void): void;
|
|
12
|
-
|
|
12
|
+
isStarted(callback: (error: CallbackError, started: boolean) => void): void;
|
|
13
|
+
scan(scanningOptions: Object, callback: (error: CallbackError) => void): void;
|
|
13
14
|
stopScan(callback: (error: CallbackError) => void): void;
|
|
14
15
|
connect(peripheralUUID: string, options: Object, callback: (error: CallbackError) => void): void;
|
|
15
16
|
disconnect(peripheralUUID: string, force: boolean, callback: (error: CallbackError) => void): void;
|
|
16
17
|
retrieveServices(peripheralUUID: string, services: string[], callback: (error: CallbackError, peripheral: PeripheralInfo) => void): void;
|
|
17
|
-
readRSSI(peripheralUUID: string, callback: (error:
|
|
18
|
-
readDescriptor(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string, callback: (error:
|
|
19
|
-
writeDescriptor(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string, data:
|
|
20
|
-
getDiscoveredPeripherals(callback: (error:
|
|
18
|
+
readRSSI(peripheralUUID: string, callback: (error: CallbackError, rssi: number) => void): void;
|
|
19
|
+
readDescriptor(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string, callback: (error: CallbackError, data: number[]) => void): void;
|
|
20
|
+
writeDescriptor(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string, data: Object[], callback: (error: CallbackError) => void): void;
|
|
21
|
+
getDiscoveredPeripherals(callback: (error: CallbackError, result: Peripheral[] | null) => void): void;
|
|
21
22
|
checkState(callback: (state: BleState) => void): void;
|
|
22
|
-
write(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, message:
|
|
23
|
-
writeWithoutResponse(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, message:
|
|
24
|
-
read(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error:
|
|
25
|
-
startNotificationWithBuffer(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, bufferLength: number, callback: (error:
|
|
26
|
-
startNotification(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error:
|
|
27
|
-
stopNotification(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error:
|
|
28
|
-
getConnectedPeripherals(serviceUUIDStrings: string[], callback: (error:
|
|
23
|
+
write(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, message: Object[], maxByteSize: number, callback: (error: CallbackError) => void): void;
|
|
24
|
+
writeWithoutResponse(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, message: Object[], maxByteSize: number, queueSleepTime: number, callback: (error: CallbackError) => void): void;
|
|
25
|
+
read(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error: CallbackError, data: number[]) => void): void;
|
|
26
|
+
startNotificationWithBuffer(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, bufferLength: number, callback: (error: CallbackError) => void): void;
|
|
27
|
+
startNotification(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error: CallbackError) => void): void;
|
|
28
|
+
stopNotification(peripheralUUID: string, serviceUUID: string, characteristicUUID: string, callback: (error: CallbackError) => void): void;
|
|
29
|
+
getConnectedPeripherals(serviceUUIDStrings: string[], callback: (error: CallbackError, result: Peripheral[] | null) => void): void;
|
|
29
30
|
isPeripheralConnected(peripheralUUID: string, callback: (error: Peripheral[]) => void): void;
|
|
30
|
-
isScanning(callback: (error:
|
|
31
|
-
getMaximumWriteValueLengthForWithoutResponse(peripheralUUID: string, callback: (error:
|
|
32
|
-
getMaximumWriteValueLengthForWithResponse(deviceUUID: string, callback: (error:
|
|
33
|
-
enableBluetooth(callback: (error:
|
|
34
|
-
getBondedPeripherals(callback: (error:
|
|
35
|
-
createBond(peripheralUUID: string, devicePin: string, callback: (error:
|
|
36
|
-
removeBond(peripheralUUID: string, callback: (error:
|
|
37
|
-
removePeripheral(peripheralUUID: string, callback: (error:
|
|
38
|
-
requestMTU(peripheralUUID: string, mtu: number, callback: (error:
|
|
39
|
-
requestConnectionPriority(peripheralUUID: string, connectionPriority: number, callback: (error:
|
|
40
|
-
refreshCache(peripheralUUID: string, callback: (error:
|
|
31
|
+
isScanning(callback: (error: CallbackError, status: boolean) => void): void;
|
|
32
|
+
getMaximumWriteValueLengthForWithoutResponse(peripheralUUID: string, callback: (error: CallbackError, max: number) => void): void;
|
|
33
|
+
getMaximumWriteValueLengthForWithResponse(deviceUUID: string, callback: (error: CallbackError, max: number) => void): void;
|
|
34
|
+
enableBluetooth(callback: (error: CallbackError) => void): void;
|
|
35
|
+
getBondedPeripherals(callback: (error: CallbackError, result: Peripheral[] | null) => void): void;
|
|
36
|
+
createBond(peripheralUUID: string, devicePin: string, callback: (error: CallbackError) => void): void;
|
|
37
|
+
removeBond(peripheralUUID: string, callback: (error: CallbackError) => void): void;
|
|
38
|
+
removePeripheral(peripheralUUID: string, callback: (error: CallbackError) => void): void;
|
|
39
|
+
requestMTU(peripheralUUID: string, mtu: number, callback: (error: CallbackError, mtu: number) => void): void;
|
|
40
|
+
requestConnectionPriority(peripheralUUID: string, connectionPriority: number, callback: (error: CallbackError, status: boolean) => void): void;
|
|
41
|
+
refreshCache(peripheralUUID: string, callback: (error: CallbackError, result: boolean) => void): void;
|
|
41
42
|
setName(name: string): void;
|
|
42
|
-
getAssociatedPeripherals(callback: (error:
|
|
43
|
-
removeAssociatedPeripheral(peripheralUUID: string, callback: (error:
|
|
43
|
+
getAssociatedPeripherals(callback: (error: CallbackError, peripherals: Peripheral[] | null) => void): void;
|
|
44
|
+
removeAssociatedPeripheral(peripheralUUID: string, callback: (error: CallbackError) => void): void;
|
|
44
45
|
supportsCompanion(callback: (supports: boolean) => void): void;
|
|
45
|
-
companionScan(serviceUUIDs: string[], option: Object, callback: (error:
|
|
46
|
+
companionScan(serviceUUIDs: string[], option: Object, callback: (error: CallbackError, peripheral: Peripheral | null) => void): void;
|
|
46
47
|
/**
|
|
47
48
|
* Supported events.
|
|
48
49
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// oxlint-disable no-wrapper-object-types
|
|
3
4
|
const react_native_1 = require("react-native");
|
|
4
5
|
exports.default = react_native_1.TurboModuleRegistry.get('BleManager');
|
|
5
6
|
//# sourceMappingURL=NativeBleManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NativeBleManager.js","sourceRoot":"","sources":["../../src/NativeBleManager.ts"],"names":[],"mappings":";;AAAA,+CAAgE;
|
|
1
|
+
{"version":3,"file":"NativeBleManager.js","sourceRoot":"","sources":["../../src/NativeBleManager.ts"],"names":[],"mappings":";;AAAA,yCAAyC;AACzC,+CAAgE;AAkNhE,kBAAe,kCAAmB,CAAC,GAAG,CAAO,YAAY,CAAS,CAAC"}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -104,6 +104,11 @@ declare class BleManager {
|
|
|
104
104
|
stopNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
|
|
105
105
|
checkState(): Promise<BleState>;
|
|
106
106
|
start(options?: StartOptions): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Check if the BLE manager has been started.
|
|
109
|
+
* @returns boolean promise indicating if the manager is started.
|
|
110
|
+
*/
|
|
111
|
+
isStarted(): Promise<boolean>;
|
|
107
112
|
/**
|
|
108
113
|
*
|
|
109
114
|
* @param serviceUUIDs
|
|
@@ -112,7 +117,7 @@ declare class BleManager {
|
|
|
112
117
|
* @param scanningOptions optional map of properties to fine-tune scan behavior, see DOCS.
|
|
113
118
|
* @returns
|
|
114
119
|
*/
|
|
115
|
-
scan(
|
|
120
|
+
scan(scanningOptions?: ScanOptions): Promise<void>;
|
|
116
121
|
stopScan(): Promise<void>;
|
|
117
122
|
/**
|
|
118
123
|
* [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
|
package/dist/cjs/index.js
CHANGED
|
@@ -318,6 +318,22 @@ class BleManager {
|
|
|
318
318
|
});
|
|
319
319
|
});
|
|
320
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Check if the BLE manager has been started.
|
|
323
|
+
* @returns boolean promise indicating if the manager is started.
|
|
324
|
+
*/
|
|
325
|
+
isStarted() {
|
|
326
|
+
return new Promise((fulfill, reject) => {
|
|
327
|
+
BleManagerModule.isStarted((error, started) => {
|
|
328
|
+
if (error) {
|
|
329
|
+
reject(error);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
fulfill(started);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
321
337
|
/**
|
|
322
338
|
*
|
|
323
339
|
* @param serviceUUIDs
|
|
@@ -326,10 +342,16 @@ class BleManager {
|
|
|
326
342
|
* @param scanningOptions optional map of properties to fine-tune scan behavior, see DOCS.
|
|
327
343
|
* @returns
|
|
328
344
|
*/
|
|
329
|
-
scan(
|
|
345
|
+
scan(scanningOptions = {}) {
|
|
330
346
|
return new Promise((fulfill, reject) => {
|
|
331
|
-
if (
|
|
332
|
-
|
|
347
|
+
if (scanningOptions.serviceUUIDs == null) {
|
|
348
|
+
scanningOptions.serviceUUIDs = [];
|
|
349
|
+
}
|
|
350
|
+
if (scanningOptions.seconds == null) {
|
|
351
|
+
scanningOptions.seconds = 0;
|
|
352
|
+
}
|
|
353
|
+
if (scanningOptions.allowDuplicates == null) {
|
|
354
|
+
scanningOptions.allowDuplicates = false;
|
|
333
355
|
}
|
|
334
356
|
// (ANDROID) Match as many advertisement per filter as hw could allow
|
|
335
357
|
// depends on current capability and availability of the resources in hw.
|
|
@@ -367,7 +389,7 @@ class BleManager {
|
|
|
367
389
|
];
|
|
368
390
|
}
|
|
369
391
|
}
|
|
370
|
-
BleManagerModule.scan(
|
|
392
|
+
BleManagerModule.scan(scanningOptions, (error) => {
|
|
371
393
|
if (error) {
|
|
372
394
|
reject(error);
|
|
373
395
|
}
|