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