react-native-ble-manager 8.8.0 → 9.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.
Files changed (60) hide show
  1. package/README.md +131 -44
  2. package/android/.gradle/7.4/checksums/checksums.lock +0 -0
  3. package/android/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock +0 -0
  4. package/android/.gradle/7.4/dependencies-accessors/gc.properties +0 -0
  5. package/android/.gradle/7.4/executionHistory/executionHistory.bin +0 -0
  6. package/android/.gradle/7.4/executionHistory/executionHistory.lock +0 -0
  7. package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
  8. package/android/.gradle/7.4/fileHashes/fileHashes.bin +0 -0
  9. package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
  10. package/android/.gradle/7.4/fileHashes/resourceHashesCache.bin +0 -0
  11. package/android/.gradle/7.4/gc.properties +0 -0
  12. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  13. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  14. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  15. package/android/.gradle/file-system.probe +0 -0
  16. package/android/.gradle/vcs-1/gc.properties +0 -0
  17. package/android/.idea/.name +1 -0
  18. package/android/.idea/compiler.xml +6 -0
  19. package/android/.idea/gradle.xml +18 -0
  20. package/android/.idea/jarRepositories.xml +40 -0
  21. package/android/.idea/misc.xml +10 -0
  22. package/android/.idea/vcs.xml +6 -0
  23. package/android/build.gradle +17 -17
  24. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  25. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  26. package/android/gradle.properties +3 -1
  27. package/android/gradlew +234 -0
  28. package/android/gradlew.bat +89 -0
  29. package/android/local.properties +8 -0
  30. package/android/src/main/java/it/innove/BleManager.java +38 -17
  31. package/android/src/main/java/it/innove/LegacyScanManager.java +33 -28
  32. package/android/src/main/java/it/innove/LollipopPeripheral.java +66 -65
  33. package/android/src/main/java/it/innove/LollipopScanManager.java +52 -25
  34. package/android/src/main/java/it/innove/Peripheral.java +973 -838
  35. package/android/src/main/java/it/innove/ScanManager.java +24 -23
  36. package/dist/cjs/index.d.ts +156 -0
  37. package/dist/cjs/index.js +507 -0
  38. package/dist/cjs/index.js.map +1 -0
  39. package/dist/cjs/types.d.ts +311 -0
  40. package/dist/cjs/types.js +110 -0
  41. package/dist/cjs/types.js.map +1 -0
  42. package/dist/esm/index.d.ts +156 -0
  43. package/dist/esm/index.js +491 -0
  44. package/dist/esm/index.js.map +1 -0
  45. package/dist/esm/types.d.ts +311 -0
  46. package/dist/esm/types.js +107 -0
  47. package/dist/esm/types.js.map +1 -0
  48. package/ios/BleManager.h +1 -0
  49. package/ios/BleManager.m +213 -115
  50. package/ios/BleManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  51. package/ios/BleManager.xcodeproj/project.xcworkspace/xcuserdata/marco.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  52. package/ios/BleManager.xcodeproj/xcuserdata/marco.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  53. package/ios/CBPeripheral+Extensions.m +1 -1
  54. package/package.json +31 -4
  55. package/src/index.ts +595 -0
  56. package/src/types.ts +336 -0
  57. package/.github/FUNDING.yml +0 -3
  58. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  59. package/BleManager.js +0 -427
  60. package/index.d.ts +0 -294
package/src/index.ts ADDED
@@ -0,0 +1,595 @@
1
+ import { NativeModules } from 'react-native';
2
+ import {
3
+ BleScanCallbackType,
4
+ BleScanMatchCount,
5
+ BleScanMatchMode,
6
+ BleScanMode,
7
+ BleState,
8
+ ConnectionPriority,
9
+ Peripheral,
10
+ PeripheralInfo,
11
+ ScanOptions,
12
+ StartOptions
13
+ } from './types';
14
+
15
+ export * from './types';
16
+
17
+ var bleManager = NativeModules.BleManager;
18
+
19
+ class BleManager {
20
+ constructor() {
21
+ this.isPeripheralConnected = this.isPeripheralConnected.bind(this);
22
+ }
23
+
24
+ /**
25
+ *
26
+ * @param peripheralId
27
+ * @param serviceUUID
28
+ * @param characteristicUUID
29
+ * @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))
30
+ */
31
+ read(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
32
+ return new Promise<number[]>((fulfill, reject) => {
33
+ bleManager.read(
34
+ peripheralId,
35
+ serviceUUID,
36
+ characteristicUUID,
37
+ (error: string | null, data: number[]) => {
38
+ if (error) {
39
+ reject(error);
40
+ } else {
41
+ fulfill(data);
42
+ }
43
+ }
44
+ );
45
+ });
46
+ }
47
+
48
+ /**
49
+ *
50
+ * @param peripheralId
51
+ * @param serviceUUID
52
+ * @param characteristicUUID
53
+ * @param descriptorUUID
54
+ * @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))
55
+ */
56
+ readDescriptor(peripheralId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string) {
57
+ return new Promise<number[]>((fulfill, reject) => {
58
+ bleManager.readDescriptor(
59
+ peripheralId,
60
+ serviceUUID,
61
+ characteristicUUID,
62
+ descriptorUUID,
63
+ (error: string | null, data: number[]) => {
64
+ if (error) {
65
+ reject(error);
66
+ } else {
67
+ fulfill(data);
68
+ }
69
+ }
70
+ );
71
+ });
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @param peripheralId
77
+ * @returns a promise resolving with the updated RSSI (`number`) if it succeeds.
78
+ */
79
+ readRSSI(peripheralId: string) {
80
+ return new Promise<number>((fulfill, reject) => {
81
+ bleManager.readRSSI(peripheralId, (error: string | null, rssi: number) => {
82
+ if (error) {
83
+ reject(error);
84
+ } else {
85
+ fulfill(rssi);
86
+ }
87
+ });
88
+ });
89
+ }
90
+
91
+ /**
92
+ * [Android only]
93
+ * @param peripheralId
94
+ * @returns a promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
95
+ */
96
+ refreshCache(peripheralId: string) {
97
+ return new Promise<boolean>((fulfill, reject) => {
98
+ bleManager.refreshCache(peripheralId, (error: string | null, result: boolean) => {
99
+ if (error) {
100
+ reject(error);
101
+ } else {
102
+ fulfill(result);
103
+ }
104
+ });
105
+ });
106
+ }
107
+
108
+ /**
109
+ *
110
+ * @param peripheralId
111
+ * @param serviceUUIDs [iOS only] optional filter of services to retrieve.
112
+ * @returns
113
+ */
114
+ retrieveServices(peripheralId: string, serviceUUIDs: string[] = []) {
115
+ return new Promise<PeripheralInfo>((fulfill, reject) => {
116
+ bleManager.retrieveServices(
117
+ peripheralId,
118
+ serviceUUIDs,
119
+ (error: string | null, peripheral: PeripheralInfo) => {
120
+ if (error) {
121
+ reject(error);
122
+ } else {
123
+ fulfill(peripheral);
124
+ }
125
+ }
126
+ );
127
+ });
128
+ }
129
+
130
+ /**
131
+ *
132
+ * @param peripheralId
133
+ * @param serviceUUID
134
+ * @param characteristicUUID
135
+ * @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))
136
+ * @param maxByteSize optional, defaults to 20
137
+ * @returns
138
+ */
139
+ write(
140
+ peripheralId: string,
141
+ serviceUUID: string,
142
+ characteristicUUID: string,
143
+ data: number[],
144
+ maxByteSize: number = 20
145
+ ) {
146
+
147
+ return new Promise<void>((fulfill, reject) => {
148
+ bleManager.write(
149
+ peripheralId,
150
+ serviceUUID,
151
+ characteristicUUID,
152
+ data,
153
+ maxByteSize,
154
+ (error: string | null) => {
155
+ if (error) {
156
+ reject(error);
157
+ } else {
158
+ fulfill();
159
+ }
160
+ }
161
+ );
162
+ });
163
+ }
164
+
165
+ /**
166
+ *
167
+ * @param peripheralId
168
+ * @param serviceUUID
169
+ * @param characteristicUUID
170
+ * @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))
171
+ * @param maxByteSize optional, defaults to 20
172
+ * @param queueSleepTime optional, defaults to 10. Only useful if data length is greater than maxByteSize.
173
+ * @returns
174
+ */
175
+ writeWithoutResponse(
176
+ peripheralId: string,
177
+ serviceUUID: string,
178
+ characteristicUUID: string,
179
+ data: number[],
180
+ maxByteSize: number = 20,
181
+ queueSleepTime: number = 10
182
+ ) {
183
+
184
+ return new Promise<void>((fulfill, reject) => {
185
+ bleManager.writeWithoutResponse(
186
+ peripheralId,
187
+ serviceUUID,
188
+ characteristicUUID,
189
+ data,
190
+ maxByteSize,
191
+ queueSleepTime,
192
+ (error: string | null) => {
193
+ if (error) {
194
+ reject(error);
195
+ } else {
196
+ fulfill();
197
+ }
198
+ }
199
+ );
200
+ });
201
+ }
202
+
203
+ connect(peripheralId: string) {
204
+ return new Promise<void>((fulfill, reject) => {
205
+ bleManager.connect(peripheralId, (error: string | null) => {
206
+ if (error) {
207
+ reject(error);
208
+ } else {
209
+ fulfill();
210
+ }
211
+ });
212
+ });
213
+ }
214
+
215
+ /**
216
+ * [Android only]
217
+ * @param peripheralId
218
+ * @param peripheralPin optional. will be used to auto-bond if possible.
219
+ * @returns
220
+ */
221
+ createBond(peripheralId: string, peripheralPin: string | null = null) {
222
+ return new Promise<void>((fulfill, reject) => {
223
+ bleManager.createBond(peripheralId, peripheralPin, (error: string | null) => {
224
+ if (error) {
225
+ reject(error);
226
+ } else {
227
+ fulfill();
228
+ }
229
+ });
230
+ });
231
+ }
232
+
233
+ /**
234
+ * [Android only]
235
+ * @param peripheralId
236
+ * @returns
237
+ */
238
+ removeBond(peripheralId: string) {
239
+ return new Promise<void>((fulfill, reject) => {
240
+ bleManager.removeBond(peripheralId, (error: string | null) => {
241
+ if (error) {
242
+ reject(error);
243
+ } else {
244
+ fulfill();
245
+ }
246
+ });
247
+ });
248
+ }
249
+
250
+ /**
251
+ *
252
+ * @param peripheralId
253
+ * @param force [Android only] defaults to true.
254
+ * @returns
255
+ */
256
+ disconnect(peripheralId: string, force: boolean = true) {
257
+ return new Promise<void>((fulfill, reject) => {
258
+ bleManager.disconnect(peripheralId, force, (error: string | null) => {
259
+ if (error) {
260
+ reject(error);
261
+ } else {
262
+ fulfill();
263
+ }
264
+ });
265
+ });
266
+ }
267
+
268
+ startNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
269
+ return new Promise<void>((fulfill, reject) => {
270
+ bleManager.startNotification(
271
+ peripheralId,
272
+ serviceUUID,
273
+ characteristicUUID,
274
+ (error: string | null) => {
275
+ if (error) {
276
+ reject(error);
277
+ } else {
278
+ fulfill();
279
+ }
280
+ }
281
+ );
282
+ });
283
+ }
284
+
285
+ /**
286
+ * [Android only]
287
+ * @param peripheralId
288
+ * @param serviceUUID
289
+ * @param characteristicUUID
290
+ * @param buffer
291
+ * @returns
292
+ */
293
+ startNotificationUseBuffer(
294
+ peripheralId: string,
295
+ serviceUUID: string,
296
+ characteristicUUID: string,
297
+ buffer: number
298
+ ) {
299
+ return new Promise<void>((fulfill, reject) => {
300
+ bleManager.startNotificationUseBuffer(
301
+ peripheralId,
302
+ serviceUUID,
303
+ characteristicUUID,
304
+ buffer,
305
+ (error: string | null) => {
306
+ if (error) {
307
+ reject(error);
308
+ } else {
309
+ fulfill();
310
+ }
311
+ }
312
+ );
313
+ });
314
+ }
315
+
316
+ stopNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string) {
317
+ return new Promise<void>((fulfill, reject) => {
318
+ bleManager.stopNotification(
319
+ peripheralId,
320
+ serviceUUID,
321
+ characteristicUUID,
322
+ (error: string | null) => {
323
+ if (error) {
324
+ reject(error);
325
+ } else {
326
+ fulfill();
327
+ }
328
+ }
329
+ );
330
+ });
331
+ }
332
+
333
+ checkState() {
334
+ return new Promise<BleState>((fulfill, _) => {
335
+ bleManager.checkState((state: BleState) => {
336
+ fulfill(state);
337
+ });
338
+ });
339
+ }
340
+
341
+ start(options?: StartOptions) {
342
+ return new Promise<void>((fulfill, reject) => {
343
+ if (options == null) {
344
+ options = {};
345
+ }
346
+ bleManager.start(options, (error: string | null) => {
347
+ if (error) {
348
+ reject(error);
349
+ } else {
350
+ fulfill();
351
+ }
352
+ });
353
+ });
354
+ }
355
+
356
+ /**
357
+ *
358
+ * @param serviceUUIDs
359
+ * @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).
360
+ * @param allowDuplicates [iOS only]
361
+ * @param scanningOptions [Android only] optional map of properties to fine-tune scan behavior on android, see README.
362
+ * @returns
363
+ */
364
+ scan(
365
+ serviceUUIDs: string[],
366
+ seconds: number,
367
+ allowDuplicates?: boolean,
368
+ scanningOptions: ScanOptions = {}
369
+ ) {
370
+
371
+ return new Promise<void>((fulfill, reject) => {
372
+ if (allowDuplicates == null) {
373
+ allowDuplicates = false;
374
+ }
375
+
376
+ // (ANDROID) Match as many advertisement per filter as hw could allow
377
+ // depends on current capability and availability of the resources in hw.
378
+ if (scanningOptions.numberOfMatches == null) {
379
+ scanningOptions.numberOfMatches = BleScanMatchCount.MaxAdvertisements;
380
+ }
381
+
382
+ // (ANDROID) Defaults to MATCH_MODE_AGGRESSIVE
383
+ if (scanningOptions.matchMode == null) {
384
+ scanningOptions.matchMode = BleScanMatchMode.Aggressive;
385
+ }
386
+
387
+ // (ANDROID) Defaults to SCAN_MODE_LOW_POWER
388
+ if (scanningOptions.scanMode == null) {
389
+ scanningOptions.scanMode = BleScanMode.LowPower;
390
+ }
391
+
392
+ // (ANDROID) Defaults to CALLBACK_TYPE_ALL_MATCHES
393
+ // WARN: sometimes, setting a scanSetting instead of leaving it untouched might result in unexpected behaviors.
394
+ // https://github.com/dariuszseweryn/RxAndroidBle/issues/462
395
+ if (scanningOptions.callbackType == null) {
396
+ scanningOptions.callbackType = BleScanCallbackType.AllMatches;
397
+ }
398
+
399
+ // (ANDROID) Defaults to 0ms (report results immediately).
400
+ if (scanningOptions.reportDelay == null) {
401
+ scanningOptions.reportDelay = 0;
402
+ }
403
+
404
+ // (ANDROID) ScanFilter used to restrict search to devices with a specific advertising name.
405
+ // https://developer.android.com/reference/android/bluetooth/le/ScanFilter.Builder#setDeviceName(java.lang.String)
406
+ if (!scanningOptions.exactAdvertisingName
407
+ || typeof scanningOptions.exactAdvertisingName !== 'string') {
408
+ delete scanningOptions.exactAdvertisingName;
409
+ }
410
+
411
+ bleManager.scan(
412
+ serviceUUIDs,
413
+ seconds,
414
+ allowDuplicates,
415
+ scanningOptions,
416
+ (error: string | null) => {
417
+ if (error) {
418
+ reject(error);
419
+ } else {
420
+ fulfill();
421
+ }
422
+ }
423
+ );
424
+ });
425
+ }
426
+
427
+ stopScan() {
428
+ return new Promise<void>((fulfill, reject) => {
429
+ bleManager.stopScan((error: string | null) => {
430
+ if (error) {
431
+ reject(error);
432
+ } else {
433
+ fulfill();
434
+ }
435
+ });
436
+ });
437
+ }
438
+
439
+ /**
440
+ * [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
441
+ * @returns
442
+ */
443
+ enableBluetooth() {
444
+ return new Promise<void>((fulfill, reject) => {
445
+ bleManager.enableBluetooth((error: string | null) => {
446
+ if (error) {
447
+ reject(error);
448
+ } else {
449
+ fulfill();
450
+ }
451
+ });
452
+ });
453
+ }
454
+
455
+ /**
456
+ *
457
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
458
+ * @returns
459
+ */
460
+ getConnectedPeripherals(serviceUUIDs: string[] = []) {
461
+ return new Promise<Peripheral[]>((fulfill, reject) => {
462
+
463
+ bleManager.getConnectedPeripherals(serviceUUIDs, (error: string | null, result: Peripheral[] | null) => {
464
+ if (error) {
465
+ reject(error);
466
+ } else {
467
+ if (result) {
468
+ fulfill(result);
469
+ } else {
470
+ fulfill([]);
471
+ }
472
+ }
473
+ });
474
+ });
475
+ }
476
+
477
+ /**
478
+ * [Android only]
479
+ * @returns
480
+ */
481
+ getBondedPeripherals() {
482
+ return new Promise<Peripheral[]>((fulfill, reject) => {
483
+ bleManager.getBondedPeripherals((error: string | null, result: Peripheral[] | null) => {
484
+ if (error) {
485
+ reject(error);
486
+ } else {
487
+ if (result) {
488
+ fulfill(result);
489
+ } else {
490
+ fulfill([]);
491
+ }
492
+ }
493
+ });
494
+ });
495
+ }
496
+
497
+ getDiscoveredPeripherals() {
498
+ return new Promise<Peripheral[]>((fulfill, reject) => {
499
+ bleManager.getDiscoveredPeripherals((error: string | null, result: Peripheral[] | null) => {
500
+ if (error) {
501
+ reject(error);
502
+ } else {
503
+ if (result) {
504
+ fulfill(result);
505
+ } else {
506
+ fulfill([]);
507
+ }
508
+ }
509
+ });
510
+ });
511
+ }
512
+
513
+ /**
514
+ * [Android only]
515
+ * @param peripheralId
516
+ * @returns
517
+ */
518
+ removePeripheral(peripheralId: string) {
519
+ return new Promise<void>((fulfill, reject) => {
520
+ bleManager.removePeripheral(peripheralId, (error: string | null) => {
521
+ if (error) {
522
+ reject(error);
523
+ } else {
524
+ fulfill();
525
+ }
526
+ });
527
+ });
528
+ }
529
+
530
+ /**
531
+ * @param peripheralId
532
+ * @param serviceUUIDs [optional] not used on android, optional on ios.
533
+ * @returns
534
+ */
535
+ isPeripheralConnected(peripheralId: string, serviceUUIDs: string[] = []) {
536
+ return this.getConnectedPeripherals(serviceUUIDs).then(result => {
537
+ if (result.find(p => p.id === peripheralId)) {
538
+ return true;
539
+ } else {
540
+ return false;
541
+ }
542
+ });
543
+ }
544
+
545
+ /**
546
+ * [Android only, API 21+]
547
+ * @param peripheralId
548
+ * @param connectionPriority
549
+ * @returns a promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
550
+ */
551
+ requestConnectionPriority(peripheralId: string, connectionPriority: ConnectionPriority) {
552
+ return new Promise<boolean>((fulfill, reject) => {
553
+ bleManager.requestConnectionPriority(
554
+ peripheralId,
555
+ connectionPriority,
556
+ (error: string | null, status: boolean) => {
557
+ if (error) {
558
+ reject(error);
559
+ } else {
560
+ fulfill(status);
561
+ }
562
+ }
563
+ );
564
+ });
565
+ }
566
+
567
+
568
+ /**
569
+ * [Android only, API 21+]
570
+ * @param peripheralId
571
+ * @param mtu size to be requested, in bytes.
572
+ * @returns a promise resolving with the negotiated MTU if it succeeded. Beware that it might not be the one requested due to device's BLE limitations on both side of the negotiation.
573
+ */
574
+ requestMTU(peripheralId: string, mtu: number) {
575
+ return new Promise<number>((fulfill, reject) => {
576
+ bleManager.requestMTU(peripheralId, mtu, (error: string | null, mtu: number) => {
577
+ if (error) {
578
+ reject(error);
579
+ } else {
580
+ fulfill(mtu);
581
+ }
582
+ });
583
+ });
584
+ }
585
+
586
+ /**
587
+ * [Android only]
588
+ * @param name
589
+ */
590
+ setName(name: string) {
591
+ bleManager.setName(name);
592
+ }
593
+ }
594
+
595
+ export default new BleManager();