react-native-ble-manager 8.6.0 → 8.8.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/BleManager.js CHANGED
@@ -250,7 +250,7 @@ class BleManager {
250
250
  }
251
251
 
252
252
  // (ANDROID) Match as many advertisement per filter as hw could allow
253
- // dependes on current capability and availability of the resources in hw.
253
+ // depends on current capability and availability of the resources in hw.
254
254
  if (scanningOptions.numberOfMatches == null) {
255
255
  scanningOptions.numberOfMatches = 3;
256
256
  }
@@ -260,11 +260,19 @@ class BleManager {
260
260
  scanningOptions.matchMode = 1;
261
261
  }
262
262
 
263
- // (ANDROID) Defaults to SCAN_MODE_LOW_POWER on android
263
+ // (ANDROID) Defaults to SCAN_MODE_LOW_POWER
264
264
  if (scanningOptions.scanMode == null) {
265
265
  scanningOptions.scanMode = 0;
266
266
  }
267
+
268
+ // (ANDROID) Defaults to CALLBACK_TYPE_ALL_MATCHES
269
+ // WARN: sometimes, setting a scanSetting instead of leaving it untouched might result in unexpected behaviors.
270
+ // https://github.com/dariuszseweryn/RxAndroidBle/issues/462
271
+ if (scanningOptions.callbackType == null) {
272
+ scanningOptions.callbackType = 1;
273
+ }
267
274
 
275
+ // (ANDROID) Defaults to 0ms (report results immediately).
268
276
  if (scanningOptions.reportDelay == null) {
269
277
  scanningOptions.reportDelay = 0;
270
278
  }
package/README.md CHANGED
@@ -122,7 +122,7 @@ BleManager.start({ showAlert: false }).then(() => {
122
122
 
123
123
  ### scan(serviceUUIDs, seconds, allowDuplicates, scanningOptions)
124
124
 
125
- Scan for availables peripherals.
125
+ Scan for available peripherals.
126
126
  Returns a `Promise` object.
127
127
 
128
128
  **Arguments**
@@ -131,10 +131,11 @@ 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` - [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)>)
134
+ - `numberOfMatches` - `Number` - [Android only] corresponding to [`setNumOfMatches`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setNumOfMatches(int)>). Defaults to `ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT`.
135
+ - `matchMode` - `Number` - [Android only] corresponding to [`setMatchMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setMatchMode(int)>). Defaults to `ScanSettings.MATCH_MODE_AGGRESSIVE`.
136
+ - `callbackType` - `Number` - [Android only] corresponding to [`setCallbackType`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setCallbackType(int)>). Defaults `ScanSettings.CALLBACK_TYPE_ALL_MATCHES`.
137
+ - `scanMode` - `Number` - [Android only] corresponding to [`setScanMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setScanMode(int)>). Defaults to `ScanSettings.SCAN_MODE_LOW_POWER`.
138
+ - `reportDelay` - `Number` - [Android only] corresponding to [`setReportDelay`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setReportDelay(long)>). Defaults to `0ms`.
138
139
  - `phy` - `Number` - [Android only] corresponding to [`setPhy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setPhy(int))
139
140
  - `legacy` - `Boolean` - [Android only] corresponding to [`setLegacy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setLegacy(boolean))
140
141
 
@@ -440,7 +441,7 @@ BleManager.writeWithoutResponse(
440
441
  ### readRSSI(peripheralId)
441
442
 
442
443
  Read the current value of the RSSI.
443
- Returns a `Promise` object.
444
+ Returns a `Promise` object resolving with the updated RSSI value (`number`) if it succeeds.
444
445
 
445
446
  **Arguments**
446
447
 
@@ -463,7 +464,7 @@ BleManager.readRSSI("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
463
464
  ### requestConnectionPriority(peripheralId, connectionPriority) [Android only API 21+]
464
465
 
465
466
  Request a connection parameter update.
466
- Returns a `Promise` object.
467
+ Returns a `Promise` object which fulfills with the status of the request.
467
468
 
468
469
  **Arguments**
469
470
 
@@ -707,7 +708,7 @@ The BLE change state.
707
708
 
708
709
  **Arguments**
709
710
 
710
- - `state` - `String` - the new BLE state ('on'/'off').
711
+ - `state` - `String` - the new BLE state. can be one of `unknown` (iOS only), `resetting` (iOS only), `unsupported`, `unauthorized` (iOS only), `on`, `off`, `turning_on` (android only), `turning_off` (android only).
711
712
 
712
713
  **Examples**
713
714
 
@@ -470,8 +470,19 @@ class BleManager extends ReactContextBaseJavaModule {
470
470
  case BluetoothAdapter.STATE_ON:
471
471
  state = "on";
472
472
  break;
473
+ case BluetoothAdapter.STATE_TURNING_ON:
474
+ state = "turning_on";
475
+ break;
473
476
  case BluetoothAdapter.STATE_OFF:
474
477
  state = "off";
478
+ break;
479
+ case BluetoothAdapter.STATE_TURNING_OFF:
480
+ state = "turning_off";
481
+ break;
482
+ default:
483
+ // should not happen as per https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getState()
484
+ state = "off";
485
+ break;
475
486
  }
476
487
  }
477
488
 
@@ -512,6 +523,10 @@ class BleManager extends ReactContextBaseJavaModule {
512
523
  case BluetoothAdapter.STATE_TURNING_ON:
513
524
  stringState = "turning_on";
514
525
  break;
526
+ default:
527
+ // should not happen as per https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
528
+ stringState = "off";
529
+ break;
515
530
  }
516
531
 
517
532
  WritableMap map = Arguments.createMap();
@@ -3,94 +3,95 @@ package it.innove;
3
3
  import android.bluetooth.BluetoothAdapter;
4
4
  import android.bluetooth.BluetoothDevice;
5
5
  import android.util.Log;
6
+
6
7
  import com.facebook.react.bridge.*;
7
8
 
8
9
  import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
9
10
 
10
11
  public class LegacyScanManager extends ScanManager {
11
12
 
12
- public LegacyScanManager(ReactApplicationContext reactContext, BleManager bleManager) {
13
- super(reactContext, bleManager);
14
- }
15
-
16
- @Override
17
- public void stopScan(Callback callback) {
18
- // update scanSessionId to prevent stopping next scan by running timeout thread
19
- scanSessionId.incrementAndGet();
20
-
21
- getBluetoothAdapter().stopLeScan(mLeScanCallback);
22
- callback.invoke();
23
- }
24
-
25
- private BluetoothAdapter.LeScanCallback mLeScanCallback =
26
- new BluetoothAdapter.LeScanCallback() {
27
-
28
- @Override
29
- public void onLeScan(final BluetoothDevice device, final int rssi,
30
- final byte[] scanRecord) {
31
- runOnUiThread(new Runnable() {
32
- @Override
33
- public void run() {
34
- Log.i(bleManager.LOG_TAG, "DiscoverPeripheral: " + device.getName());
35
-
36
- Peripheral peripheral = bleManager.getPeripheral(device);
37
- if (peripheral == null) {
38
- peripheral = new Peripheral(device, rssi, scanRecord, bleManager.getReactContext());
39
- } else {
40
- peripheral.updateData(scanRecord);
41
- peripheral.updateRssi(rssi);
42
- }
43
- bleManager.savePeripheral(peripheral);
44
-
45
- WritableMap map = peripheral.asWritableMap();
46
- bleManager.sendEvent("BleManagerDiscoverPeripheral", map);
47
- }
48
- });
49
- }
50
-
51
-
52
- };
53
-
54
- @Override
55
- public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
56
- if (serviceUUIDs.size() > 0) {
57
- Log.d(bleManager.LOG_TAG, "Filter is not working in pre-lollipop devices");
58
- }
59
- getBluetoothAdapter().startLeScan(mLeScanCallback);
60
-
61
- if (scanSeconds > 0) {
62
- Thread thread = new Thread() {
63
- private int currentScanSession = scanSessionId.incrementAndGet();
64
-
65
- @Override
66
- public void run() {
67
-
68
- try {
69
- Thread.sleep(scanSeconds * 1000);
70
- } catch (InterruptedException ignored) {
71
- }
72
-
73
- runOnUiThread(new Runnable() {
74
- @Override
75
- public void run() {
76
- BluetoothAdapter btAdapter = getBluetoothAdapter();
77
- // check current scan session was not stopped
78
- if (scanSessionId.intValue() == currentScanSession) {
79
- if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
80
- btAdapter.stopLeScan(mLeScanCallback);
81
- }
82
- WritableMap map = Arguments.createMap();
83
- map.putInt("status", 0);
84
- bleManager.sendEvent("BleManagerStopScan", map);
85
- }
86
- }
87
- });
88
-
89
- }
90
-
91
- };
92
- thread.start();
93
- }
94
- callback.invoke();
95
- }
13
+ public LegacyScanManager(ReactApplicationContext reactContext, BleManager bleManager) {
14
+ super(reactContext, bleManager);
15
+ }
16
+
17
+ @Override
18
+ public void stopScan(Callback callback) {
19
+ // update scanSessionId to prevent stopping next scan by running timeout thread
20
+ scanSessionId.incrementAndGet();
21
+
22
+ getBluetoothAdapter().stopLeScan(mLeScanCallback);
23
+ callback.invoke();
24
+ }
25
+
26
+ private BluetoothAdapter.LeScanCallback mLeScanCallback =
27
+ new BluetoothAdapter.LeScanCallback() {
28
+
29
+ @Override
30
+ public void onLeScan(final BluetoothDevice device, final int rssi,
31
+ final byte[] scanRecord) {
32
+ runOnUiThread(new Runnable() {
33
+ @Override
34
+ public void run() {
35
+ Log.i(bleManager.LOG_TAG, "DiscoverPeripheral: " + device.getName());
36
+
37
+ Peripheral peripheral = bleManager.getPeripheral(device);
38
+ if (peripheral == null) {
39
+ peripheral = new Peripheral(device, rssi, scanRecord, bleManager.getReactContext());
40
+ } else {
41
+ peripheral.updateData(scanRecord);
42
+ peripheral.updateRssi(rssi);
43
+ }
44
+ bleManager.savePeripheral(peripheral);
45
+
46
+ WritableMap map = peripheral.asWritableMap();
47
+ bleManager.sendEvent("BleManagerDiscoverPeripheral", map);
48
+ }
49
+ });
50
+ }
51
+
52
+
53
+ };
54
+
55
+ @Override
56
+ public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
57
+ if (serviceUUIDs.size() > 0) {
58
+ Log.d(bleManager.LOG_TAG, "Filter is not working in pre-lollipop devices");
59
+ }
60
+ getBluetoothAdapter().startLeScan(mLeScanCallback);
61
+
62
+ if (scanSeconds > 0) {
63
+ Thread thread = new Thread() {
64
+ private int currentScanSession = scanSessionId.incrementAndGet();
65
+
66
+ @Override
67
+ public void run() {
68
+
69
+ try {
70
+ Thread.sleep(scanSeconds * 1000);
71
+ } catch (InterruptedException ignored) {
72
+ }
73
+
74
+ runOnUiThread(new Runnable() {
75
+ @Override
76
+ public void run() {
77
+ BluetoothAdapter btAdapter = getBluetoothAdapter();
78
+ // check current scan session was not stopped
79
+ if (scanSessionId.intValue() == currentScanSession) {
80
+ if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
81
+ btAdapter.stopLeScan(mLeScanCallback);
82
+ }
83
+ WritableMap map = Arguments.createMap();
84
+ map.putInt("status", 0);
85
+ bleManager.sendEvent("BleManagerStopScan", map);
86
+ }
87
+ }
88
+ });
89
+
90
+ }
91
+
92
+ };
93
+ thread.start();
94
+ }
95
+ callback.invoke();
96
+ }
96
97
  }
@@ -1,52 +1,63 @@
1
1
  package it.innove;
2
2
 
3
3
 
4
+ import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
5
+
6
+ import android.Manifest;
4
7
  import android.bluetooth.BluetoothAdapter;
5
8
  import android.bluetooth.BluetoothDevice;
6
9
  import android.bluetooth.le.ScanCallback;
7
10
  import android.bluetooth.le.ScanFilter;
11
+ import android.bluetooth.le.ScanRecord;
8
12
  import android.bluetooth.le.ScanResult;
9
13
  import android.bluetooth.le.ScanSettings;
14
+ import android.content.pm.PackageManager;
10
15
  import android.os.Build;
11
16
  import android.os.ParcelUuid;
12
- import androidx.annotation.RequiresApi;
13
17
  import android.util.Log;
14
- import com.facebook.react.bridge.*;
18
+
19
+ import androidx.annotation.RequiresApi;
20
+ import androidx.core.app.ActivityCompat;
21
+
22
+ import com.facebook.react.bridge.Arguments;
23
+ import com.facebook.react.bridge.Callback;
24
+ import com.facebook.react.bridge.ReactApplicationContext;
25
+ import com.facebook.react.bridge.ReadableArray;
26
+ import com.facebook.react.bridge.ReadableMap;
27
+ import com.facebook.react.bridge.WritableMap;
15
28
 
16
29
  import java.util.ArrayList;
17
30
  import java.util.List;
18
31
 
19
- import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
20
-
21
32
  @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
22
33
  public class LollipopScanManager extends ScanManager {
23
34
 
24
- public LollipopScanManager(ReactApplicationContext reactContext, BleManager bleManager) {
25
- super(reactContext, bleManager);
26
- }
35
+ public LollipopScanManager(ReactApplicationContext reactContext, BleManager bleManager) {
36
+ super(reactContext, bleManager);
37
+ }
27
38
 
28
- @Override
29
- public void stopScan(Callback callback) {
30
- // update scanSessionId to prevent stopping next scan by running timeout thread
31
- scanSessionId.incrementAndGet();
39
+ @Override
40
+ public void stopScan(Callback callback) {
41
+ // update scanSessionId to prevent stopping next scan by running timeout thread
42
+ scanSessionId.incrementAndGet();
32
43
 
33
- getBluetoothAdapter().getBluetoothLeScanner().stopScan(mScanCallback);
34
- callback.invoke();
35
- }
44
+ getBluetoothAdapter().getBluetoothLeScanner().stopScan(mScanCallback);
45
+ callback.invoke();
46
+ }
36
47
 
37
48
  @Override
38
- public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
49
+ public void scan(ReadableArray serviceUUIDs, final int scanSeconds, ReadableMap options, Callback callback) {
39
50
  ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
40
51
  List<ScanFilter> filters = new ArrayList<>();
41
52
 
42
53
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && options.hasKey("legacy")) {
43
54
  scanSettingsBuilder.setLegacy(options.getBoolean("legacy"));
44
55
  }
45
-
56
+
46
57
  if (options.hasKey("scanMode")) {
47
58
  scanSettingsBuilder.setScanMode(options.getInt("scanMode"));
48
59
  }
49
-
60
+
50
61
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
51
62
  if (options.hasKey("numberOfMatches")) {
52
63
  scanSettingsBuilder.setNumOfMatches(options.getInt("numberOfMatches"));
@@ -54,6 +65,9 @@ public class LollipopScanManager extends ScanManager {
54
65
  if (options.hasKey("matchMode")) {
55
66
  scanSettingsBuilder.setMatchMode(options.getInt("matchMode"));
56
67
  }
68
+ if (options.hasKey("callbackType")) {
69
+ scanSettingsBuilder.setCallbackType(options.getInt("callbackType"));
70
+ }
57
71
  }
58
72
 
59
73
  if (options.hasKey("reportDelay")) {
@@ -69,60 +83,69 @@ public class LollipopScanManager extends ScanManager {
69
83
  scanSettingsBuilder.setPhy(BluetoothDevice.PHY_LE_2M);
70
84
  }
71
85
  }
72
-
86
+
73
87
  if (serviceUUIDs.size() > 0) {
74
- for(int i = 0; i < serviceUUIDs.size(); i++){
75
- ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUIDHelper.uuidFromString(serviceUUIDs.getString(i)))).build();
88
+ for (int i = 0; i < serviceUUIDs.size(); i++) {
89
+ ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUIDHelper.uuidFromString(serviceUUIDs.getString(i)))).build();
76
90
  filters.add(filter);
77
91
  Log.d(bleManager.LOG_TAG, "Filter service: " + serviceUUIDs.getString(i));
78
92
  }
79
93
  }
80
-
94
+
81
95
  getBluetoothAdapter().getBluetoothLeScanner().startScan(filters, scanSettingsBuilder.build(), mScanCallback);
82
96
  if (scanSeconds > 0) {
83
97
  Thread thread = new Thread() {
84
98
  private int currentScanSession = scanSessionId.incrementAndGet();
85
-
99
+
86
100
  @Override
87
101
  public void run() {
88
-
102
+
89
103
  try {
90
104
  Thread.sleep(scanSeconds * 1000);
91
105
  } catch (InterruptedException ignored) {
92
106
  }
93
-
107
+
94
108
  runOnUiThread(new Runnable() {
95
109
  @Override
96
110
  public void run() {
97
111
  BluetoothAdapter btAdapter = getBluetoothAdapter();
98
112
  // check current scan session was not stopped
99
113
  if (scanSessionId.intValue() == currentScanSession) {
100
- if(btAdapter.getState() == BluetoothAdapter.STATE_ON) {
114
+ if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
101
115
  btAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
102
116
  }
103
117
  WritableMap map = Arguments.createMap();
104
- map.putInt("status", 10);
118
+ map.putInt("status", 10);
105
119
  bleManager.sendEvent("BleManagerStopScan", map);
106
120
  }
107
121
  }
108
122
  });
109
-
123
+
110
124
  }
111
-
125
+
112
126
  };
113
127
  thread.start();
114
128
  }
115
129
  callback.invoke();
116
130
  }
117
131
 
118
- private ScanCallback mScanCallback = new ScanCallback() {
119
- @Override
120
- public void onScanResult(final int callbackType, final ScanResult result) {
132
+ private ScanCallback mScanCallback = new ScanCallback() {
133
+ @Override
134
+ public void onScanResult(final int callbackType, final ScanResult result) {
121
135
 
122
- runOnUiThread(new Runnable() {
123
- @Override
124
- public void run() {
125
- Log.i(bleManager.LOG_TAG, "DiscoverPeripheral: " + result.getDevice().getName());
136
+ runOnUiThread(new Runnable() {
137
+ @Override
138
+ public void run() {
139
+ String info;
140
+ ScanRecord record = result.getScanRecord();
141
+ if (record != null)
142
+ info = record.getDeviceName();
143
+ else if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED)
144
+ info = result.getDevice().getName();
145
+ else
146
+ info = result.toString();
147
+
148
+ Log.i(bleManager.LOG_TAG, "DiscoverPeripheral: " + info);
126
149
 
127
150
  LollipopPeripheral peripheral = (LollipopPeripheral) bleManager.getPeripheral(result.getDevice());
128
151
  if (peripheral == null) {
@@ -133,21 +156,21 @@ public class LollipopScanManager extends ScanManager {
133
156
  }
134
157
  bleManager.savePeripheral(peripheral);
135
158
 
136
- WritableMap map = peripheral.asWritableMap();
137
- bleManager.sendEvent("BleManagerDiscoverPeripheral", map);
138
- }
139
- });
140
- }
159
+ WritableMap map = peripheral.asWritableMap();
160
+ bleManager.sendEvent("BleManagerDiscoverPeripheral", map);
161
+ }
162
+ });
163
+ }
141
164
 
142
- @Override
143
- public void onBatchScanResults(final List<ScanResult> results) {
144
- }
165
+ @Override
166
+ public void onBatchScanResults(final List<ScanResult> results) {
167
+ }
145
168
 
146
- @Override
147
- public void onScanFailed(final int errorCode) {
169
+ @Override
170
+ public void onScanFailed(final int errorCode) {
148
171
  WritableMap map = Arguments.createMap();
149
- map.putInt("status", errorCode);
172
+ map.putInt("status", errorCode);
150
173
  bleManager.sendEvent("BleManagerStopScan", map);
151
- }
152
- };
174
+ }
175
+ };
153
176
  }
package/index.d.ts CHANGED
@@ -1,4 +1,18 @@
1
1
  declare module "react-native-ble-manager" {
2
+
3
+ // android states: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE
4
+ // ios states: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerstate
5
+ export enum BleState {
6
+ Unknown = 'unknown', // [iOS only]
7
+ Resetting = 'resetting', // [iOS only]
8
+ Unsupported = 'unsupported',
9
+ Unauthorized = 'unauthorized', // [iOS only]
10
+ On = 'on',
11
+ Off = 'off',
12
+ TurningOn = 'turning_on', // [android only]
13
+ TurningOff = 'turning_off', // [android only]
14
+ }
15
+
2
16
  export interface Peripheral {
3
17
  id: string;
4
18
  rssi: number;
@@ -9,29 +23,75 @@ declare module "react-native-ble-manager" {
9
23
  export interface AdvertisingData {
10
24
  isConnectable?: boolean;
11
25
  localName?: string;
12
- manufacturerData?: any;
26
+ manufacturerData?: CustomAdvertisingData,
27
+ serviceData?: CustomAdvertisingData,
13
28
  serviceUUIDs?: string[];
14
29
  txPowerLevel?: number;
15
30
  }
16
31
 
32
+ export interface CustomAdvertisingData {
33
+ CDVType : 'ArrayBuffer',
34
+ bytes: Uint8Array,
35
+ data: string // base64-encoded string of the data
36
+ }
37
+
17
38
  export interface StartOptions {
18
- showAlert?: boolean;
19
- restoreIdentifierKey?: string;
20
- queueIdentifierKey?: string;
21
- forceLegacy?: boolean;
39
+ showAlert?: boolean; // [iOS only]
40
+ restoreIdentifierKey?: string; // [iOS only]
41
+ queueIdentifierKey?: string; // [iOS only]
42
+ forceLegacy?: boolean; // [android only]
22
43
  }
23
44
 
24
45
  export function start(options?: StartOptions): Promise<void>;
25
46
 
47
+ // [android only]
48
+ // https://developer.android.com/reference/android/bluetooth/le/ScanSettings
26
49
  export interface ScanOptions {
27
- numberOfMatches?: number;
28
- matchMode?: number;
29
- scanMode?: number;
50
+ numberOfMatches?: BleScanMatchCount;
51
+ matchMode?: BleScanMatchMode;
52
+ callbackType?: BleScanCallbackType;
53
+ scanMode?: BleScanMode;
30
54
  reportDelay?: number;
31
- phy?: number;
55
+ phy?: BleScanPhyMode;
32
56
  legacy?: boolean;
33
57
  }
34
58
 
59
+ // [android only]
60
+ export enum BleScanMode {
61
+ Opportunistic = -1,
62
+ LowPower = 0,
63
+ Balanced = 1,
64
+ LowLatency = 2,
65
+ }
66
+
67
+ // [android only]
68
+ export enum BleScanMatchMode {
69
+ Aggressive = 1,
70
+ Sticky = 2,
71
+ }
72
+
73
+ // [android only]
74
+ export enum BleScanCallbackType {
75
+ AllMatches = 1,
76
+ FirstMatch = 2,
77
+ MatchLost = 4,
78
+ }
79
+
80
+ // [android only]
81
+ export enum BleScanMatchCount {
82
+ OneAdvertisement = 1,
83
+ FewAdvertisements = 2,
84
+ MaxAdvertisements = 3,
85
+ }
86
+
87
+ // [android only]
88
+ export enum BleScanPhyMode {
89
+ LE_1M = 1,
90
+ LE_2M = 2,
91
+ LE_CODED = 3,
92
+ ALL_SUPPORTED = 255,
93
+ }
94
+
35
95
  export function scan(
36
96
  serviceUUIDs: string[],
37
97
  seconds: number,
@@ -44,14 +104,16 @@ declare module "react-native-ble-manager" {
44
104
  peripheralID: string,
45
105
  force?: boolean
46
106
  ): Promise<void>;
107
+
47
108
  export function checkState(): void;
109
+
48
110
  export function startNotification(
49
111
  peripheralID: string,
50
112
  serviceUUID: string,
51
113
  characteristicUUID: string
52
114
  ): Promise<void>;
53
115
 
54
- /// Android only
116
+ // [android only]
55
117
  export function startNotificationUseBuffer(
56
118
  peripheralID: string,
57
119
  serviceUUID: string,
@@ -70,6 +132,7 @@ declare module "react-native-ble-manager" {
70
132
  serviceUUID: string,
71
133
  characteristicUUID: string
72
134
  ): Promise<any>;
135
+
73
136
  export function write(
74
137
  peripheralID: string,
75
138
  serviceUUID: string,
@@ -77,6 +140,7 @@ declare module "react-native-ble-manager" {
77
140
  data: any,
78
141
  maxByteSize?: number
79
142
  ): Promise<void>;
143
+
80
144
  export function writeWithoutResponse(
81
145
  peripheralID: string,
82
146
  serviceUUID: string,
@@ -86,12 +150,14 @@ declare module "react-native-ble-manager" {
86
150
  queueSleepTime?: number
87
151
  ): Promise<void>;
88
152
 
89
- export function readRSSI(peripheralID: string): Promise<void>;
153
+ export function readRSSI(peripheralID: string): Promise<number>;
90
154
 
91
155
  export function getConnectedPeripherals(
92
156
  serviceUUIDs: string[]
93
157
  ): Promise<Peripheral[]>;
158
+
94
159
  export function getDiscoveredPeripherals(): Promise<Peripheral[]>;
160
+
95
161
  export function isPeripheralConnected(
96
162
  peripheralID: string,
97
163
  serviceUUIDs: string[]
@@ -103,14 +169,19 @@ declare module "react-native-ble-manager" {
103
169
  high = 1,
104
170
  low = 2,
105
171
  }
172
+
173
+ // [Android only 21+]
106
174
  export function requestConnectionPriority(
107
175
  peripheralID: string,
108
176
  connectionPriority: ConnectionPriority
109
- ): Promise<void>;
110
- /// Android only
177
+ ): Promise<boolean>;
178
+
179
+ // [Android only]
111
180
  export function enableBluetooth(): Promise<void>;
181
+
112
182
  // [Android only]
113
183
  export function refreshCache(peripheralID: string): Promise<void>;
184
+
114
185
  // [Android only API 21+]
115
186
  export function requestMTU(peripheralID: string, mtu: number): Promise<number>;
116
187
 
@@ -118,8 +189,11 @@ declare module "react-native-ble-manager" {
118
189
  peripheralID: string,
119
190
  peripheralPin?: string
120
191
  ): Promise<void>;
192
+
121
193
  export function removeBond(peripheralID: string): Promise<void>;
194
+
122
195
  export function getBondedPeripherals(): Promise<Peripheral[]>;
196
+
123
197
  export function removePeripheral(peripheralID: string): Promise<void>;
124
198
 
125
199
  // [Android only]
@@ -164,4 +238,57 @@ declare module "react-native-ble-manager" {
164
238
  peripheralID: string,
165
239
  serviceUUIDs?: string[]
166
240
  ): Promise<PeripheralInfo>;
241
+
242
+ export enum BleEventType {
243
+ BleManagerDidUpdateState = 'BleManagerDidUpdateState',
244
+ BleManagerStopScan = 'BleManagerStopScan',
245
+ BleManagerDiscoverPeripheral = 'BleManagerDiscoverPeripheral',
246
+ BleManagerDidUpdateValueForCharacteristic = 'BleManagerDidUpdateValueForCharacteristic',
247
+ BleManagerConnectPeripheral = 'BleManagerConnectPeripheral',
248
+ BleManagerDisconnectPeripheral = 'BleManagerDisconnectPeripheral',
249
+ BleManagerPeripheralDidBond = 'BleManagerPeripheralDidBond', // [Android only]
250
+ BleManagerCentralManagerWillRestoreState = 'BleManagerCentralManagerWillRestoreState', // [iOS only]
251
+ BleManagerDidUpdateNotificationStateFor = 'BleManagerDidUpdateNotificationStateFor', // [iOS only]
252
+ }
253
+
254
+ export interface BleStopScanEvent {
255
+ status?: number; // [iOS only]
256
+ }
257
+
258
+ export interface BleManagerDidUpdateStateEvent {
259
+ state: BleState;
260
+ }
261
+
262
+ export interface BleConnectPeripheralEvent {
263
+ readonly peripheral: string; // peripheral id
264
+ readonly status?: number; // [android only]
265
+ }
266
+
267
+ export type BleDiscoverPeripheralEvent = Peripheral;
268
+
269
+ // [Android only]
270
+ export type BleBondedPeripheralEvent = Peripheral;
271
+
272
+ export interface BleDisconnectPeripheralEvent {
273
+ readonly peripheral: string; // peripheral id
274
+ readonly status?: number; // [android only] disconnect reason.
275
+ readonly domain?: string; // [iOS only] disconnect error domain.
276
+ readonly code?: number; // [iOS only] disconnect error code.
277
+ }
278
+
279
+ export interface BleManagerDidUpdateValueForCharacteristicEvent {
280
+ readonly characteristic: string; // characteristic UUID
281
+ readonly peripheral: string; // peripheral id
282
+ readonly service: string; // service UUID
283
+ readonly value: Uint8Array;
284
+ }
285
+
286
+ // [iOS only]
287
+ export interface BleManagerDidUpdateNotificationStateForEvent {
288
+ readonly peripheral: string; // peripheral id
289
+ readonly characteristic: string; // characteristic UUID
290
+ readonly isNotifying: boolean; // is the characteristic notifying or not
291
+ readonly domain: string; // error domain.
292
+ readonly code: number; // error code.
293
+ }
167
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ble-manager",
3
- "version": "8.6.0",
3
+ "version": "8.8.0",
4
4
  "description": "A BLE module for react native.",
5
5
  "main": "BleManager",
6
6
  "types": "./index.d.ts",