react-native-ble-manager 11.0.8 → 11.2.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/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NativeEventEmitter, NativeModules } from 'react-native';
1
+ import { NativeEventEmitter, NativeModules } from "react-native";
2
2
  import {
3
3
  BleScanCallbackType,
4
4
  BleScanMatchCount,
@@ -10,10 +10,10 @@ import {
10
10
  Peripheral,
11
11
  PeripheralInfo,
12
12
  ScanOptions,
13
- StartOptions
14
- } from './types';
13
+ StartOptions,
14
+ } from "./types";
15
15
 
16
- export * from './types';
16
+ export * from "./types";
17
17
 
18
18
  let bleManager = NativeModules.BleManager;
19
19
 
@@ -24,10 +24,10 @@ class BleManager extends NativeEventEmitter {
24
24
  }
25
25
 
26
26
  /**
27
- *
28
- * @param peripheralId
29
- * @param serviceUUID
30
- * @param characteristicUUID
27
+ *
28
+ * @param peripheralId
29
+ * @param serviceUUID
30
+ * @param characteristicUUID
31
31
  * @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
32
32
  */
33
33
  read(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
@@ -48,14 +48,19 @@ class BleManager extends NativeEventEmitter {
48
48
  }
49
49
 
50
50
  /**
51
- *
52
- * @param peripheralId
53
- * @param serviceUUID
54
- * @param characteristicUUID
51
+ *
52
+ * @param peripheralId
53
+ * @param serviceUUID
54
+ * @param characteristicUUID
55
55
  * @param descriptorUUID
56
56
  * @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
57
57
  */
58
- readDescriptor(peripheralId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string) {
58
+ readDescriptor(
59
+ peripheralId: string,
60
+ serviceUUID: string,
61
+ characteristicUUID: string,
62
+ descriptorUUID: string
63
+ ) {
59
64
  return new Promise<number[]>((fulfill, reject) => {
60
65
  bleManager.readDescriptor(
61
66
  peripheralId,
@@ -74,44 +79,84 @@ class BleManager extends NativeEventEmitter {
74
79
  }
75
80
 
76
81
  /**
77
- *
78
- * @param peripheralId
82
+ *
83
+ * @param peripheralId
84
+ * @param serviceUUID
85
+ * @param characteristicUUID
86
+ * @param descriptorUUID
87
+ * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
88
+ * @returns
89
+ */
90
+ writeDescriptor(
91
+ peripheralId: string,
92
+ serviceUUID: string,
93
+ characteristicUUID: string,
94
+ descriptorUUID: string,
95
+ data: number[]
96
+ ) {
97
+ return new Promise<void>((fulfill, reject) => {
98
+ bleManager.writeDescriptor(
99
+ peripheralId,
100
+ serviceUUID,
101
+ characteristicUUID,
102
+ descriptorUUID,
103
+ data,
104
+ (error: string | null) => {
105
+ if (error) {
106
+ reject(error);
107
+ } else {
108
+ fulfill();
109
+ }
110
+ }
111
+ );
112
+ });
113
+ }
114
+
115
+ /**
116
+ *
117
+ * @param peripheralId
79
118
  * @returns a promise resolving with the updated RSSI (`number`) if it succeeds.
80
119
  */
81
120
  readRSSI(peripheralId: string) {
82
121
  return new Promise<number>((fulfill, reject) => {
83
- bleManager.readRSSI(peripheralId, (error: string | null, rssi: number) => {
84
- if (error) {
85
- reject(error);
86
- } else {
87
- fulfill(rssi);
122
+ bleManager.readRSSI(
123
+ peripheralId,
124
+ (error: string | null, rssi: number) => {
125
+ if (error) {
126
+ reject(error);
127
+ } else {
128
+ fulfill(rssi);
129
+ }
88
130
  }
89
- });
131
+ );
90
132
  });
91
133
  }
92
134
 
93
135
  /**
94
136
  * [Android only]
95
- * @param peripheralId
137
+ * @param peripheralId
96
138
  * @returns a promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
97
139
  */
98
140
  refreshCache(peripheralId: string) {
99
141
  return new Promise<boolean>((fulfill, reject) => {
100
- bleManager.refreshCache(peripheralId, (error: string | null, result: boolean) => {
101
- if (error) {
102
- reject(error);
103
- } else {
104
- fulfill(result);
142
+ bleManager.refreshCache(
143
+ peripheralId,
144
+ (error: string | null, result: boolean) => {
145
+ if (error) {
146
+ reject(error);
147
+ } else {
148
+ fulfill(result);
149
+ }
105
150
  }
106
- });
151
+ );
107
152
  });
108
153
  }
109
154
 
110
155
  /**
111
- *
112
- * @param peripheralId
156
+ *
157
+ * @param peripheralId
113
158
  * @param serviceUUIDs [iOS only] optional filter of services to retrieve.
114
- * @returns
159
+ * @returns
115
160
  */
116
161
  retrieveServices(peripheralId: string, serviceUUIDs: string[] = []) {
117
162
  return new Promise<PeripheralInfo>((fulfill, reject) => {
@@ -130,13 +175,13 @@ class BleManager extends NativeEventEmitter {
130
175
  }
131
176
 
132
177
  /**
133
- *
134
- * @param peripheralId
135
- * @param serviceUUID
136
- * @param characteristicUUID
178
+ *
179
+ * @param peripheralId
180
+ * @param serviceUUID
181
+ * @param characteristicUUID
137
182
  * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
138
183
  * @param maxByteSize optional, defaults to 20
139
- * @returns
184
+ * @returns
140
185
  */
141
186
  write(
142
187
  peripheralId: string,
@@ -145,7 +190,6 @@ class BleManager extends NativeEventEmitter {
145
190
  data: number[],
146
191
  maxByteSize: number = 20
147
192
  ) {
148
-
149
193
  return new Promise<void>((fulfill, reject) => {
150
194
  bleManager.write(
151
195
  peripheralId,
@@ -165,14 +209,14 @@ class BleManager extends NativeEventEmitter {
165
209
  }
166
210
 
167
211
  /**
168
- *
169
- * @param peripheralId
170
- * @param serviceUUID
171
- * @param characteristicUUID
212
+ *
213
+ * @param peripheralId
214
+ * @param serviceUUID
215
+ * @param characteristicUUID
172
216
  * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
173
217
  * @param maxByteSize optional, defaults to 20
174
218
  * @param queueSleepTime optional, defaults to 10. Only useful if data length is greater than maxByteSize.
175
- * @returns
219
+ * @returns
176
220
  */
177
221
  writeWithoutResponse(
178
222
  peripheralId: string,
@@ -182,7 +226,6 @@ class BleManager extends NativeEventEmitter {
182
226
  maxByteSize: number = 20,
183
227
  queueSleepTime: number = 10
184
228
  ) {
185
-
186
229
  return new Promise<void>((fulfill, reject) => {
187
230
  bleManager.writeWithoutResponse(
188
231
  peripheralId,
@@ -219,26 +262,30 @@ class BleManager extends NativeEventEmitter {
219
262
 
220
263
  /**
221
264
  * [Android only]
222
- * @param peripheralId
265
+ * @param peripheralId
223
266
  * @param peripheralPin optional. will be used to auto-bond if possible.
224
- * @returns
267
+ * @returns
225
268
  */
226
269
  createBond(peripheralId: string, peripheralPin: string | null = null) {
227
270
  return new Promise<void>((fulfill, reject) => {
228
- bleManager.createBond(peripheralId, peripheralPin, (error: string | null) => {
229
- if (error) {
230
- reject(error);
231
- } else {
232
- fulfill();
271
+ bleManager.createBond(
272
+ peripheralId,
273
+ peripheralPin,
274
+ (error: string | null) => {
275
+ if (error) {
276
+ reject(error);
277
+ } else {
278
+ fulfill();
279
+ }
233
280
  }
234
- });
281
+ );
235
282
  });
236
283
  }
237
284
 
238
285
  /**
239
286
  * [Android only]
240
- * @param peripheralId
241
- * @returns
287
+ * @param peripheralId
288
+ * @returns
242
289
  */
243
290
  removeBond(peripheralId: string) {
244
291
  return new Promise<void>((fulfill, reject) => {
@@ -253,10 +300,10 @@ class BleManager extends NativeEventEmitter {
253
300
  }
254
301
 
255
302
  /**
256
- *
257
- * @param peripheralId
303
+ *
304
+ * @param peripheralId
258
305
  * @param force [Android only] defaults to true.
259
- * @returns
306
+ * @returns
260
307
  */
261
308
  disconnect(peripheralId: string, force: boolean = true) {
262
309
  return new Promise<void>((fulfill, reject) => {
@@ -270,7 +317,11 @@ class BleManager extends NativeEventEmitter {
270
317
  });
271
318
  }
272
319
 
273
- startNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
320
+ startNotification(
321
+ peripheralId: string,
322
+ serviceUUID: string,
323
+ characteristicUUID: string
324
+ ) {
274
325
  return new Promise<void>((fulfill, reject) => {
275
326
  bleManager.startNotification(
276
327
  peripheralId,
@@ -289,11 +340,11 @@ class BleManager extends NativeEventEmitter {
289
340
 
290
341
  /**
291
342
  * [Android only]
292
- * @param peripheralId
293
- * @param serviceUUID
294
- * @param characteristicUUID
295
- * @param buffer
296
- * @returns
343
+ * @param peripheralId
344
+ * @param serviceUUID
345
+ * @param characteristicUUID
346
+ * @param buffer
347
+ * @returns
297
348
  */
298
349
  startNotificationUseBuffer(
299
350
  peripheralId: string,
@@ -318,7 +369,11 @@ class BleManager extends NativeEventEmitter {
318
369
  });
319
370
  }
320
371
 
321
- stopNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
372
+ stopNotification(
373
+ peripheralId: string,
374
+ serviceUUID: string,
375
+ characteristicUUID: string
376
+ ) {
322
377
  return new Promise<void>((fulfill, reject) => {
323
378
  bleManager.stopNotification(
324
379
  peripheralId,
@@ -359,12 +414,12 @@ class BleManager extends NativeEventEmitter {
359
414
  }
360
415
 
361
416
  /**
362
- *
363
- * @param serviceUUIDs
417
+ *
418
+ * @param serviceUUIDs
364
419
  * @param seconds amount of seconds to scan. if set to 0 or less, will scan until you call stopScan() or the OS stops the scan (background etc).
365
420
  * @param allowDuplicates [iOS only]
366
- * @param scanningOptions [Android only] optional map of properties to fine-tune scan behavior on android, see README.
367
- * @returns
421
+ * @param scanningOptions optional map of properties to fine-tune scan behavior, see DOCS.
422
+ * @returns
368
423
  */
369
424
  scan(
370
425
  serviceUUIDs: string[],
@@ -372,7 +427,6 @@ class BleManager extends NativeEventEmitter {
372
427
  allowDuplicates?: boolean,
373
428
  scanningOptions: ScanOptions = {}
374
429
  ) {
375
-
376
430
  return new Promise<void>((fulfill, reject) => {
377
431
  if (allowDuplicates == null) {
378
432
  allowDuplicates = false;
@@ -394,7 +448,7 @@ class BleManager extends NativeEventEmitter {
394
448
  scanningOptions.scanMode = BleScanMode.LowPower;
395
449
  }
396
450
 
397
- // (ANDROID) Defaults to CALLBACK_TYPE_ALL_MATCHES
451
+ // (ANDROID) Defaults to CALLBACK_TYPE_ALL_MATCHES
398
452
  // WARN: sometimes, setting a scanSetting instead of leaving it untouched might result in unexpected behaviors.
399
453
  // https://github.com/dariuszseweryn/RxAndroidBle/issues/462
400
454
  if (scanningOptions.callbackType == null) {
@@ -412,8 +466,10 @@ class BleManager extends NativeEventEmitter {
412
466
  if (!scanningOptions.exactAdvertisingName) {
413
467
  delete scanningOptions.exactAdvertisingName;
414
468
  } else {
415
- if (typeof scanningOptions.exactAdvertisingName === 'string') {
416
- scanningOptions.exactAdvertisingName = [scanningOptions.exactAdvertisingName];
469
+ if (typeof scanningOptions.exactAdvertisingName === "string") {
470
+ scanningOptions.exactAdvertisingName = [
471
+ scanningOptions.exactAdvertisingName,
472
+ ];
417
473
  }
418
474
  }
419
475
 
@@ -447,7 +503,7 @@ class BleManager extends NativeEventEmitter {
447
503
 
448
504
  /**
449
505
  * [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
450
- * @returns
506
+ * @returns
451
507
  */
452
508
  enableBluetooth() {
453
509
  return new Promise<void>((fulfill, reject) => {
@@ -462,67 +518,73 @@ class BleManager extends NativeEventEmitter {
462
518
  }
463
519
 
464
520
  /**
465
- *
521
+ *
466
522
  * @param serviceUUIDs [optional] not used on android, optional on ios.
467
- * @returns
523
+ * @returns
468
524
  */
469
525
  getConnectedPeripherals(serviceUUIDs: string[] = []) {
470
526
  return new Promise<Peripheral[]>((fulfill, reject) => {
471
-
472
- bleManager.getConnectedPeripherals(serviceUUIDs, (error: string | null, result: Peripheral[] | null) => {
473
- if (error) {
474
- reject(error);
475
- } else {
476
- if (result) {
477
- fulfill(result);
527
+ bleManager.getConnectedPeripherals(
528
+ serviceUUIDs,
529
+ (error: string | null, result: Peripheral[] | null) => {
530
+ if (error) {
531
+ reject(error);
478
532
  } else {
479
- fulfill([]);
533
+ if (result) {
534
+ fulfill(result);
535
+ } else {
536
+ fulfill([]);
537
+ }
480
538
  }
481
539
  }
482
- });
540
+ );
483
541
  });
484
542
  }
485
543
 
486
544
  /**
487
545
  * [Android only]
488
- * @returns
546
+ * @returns
489
547
  */
490
548
  getBondedPeripherals() {
491
549
  return new Promise<Peripheral[]>((fulfill, reject) => {
492
- bleManager.getBondedPeripherals((error: string | null, result: Peripheral[] | null) => {
493
- if (error) {
494
- reject(error);
495
- } else {
496
- if (result) {
497
- fulfill(result);
550
+ bleManager.getBondedPeripherals(
551
+ (error: string | null, result: Peripheral[] | null) => {
552
+ if (error) {
553
+ reject(error);
498
554
  } else {
499
- fulfill([]);
555
+ if (result) {
556
+ fulfill(result);
557
+ } else {
558
+ fulfill([]);
559
+ }
500
560
  }
501
561
  }
502
- });
562
+ );
503
563
  });
504
564
  }
505
565
 
506
566
  getDiscoveredPeripherals() {
507
567
  return new Promise<Peripheral[]>((fulfill, reject) => {
508
- bleManager.getDiscoveredPeripherals((error: string | null, result: Peripheral[] | null) => {
509
- if (error) {
510
- reject(error);
511
- } else {
512
- if (result) {
513
- fulfill(result);
568
+ bleManager.getDiscoveredPeripherals(
569
+ (error: string | null, result: Peripheral[] | null) => {
570
+ if (error) {
571
+ reject(error);
514
572
  } else {
515
- fulfill([]);
573
+ if (result) {
574
+ fulfill(result);
575
+ } else {
576
+ fulfill([]);
577
+ }
516
578
  }
517
579
  }
518
- });
580
+ );
519
581
  });
520
582
  }
521
583
 
522
584
  /**
523
585
  * [Android only]
524
- * @param peripheralId
525
- * @returns
586
+ * @param peripheralId
587
+ * @returns
526
588
  */
527
589
  removePeripheral(peripheralId: string) {
528
590
  return new Promise<void>((fulfill, reject) => {
@@ -537,13 +599,13 @@ class BleManager extends NativeEventEmitter {
537
599
  }
538
600
 
539
601
  /**
540
- * @param peripheralId
602
+ * @param peripheralId
541
603
  * @param serviceUUIDs [optional] not used on android, optional on ios.
542
- * @returns
604
+ * @returns
543
605
  */
544
606
  isPeripheralConnected(peripheralId: string, serviceUUIDs: string[] = []) {
545
- return this.getConnectedPeripherals(serviceUUIDs).then(result => {
546
- if (result.find(p => p.id === peripheralId)) {
607
+ return this.getConnectedPeripherals(serviceUUIDs).then((result) => {
608
+ if (result.find((p) => p.id === peripheralId)) {
547
609
  return true;
548
610
  } else {
549
611
  return false;
@@ -551,13 +613,33 @@ class BleManager extends NativeEventEmitter {
551
613
  });
552
614
  }
553
615
 
616
+ /**
617
+ * @param peripheralId
618
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
619
+ * @returns
620
+ */
621
+ isScanning() {
622
+ return new Promise<boolean>((fulfill, reject) => {
623
+ bleManager.isScanning((error: string | null, status: boolean) => {
624
+ if (error) {
625
+ reject(error);
626
+ } else {
627
+ fulfill(status);
628
+ }
629
+ });
630
+ });
631
+ }
632
+
554
633
  /**
555
634
  * [Android only, API 21+]
556
- * @param peripheralId
557
- * @param connectionPriority
635
+ * @param peripheralId
636
+ * @param connectionPriority
558
637
  * @returns a promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
559
638
  */
560
- requestConnectionPriority(peripheralId: string, connectionPriority: ConnectionPriority) {
639
+ requestConnectionPriority(
640
+ peripheralId: string,
641
+ connectionPriority: ConnectionPriority
642
+ ) {
561
643
  return new Promise<boolean>((fulfill, reject) => {
562
644
  bleManager.requestConnectionPriority(
563
645
  peripheralId,
@@ -573,7 +655,6 @@ class BleManager extends NativeEventEmitter {
573
655
  });
574
656
  }
575
657
 
576
-
577
658
  /**
578
659
  * [Android only, API 21+]
579
660
  * @param peripheralId
@@ -582,19 +663,23 @@ class BleManager extends NativeEventEmitter {
582
663
  */
583
664
  requestMTU(peripheralId: string, mtu: number) {
584
665
  return new Promise<number>((fulfill, reject) => {
585
- bleManager.requestMTU(peripheralId, mtu, (error: string | null, mtu: number) => {
586
- if (error) {
587
- reject(error);
588
- } else {
589
- fulfill(mtu);
666
+ bleManager.requestMTU(
667
+ peripheralId,
668
+ mtu,
669
+ (error: string | null, mtu: number) => {
670
+ if (error) {
671
+ reject(error);
672
+ } else {
673
+ fulfill(mtu);
674
+ }
590
675
  }
591
- });
676
+ );
592
677
  });
593
678
  }
594
679
 
595
680
  /**
596
681
  * [Android only]
597
- * @param name
682
+ * @param name
598
683
  */
599
684
  setName(name: string) {
600
685
  bleManager.setName(name);
@@ -602,40 +687,43 @@ class BleManager extends NativeEventEmitter {
602
687
 
603
688
  /**
604
689
  * [iOS only]
605
- * @param peripheralId
606
- * @returns
690
+ * @param peripheralId
691
+ * @returns
607
692
  */
608
693
  getMaximumWriteValueLengthForWithoutResponse(peripheralId: string) {
609
694
  return new Promise<number>((fulfill, reject) => {
610
- bleManager.getMaximumWriteValueLengthForWithoutResponse(peripheralId, (error: string | null, max: number) => {
611
- if (error) {
612
- reject(error);
613
- } else {
614
- fulfill(max);
695
+ bleManager.getMaximumWriteValueLengthForWithoutResponse(
696
+ peripheralId,
697
+ (error: string | null, max: number) => {
698
+ if (error) {
699
+ reject(error);
700
+ } else {
701
+ fulfill(max);
702
+ }
615
703
  }
616
- });
704
+ );
617
705
  });
618
706
  }
619
707
 
620
708
  /**
621
709
  * [iOS only]
622
- * @param peripheralId
623
- * @returns
710
+ * @param peripheralId
711
+ * @returns
624
712
  */
625
713
  getMaximumWriteValueLengthForWithResponse(peripheralId: string) {
626
714
  return new Promise<number>((fulfill, reject) => {
627
- bleManager.getMaximumWriteValueLengthForWithResponse(peripheralId, (error: string | null, max: number) => {
628
- if (error) {
629
- reject(error);
630
- } else {
631
- fulfill(max);
715
+ bleManager.getMaximumWriteValueLengthForWithResponse(
716
+ peripheralId,
717
+ (error: string | null, max: number) => {
718
+ if (error) {
719
+ reject(error);
720
+ } else {
721
+ fulfill(max);
722
+ }
632
723
  }
633
- });
724
+ );
634
725
  });
635
726
  }
636
-
637
727
  }
638
728
 
639
-
640
-
641
729
  export default new BleManager();