react-native-ble-manager 8.0.0 → 8.1.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/README.md
CHANGED
|
@@ -30,15 +30,37 @@ The library support the react native autolink feature.
|
|
|
30
30
|
|
|
31
31
|
```xml
|
|
32
32
|
// file: android/app/src/main/AndroidManifest.xml
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
<!-- Add xmlns:tools -->
|
|
34
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
35
|
+
xmlns:tools="http://schemas.android.com/tools"
|
|
36
|
+
package="YOUR_PACKAGE_NAME">
|
|
37
|
+
|
|
38
|
+
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
|
|
39
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
|
|
40
|
+
|
|
41
|
+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28"/>
|
|
42
|
+
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" tools:targetApi="Q"/>
|
|
43
|
+
|
|
44
|
+
<!-- Only when targeting Android 12 or higher -->
|
|
45
|
+
<!-- Please make sure you read the following documentation to have a
|
|
46
|
+
better understanding of the new permissions.
|
|
47
|
+
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#assert-never-for-location
|
|
48
|
+
-->
|
|
49
|
+
|
|
50
|
+
<!-- If your app doesn't use Bluetooth scan results to derive physical location information,
|
|
51
|
+
you can strongly assert that your app
|
|
52
|
+
doesn't derive physical location. -->
|
|
53
|
+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
|
|
54
|
+
android:usesPermissionFlags="neverForLocation"
|
|
55
|
+
tools:targetApi="s" />
|
|
56
|
+
|
|
57
|
+
<!-- Needed only if your app looks for Bluetooth devices. -->
|
|
58
|
+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
|
59
|
+
<!-- Needed only if your app makes the device discoverable to Bluetooth devices. -->
|
|
60
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
|
|
37
61
|
...
|
|
38
62
|
```
|
|
39
63
|
|
|
40
|
-
In Android API 29 >= you need to use "ACCESS_FINE_LOCATION" instead of "ACCESS_COARSE_LOCATION".
|
|
41
|
-
|
|
42
64
|
If you need communication while the app is not in the foreground you need the "ACCESS_BACKGROUND_LOCATION" permission.
|
|
43
65
|
|
|
44
66
|
##### iOS - Update Info.plist
|
|
@@ -53,6 +75,7 @@ In iOS >= 13 you need to add the `NSBluetoothAlwaysUsageDescription` string key.
|
|
|
53
75
|
- Android API >= 29 require the ACCESS_FINE_LOCATION permission to scan for peripherals.
|
|
54
76
|
React-Native 0.63.X started targeting Android API 29.
|
|
55
77
|
- Before write, read or start notification you need to call `retrieveServices` method
|
|
78
|
+
- Because location and bluetooth permissions are runtime permissions, you **must** request these permissions at runtime along with declaring them in your manifest.
|
|
56
79
|
|
|
57
80
|
## Example
|
|
58
81
|
|
|
@@ -757,6 +780,16 @@ A peripheral was disconnected.
|
|
|
757
780
|
|
|
758
781
|
- `peripheral` - `String` - the id of the peripheral
|
|
759
782
|
- `status` - `Number` - [Android only] disconnect [`reasons`](<https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onConnectionStateChange(android.bluetooth.BluetoothGatt,%20int,%20int)>)
|
|
783
|
+
- `domain` - `String` - [iOS only] disconnect error domain
|
|
784
|
+
- `code` - `Number` - [iOS only] disconnect error code (<https://developer.apple.com/documentation/corebluetooth/cberror/code>)
|
|
785
|
+
|
|
786
|
+
### BleManagerPeripheralDidBond
|
|
787
|
+
|
|
788
|
+
A bond with a peripheral was established
|
|
789
|
+
|
|
790
|
+
**Arguments**
|
|
791
|
+
|
|
792
|
+
Object with information about the device
|
|
760
793
|
|
|
761
794
|
### BleManagerCentralManagerWillRestoreState [iOS only]
|
|
762
795
|
|
|
@@ -771,3 +804,15 @@ _For more on performing long-term bluetooth actions in the background:_
|
|
|
771
804
|
[iOS Bluetooth State Preservation and Restoration](https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW10)
|
|
772
805
|
|
|
773
806
|
[iOS Relaunch Conditions](https://developer.apple.com/library/archive/qa/qa1962/_index.html)
|
|
807
|
+
|
|
808
|
+
### BleManagerDidUpdateNotificationStateFor [iOS only]
|
|
809
|
+
|
|
810
|
+
The peripheral received a request to start or stop providing notifications for a specified characteristic's value.
|
|
811
|
+
|
|
812
|
+
**Arguments**
|
|
813
|
+
|
|
814
|
+
- `peripheral` - `String` - the id of the peripheral
|
|
815
|
+
- `characteristic` - `String` - the UUID of the characteristic
|
|
816
|
+
- `isNotifying` - `Boolean` - Is the characteristic notifying or not
|
|
817
|
+
- `domain` - `String` - [iOS only] error domain
|
|
818
|
+
- `code` - `Number` - [iOS only] error code
|
|
@@ -193,7 +193,7 @@ class BleManager extends ReactContextBaseJavaModule {
|
|
|
193
193
|
for (Iterator<Map.Entry<String, Peripheral>> iterator = peripherals.entrySet().iterator(); iterator
|
|
194
194
|
.hasNext(); ) {
|
|
195
195
|
Map.Entry<String, Peripheral> entry = iterator.next();
|
|
196
|
-
if (!entry.getValue().isConnected()) {
|
|
196
|
+
if (!(entry.getValue().isConnected() || entry.getValue().isConnecting())) {
|
|
197
197
|
iterator.remove();
|
|
198
198
|
}
|
|
199
199
|
}
|
|
@@ -53,6 +53,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
53
53
|
protected byte[] advertisingDataBytes = new byte[0];
|
|
54
54
|
protected int advertisingRSSI;
|
|
55
55
|
private boolean connected = false;
|
|
56
|
+
private boolean connecting = false;
|
|
56
57
|
private ReactContext reactContext;
|
|
57
58
|
|
|
58
59
|
private BluetoothGatt gatt;
|
|
@@ -104,6 +105,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
104
105
|
if (!connected) {
|
|
105
106
|
BluetoothDevice device = getDevice();
|
|
106
107
|
this.connectCallback = callback;
|
|
108
|
+
this.connecting = true;
|
|
107
109
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
108
110
|
Log.d(BleManager.LOG_TAG, " Is Or Greater than M $mBluetoothDevice");
|
|
109
111
|
gatt = device.connectGatt(activity, false, this, BluetoothDevice.TRANSPORT_LE);
|
|
@@ -252,6 +254,10 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
252
254
|
return connected;
|
|
253
255
|
}
|
|
254
256
|
|
|
257
|
+
public boolean isConnecting() {
|
|
258
|
+
return connecting;
|
|
259
|
+
}
|
|
260
|
+
|
|
255
261
|
public BluetoothDevice getDevice() {
|
|
256
262
|
return device;
|
|
257
263
|
}
|
|
@@ -288,6 +294,7 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
288
294
|
newState = BluetoothProfile.STATE_DISCONNECTED;
|
|
289
295
|
}
|
|
290
296
|
|
|
297
|
+
connecting = false;
|
|
291
298
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
|
292
299
|
connected = true;
|
|
293
300
|
|
|
@@ -315,14 +322,11 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
315
322
|
|
|
316
323
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
|
317
324
|
|
|
318
|
-
this.disconnect(true);
|
|
319
|
-
|
|
320
325
|
if (discoverServicesRunnable != null) {
|
|
321
326
|
mainHandler.removeCallbacks(discoverServicesRunnable);
|
|
322
327
|
discoverServicesRunnable = null;
|
|
323
328
|
}
|
|
324
329
|
|
|
325
|
-
sendConnectionEvent(device, "BleManagerDisconnectPeripheral", status);
|
|
326
330
|
List<Callback> callbacks = Arrays.asList(writeCallback, retrieveServicesCallback, readRSSICallback,
|
|
327
331
|
readCallback, registerNotifyCallback, requestMTUCallback);
|
|
328
332
|
for (Callback currentCallback : callbacks) {
|
|
@@ -343,6 +347,8 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
343
347
|
requestMTUCallback = null;
|
|
344
348
|
commandQueue.clear();
|
|
345
349
|
commandQueueBusy = false;
|
|
350
|
+
|
|
351
|
+
this.disconnect(true);
|
|
346
352
|
}
|
|
347
353
|
|
|
348
354
|
}
|
|
@@ -847,6 +853,10 @@ public class Peripheral extends BluetoothGattCallback {
|
|
|
847
853
|
if (! doWrite(characteristic, data, callback)) {
|
|
848
854
|
callback.invoke("Write failed");
|
|
849
855
|
writeCallback = null;
|
|
856
|
+
} else {
|
|
857
|
+
if (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE == writeType) {
|
|
858
|
+
callback.invoke();
|
|
859
|
+
}
|
|
850
860
|
}
|
|
851
861
|
} else {
|
|
852
862
|
int dataLength = data.length;
|
package/ios/BleManager.m
CHANGED
|
@@ -76,7 +76,7 @@ bool hasListeners;
|
|
|
76
76
|
|
|
77
77
|
- (NSArray<NSString *> *)supportedEvents
|
|
78
78
|
{
|
|
79
|
-
return @[@"BleManagerDidUpdateValueForCharacteristic", @"BleManagerStopScan", @"BleManagerDiscoverPeripheral", @"BleManagerConnectPeripheral", @"BleManagerDisconnectPeripheral", @"BleManagerDidUpdateState", @"BleManagerCentralManagerWillRestoreState"];
|
|
79
|
+
return @[@"BleManagerDidUpdateValueForCharacteristic", @"BleManagerStopScan", @"BleManagerDiscoverPeripheral", @"BleManagerConnectPeripheral", @"BleManagerDisconnectPeripheral", @"BleManagerDidUpdateState", @"BleManagerCentralManagerWillRestoreState", @"BleManagerDidUpdateNotificationStateFor"];
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
|
|
@@ -110,6 +110,9 @@ bool hasListeners;
|
|
|
110
110
|
if (characteristic == nil){
|
|
111
111
|
return;
|
|
112
112
|
}
|
|
113
|
+
if (hasListeners) {
|
|
114
|
+
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString, @"isNotifying": @(false), @"domain": [error domain], @"code": @(error.code)}];
|
|
115
|
+
}
|
|
113
116
|
}
|
|
114
117
|
|
|
115
118
|
NSString *key = [self keyForPeripheral: peripheral andCharacteristic:characteristic];
|
|
@@ -138,6 +141,9 @@ bool hasListeners;
|
|
|
138
141
|
[stopNotificationCallbacks removeObjectForKey:key];
|
|
139
142
|
}
|
|
140
143
|
}
|
|
144
|
+
if (hasListeners) {
|
|
145
|
+
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString, @"isNotifying": @(characteristic.isNotifying)}];
|
|
146
|
+
}
|
|
141
147
|
}
|
|
142
148
|
|
|
143
149
|
|
|
@@ -860,7 +866,11 @@ RCT_EXPORT_METHOD(requestMTU:(NSString *)deviceUUID mtu:(NSInteger)mtu callback:
|
|
|
860
866
|
}
|
|
861
867
|
|
|
862
868
|
if (hasListeners) {
|
|
863
|
-
|
|
869
|
+
if (error) {
|
|
870
|
+
[self sendEventWithName:@"BleManagerDisconnectPeripheral" body:@{@"peripheral": [peripheral uuidAsString], @"domain": [error domain], @"code": @(error.code)}];
|
|
871
|
+
} else {
|
|
872
|
+
[self sendEventWithName:@"BleManagerDisconnectPeripheral" body:@{@"peripheral": [peripheral uuidAsString]}];
|
|
873
|
+
}
|
|
864
874
|
}
|
|
865
875
|
}
|
|
866
876
|
|