react-native-ble-manager 12.1.1 → 12.1.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/android/src/main/java/it/innove/BleManager.java +2 -0
- package/android/src/main/java/it/innove/DefaultScanManager.java +7 -2
- package/android/src/main/java/it/innove/Peripheral.java +92 -65
- package/ios/BleManager.h +3 -0
- package/ios/BleManager.mm +9 -1
- package/ios/SwiftBleManager.swift +2 -2
- package/package.json +1 -1
|
@@ -767,6 +767,8 @@ class BleManager extends NativeBleManagerSpec {
|
|
|
767
767
|
if (peripheral.isConnected()) {
|
|
768
768
|
peripheral.disconnect(null, true);
|
|
769
769
|
}
|
|
770
|
+
peripheral.errorAndClearAllCallbacks("disconnected by BleManager");
|
|
771
|
+
peripheral.resetQueuesAndBuffers();
|
|
770
772
|
}
|
|
771
773
|
}
|
|
772
774
|
}
|
|
@@ -7,6 +7,8 @@ import android.Manifest;
|
|
|
7
7
|
import android.annotation.SuppressLint;
|
|
8
8
|
import android.bluetooth.BluetoothAdapter;
|
|
9
9
|
import android.bluetooth.BluetoothDevice;
|
|
10
|
+
import android.bluetooth.BluetoothDevice;
|
|
11
|
+
import android.bluetooth.le.BluetoothLeScanner;
|
|
10
12
|
import android.bluetooth.le.ScanCallback;
|
|
11
13
|
import android.bluetooth.le.ScanFilter;
|
|
12
14
|
import android.bluetooth.le.ScanRecord;
|
|
@@ -45,7 +47,9 @@ public class DefaultScanManager extends ScanManager {
|
|
|
45
47
|
// update scanSessionId to prevent stopping next scan by running timeout thread
|
|
46
48
|
scanSessionId.incrementAndGet();
|
|
47
49
|
|
|
48
|
-
getBluetoothAdapter().getBluetoothLeScanner()
|
|
50
|
+
final BluetoothLeScanner scanner = getBluetoothAdapter().getBluetoothLeScanner();
|
|
51
|
+
if (scanner != null)
|
|
52
|
+
scanner.stopScan(mScanCallback);
|
|
49
53
|
isScanning = false;
|
|
50
54
|
callback.invoke();
|
|
51
55
|
}
|
|
@@ -173,7 +177,8 @@ public class DefaultScanManager extends ScanManager {
|
|
|
173
177
|
// check current scan session was not stopped
|
|
174
178
|
if (scanSessionId.intValue() == currentScanSession) {
|
|
175
179
|
if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
|
|
176
|
-
btAdapter.getBluetoothLeScanner()
|
|
180
|
+
final BluetoothLeScanner scanner = btAdapter.getBluetoothLeScanner();
|
|
181
|
+
if (scanner != null) scanner.stopScan(mScanCallback);
|
|
177
182
|
isScanning = false;
|
|
178
183
|
}
|
|
179
184
|
|
|
@@ -12,6 +12,7 @@ import android.bluetooth.BluetoothGattDescriptor;
|
|
|
12
12
|
import android.bluetooth.BluetoothGattService;
|
|
13
13
|
import android.bluetooth.BluetoothProfile;
|
|
14
14
|
import android.bluetooth.BluetoothStatusCodes;
|
|
15
|
+
|
|
15
16
|
import android.content.Context;
|
|
16
17
|
import android.os.Build;
|
|
17
18
|
import android.os.Handler;
|
|
@@ -160,14 +161,9 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
160
161
|
|
|
161
162
|
public void disconnect(final Callback callback, final boolean force) {
|
|
162
163
|
mainHandler.post(() -> {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
connectCallbacks.clear();
|
|
164
|
+
errorAndClearAllCallbacks("Disconnect called before the command completed");
|
|
165
|
+
resetQueuesAndBuffers();
|
|
167
166
|
connected = false;
|
|
168
|
-
clearBuffers();
|
|
169
|
-
commandQueue.clear();
|
|
170
|
-
commandQueueBusy = false;
|
|
171
167
|
|
|
172
168
|
if (gatt != null) {
|
|
173
169
|
try {
|
|
@@ -297,15 +293,83 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
297
293
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
|
298
294
|
super.onServicesDiscovered(gatt, status);
|
|
299
295
|
mainHandler.post(() -> {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
296
|
+
if (gatt == null) {
|
|
297
|
+
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
298
|
+
retrieveServicesCallback.invoke("Error during service retrieval: gatt is null");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
else if (status == BluetoothGatt.GATT_SUCCESS)
|
|
302
|
+
{
|
|
303
|
+
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
304
|
+
WritableMap map = this.asWritableMap(gatt);
|
|
305
|
+
retrieveServicesCallback.invoke(null, map);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
310
|
+
retrieveServicesCallback.invoke("Error during service retrieval.");
|
|
311
|
+
}
|
|
303
312
|
}
|
|
304
313
|
retrieveServicesCallbacks.clear();
|
|
305
314
|
completedCommand();
|
|
306
315
|
});
|
|
307
316
|
}
|
|
308
317
|
|
|
318
|
+
public void errorAndClearAllCallbacks(final String errorMessage) {
|
|
319
|
+
|
|
320
|
+
for (Callback writeCallback : writeCallbacks) {
|
|
321
|
+
writeCallback.invoke(errorMessage);
|
|
322
|
+
}
|
|
323
|
+
writeCallbacks.clear();
|
|
324
|
+
|
|
325
|
+
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
326
|
+
retrieveServicesCallback.invoke(errorMessage);
|
|
327
|
+
}
|
|
328
|
+
retrieveServicesCallbacks.clear();
|
|
329
|
+
|
|
330
|
+
for (Callback readRSSICallback : readRSSICallbacks) {
|
|
331
|
+
readRSSICallback.invoke(errorMessage);
|
|
332
|
+
}
|
|
333
|
+
readRSSICallbacks.clear();
|
|
334
|
+
|
|
335
|
+
for (Callback registerNotifyCallback : registerNotifyCallbacks) {
|
|
336
|
+
registerNotifyCallback.invoke(errorMessage);
|
|
337
|
+
}
|
|
338
|
+
registerNotifyCallbacks.clear();
|
|
339
|
+
|
|
340
|
+
for (Callback requestMTUCallback : requestMTUCallbacks) {
|
|
341
|
+
requestMTUCallback.invoke(errorMessage);
|
|
342
|
+
}
|
|
343
|
+
requestMTUCallbacks.clear();
|
|
344
|
+
|
|
345
|
+
for (Callback readCallback : readCallbacks) {
|
|
346
|
+
readCallback.invoke(errorMessage);
|
|
347
|
+
}
|
|
348
|
+
readCallbacks.clear();
|
|
349
|
+
|
|
350
|
+
for (Callback readDescriptorCallback : readDescriptorCallbacks) {
|
|
351
|
+
readDescriptorCallback.invoke(errorMessage);
|
|
352
|
+
}
|
|
353
|
+
readDescriptorCallbacks.clear();
|
|
354
|
+
|
|
355
|
+
for (Callback callback : writeDescriptorCallbacks) {
|
|
356
|
+
callback.invoke(errorMessage);
|
|
357
|
+
}
|
|
358
|
+
writeDescriptorCallbacks.clear();
|
|
359
|
+
|
|
360
|
+
for (Callback connectCallback : connectCallbacks) {
|
|
361
|
+
connectCallback.invoke(errorMessage);
|
|
362
|
+
}
|
|
363
|
+
connectCallbacks.clear();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
public void resetQueuesAndBuffers() {
|
|
367
|
+
writeQueue.clear();
|
|
368
|
+
commandQueue.clear();
|
|
369
|
+
commandQueueBusy = false;
|
|
370
|
+
connected = false;
|
|
371
|
+
clearBuffers();
|
|
372
|
+
}
|
|
309
373
|
@Override
|
|
310
374
|
public void onConnectionStateChange(BluetoothGatt gatta, int status, final int newState) {
|
|
311
375
|
|
|
@@ -315,7 +379,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
315
379
|
mainHandler.post(() -> {
|
|
316
380
|
gatt = gatta;
|
|
317
381
|
|
|
318
|
-
if (status != BluetoothGatt.GATT_SUCCESS) {
|
|
382
|
+
if (gatt != null && status != BluetoothGatt.GATT_SUCCESS) {
|
|
319
383
|
gatt.close();
|
|
320
384
|
}
|
|
321
385
|
|
|
@@ -333,59 +397,13 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
333
397
|
|
|
334
398
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED || status != BluetoothGatt.GATT_SUCCESS) {
|
|
335
399
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
for (Callback retrieveServicesCallback : retrieveServicesCallbacks) {
|
|
342
|
-
retrieveServicesCallback.invoke("Device disconnected");
|
|
343
|
-
}
|
|
344
|
-
retrieveServicesCallbacks.clear();
|
|
345
|
-
|
|
346
|
-
for (Callback readRSSICallback : readRSSICallbacks) {
|
|
347
|
-
readRSSICallback.invoke("Device disconnected");
|
|
348
|
-
}
|
|
349
|
-
readRSSICallbacks.clear();
|
|
350
|
-
|
|
351
|
-
for (Callback registerNotifyCallback : registerNotifyCallbacks) {
|
|
352
|
-
registerNotifyCallback.invoke("Device disconnected");
|
|
353
|
-
}
|
|
354
|
-
registerNotifyCallbacks.clear();
|
|
355
|
-
|
|
356
|
-
for (Callback requestMTUCallback : requestMTUCallbacks) {
|
|
357
|
-
requestMTUCallback.invoke("Device disconnected");
|
|
358
|
-
}
|
|
359
|
-
requestMTUCallbacks.clear();
|
|
360
|
-
|
|
361
|
-
for (Callback readCallback : readCallbacks) {
|
|
362
|
-
readCallback.invoke("Device disconnected");
|
|
363
|
-
}
|
|
364
|
-
readCallbacks.clear();
|
|
365
|
-
|
|
366
|
-
for (Callback readDescriptorCallback : readDescriptorCallbacks) {
|
|
367
|
-
readDescriptorCallback.invoke("Device disconnected");
|
|
368
|
-
}
|
|
369
|
-
readDescriptorCallbacks.clear();
|
|
370
|
-
|
|
371
|
-
for (Callback callback : writeDescriptorCallbacks) {
|
|
372
|
-
callback.invoke("Device disconnected");
|
|
373
|
-
}
|
|
374
|
-
writeDescriptorCallbacks.clear();
|
|
375
|
-
|
|
376
|
-
for (Callback connectCallback : connectCallbacks) {
|
|
377
|
-
connectCallback.invoke("Connection error");
|
|
400
|
+
errorAndClearAllCallbacks("Device disconnected");
|
|
401
|
+
resetQueuesAndBuffers();
|
|
402
|
+
if (gatt != null) {
|
|
403
|
+
gatt.disconnect();
|
|
404
|
+
gatt.close();
|
|
378
405
|
}
|
|
379
|
-
connectCallbacks.clear();
|
|
380
406
|
|
|
381
|
-
writeQueue.clear();
|
|
382
|
-
commandQueue.clear();
|
|
383
|
-
commandQueueBusy = false;
|
|
384
|
-
connected = false;
|
|
385
|
-
clearBuffers();
|
|
386
|
-
|
|
387
|
-
gatt.disconnect();
|
|
388
|
-
gatt.close();
|
|
389
407
|
gatt = null;
|
|
390
408
|
sendDisconnectionEvent(device, BluetoothGatt.GATT_SUCCESS);
|
|
391
409
|
}
|
|
@@ -891,7 +909,9 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
891
909
|
}
|
|
892
910
|
|
|
893
911
|
private boolean enqueue(Runnable command) {
|
|
912
|
+
|
|
894
913
|
final boolean result = commandQueue.add(command);
|
|
914
|
+
|
|
895
915
|
if (result) {
|
|
896
916
|
nextCommand();
|
|
897
917
|
} else {
|
|
@@ -921,9 +941,9 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
921
941
|
|
|
922
942
|
// Check if we still have a valid gatt object
|
|
923
943
|
if (gatt == null) {
|
|
924
|
-
Log.d(BleManager.LOG_TAG, "Error, gatt is null");
|
|
925
|
-
|
|
926
|
-
|
|
944
|
+
Log.d(BleManager.LOG_TAG, "Error, gatt is null. Fill all callbacks with an error");
|
|
945
|
+
errorAndClearAllCallbacks("Gatt is null");
|
|
946
|
+
resetQueuesAndBuffers();
|
|
927
947
|
return;
|
|
928
948
|
}
|
|
929
949
|
|
|
@@ -971,6 +991,10 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
971
991
|
public void refreshCache(Callback callback) {
|
|
972
992
|
enqueue(() -> {
|
|
973
993
|
try {
|
|
994
|
+
if (gatt == null) {
|
|
995
|
+
throw new Exception("gatt is null");
|
|
996
|
+
}
|
|
997
|
+
|
|
974
998
|
Method localMethod = gatt.getClass().getMethod("refresh", new Class[0]);
|
|
975
999
|
boolean res = (Boolean) localMethod.invoke(gatt, new Object[0]);
|
|
976
1000
|
callback.invoke(null, res);
|
|
@@ -1218,6 +1242,9 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
1218
1242
|
writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
|
|
1219
1243
|
}
|
|
1220
1244
|
|
|
1245
|
+
if (service == null) {
|
|
1246
|
+
throw new Exception("Service is null.");
|
|
1247
|
+
}
|
|
1221
1248
|
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
|
|
1222
1249
|
for (BluetoothGattCharacteristic characteristic : characteristics) {
|
|
1223
1250
|
if ((characteristic.getProperties() & writeProperty) != 0
|
package/ios/BleManager.h
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
#ifdef RCT_NEW_ARCH_ENABLED
|
|
5
5
|
|
|
6
6
|
#import <BleManagerSpec/BleManagerSpec.h>
|
|
7
|
+
@class SwiftBleManager;
|
|
7
8
|
|
|
8
9
|
@interface BleManager : NativeBleManagerSpecBase <NativeBleManagerSpec>
|
|
9
10
|
- (void)emitOnDiscoverPeripheral:(NSDictionary *)value;
|
|
@@ -17,6 +18,8 @@
|
|
|
17
18
|
- (void)emitOnDidUpdateNotificationStateFor:(NSDictionary *)value;
|
|
18
19
|
- (void)emitOnCompanionPeripheral:(NSDictionary *)value;
|
|
19
20
|
- (void)emitOnCompanionFailure:(NSDictionary *)value;
|
|
21
|
+
+ (nullable CBCentralManager *)getCentralManager;
|
|
22
|
+
+ (nullable SwiftBleManager *)getInstance;
|
|
20
23
|
@end
|
|
21
24
|
|
|
22
25
|
#else
|
package/ios/BleManager.mm
CHANGED
|
@@ -214,7 +214,7 @@ RCT_EXPORT_MODULE()
|
|
|
214
214
|
[_swBleManager startNotificationWithBuffer:peripheralUUID
|
|
215
215
|
serviceUUID:serviceUUID
|
|
216
216
|
characteristicUUID:characteristicUUID
|
|
217
|
-
bufferLength
|
|
217
|
+
bufferLength:@(bufferLength)
|
|
218
218
|
callback:callback];
|
|
219
219
|
}
|
|
220
220
|
|
|
@@ -290,4 +290,12 @@ RCT_EXPORT_MODULE()
|
|
|
290
290
|
callback:callback];
|
|
291
291
|
}
|
|
292
292
|
|
|
293
|
+
+ (nullable CBCentralManager *)getCentralManager {
|
|
294
|
+
return [SwiftBleManager getCentralManager];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
+ (nullable SwiftBleManager *)getInstance {
|
|
298
|
+
return [SwiftBleManager getInstance];
|
|
299
|
+
}
|
|
300
|
+
|
|
293
301
|
@end
|
|
@@ -1178,11 +1178,11 @@ import CoreBluetooth
|
|
|
1178
1178
|
}
|
|
1179
1179
|
|
|
1180
1180
|
|
|
1181
|
-
static func getCentralManager() -> CBCentralManager? {
|
|
1181
|
+
@objc public static func getCentralManager() -> CBCentralManager? {
|
|
1182
1182
|
return sharedManager
|
|
1183
1183
|
}
|
|
1184
1184
|
|
|
1185
|
-
static func getInstance() -> SwiftBleManager? {
|
|
1185
|
+
@objc public static func getInstance() -> SwiftBleManager? {
|
|
1186
1186
|
return shared
|
|
1187
1187
|
}
|
|
1188
1188
|
|