react-native-ble-nitro 1.10.2 → 1.11.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/android/src/main/cpp/cpp-adapter.cpp +4 -1
- package/lib/commonjs/manager.d.ts +2 -3
- package/lib/commonjs/manager.d.ts.map +1 -1
- package/lib/commonjs/manager.js +14 -23
- package/lib/commonjs/manager.js.map +1 -1
- package/lib/manager.d.ts +2 -3
- package/lib/manager.js +14 -23
- package/nitrogen/generated/android/BleNitroOnLoad.cpp +48 -32
- package/nitrogen/generated/android/BleNitroOnLoad.hpp +13 -4
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroFactorySpec.cpp +20 -26
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroFactorySpec.hpp +19 -22
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +43 -49
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +19 -22
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroFactorySpec.kt +15 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +15 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Variant_NullType_BLEDevice.kt +0 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Variant_NullType_String.kt +0 -6
- package/package.json +9 -9
- package/src/__tests__/index.test.ts +22 -1
- package/src/manager.ts +15 -24
package/src/manager.ts
CHANGED
|
@@ -127,7 +127,6 @@ export function byteArrayToArrayBuffer(data: ByteArray): ArrayBuffer {
|
|
|
127
127
|
|
|
128
128
|
export class BleNitroManager {
|
|
129
129
|
private _isScanning: boolean = false;
|
|
130
|
-
private _connectedDevices: { [deviceId: string]: boolean } = {};
|
|
131
130
|
|
|
132
131
|
private _restoredStateCallback: RestoreStateCallback | null;
|
|
133
132
|
private _restoredState: BLEDevice[] | null = null;
|
|
@@ -144,9 +143,6 @@ export class BleNitroManager {
|
|
|
144
143
|
private onNativeRestoreStateCallback(peripherals: NativeBLEDevice[]) {
|
|
145
144
|
if (!this._restoreStateIdentifier) return;
|
|
146
145
|
const bleDevices = peripherals.map((peripheral) => convertNativeBleDeviceToBleDevice(peripheral));
|
|
147
|
-
bleDevices.forEach((device) => {
|
|
148
|
-
this._connectedDevices[device.id] = device.isConnected;
|
|
149
|
-
});
|
|
150
146
|
if (this._restoredStateCallback) {
|
|
151
147
|
this._restoredStateCallback(bleDevices);
|
|
152
148
|
} else {
|
|
@@ -289,7 +285,7 @@ export class BleNitroManager {
|
|
|
289
285
|
): Promise<string> {
|
|
290
286
|
return new Promise((resolve, reject) => {
|
|
291
287
|
// Check if already connected
|
|
292
|
-
if (this.
|
|
288
|
+
if (this.isConnected(deviceId)) {
|
|
293
289
|
resolve(deviceId);
|
|
294
290
|
return;
|
|
295
291
|
}
|
|
@@ -298,15 +294,12 @@ export class BleNitroManager {
|
|
|
298
294
|
deviceId,
|
|
299
295
|
(success: boolean, connectedDeviceId: string, error: string) => {
|
|
300
296
|
if (success) {
|
|
301
|
-
this._connectedDevices[deviceId] = true;
|
|
302
297
|
resolve(connectedDeviceId);
|
|
303
298
|
} else {
|
|
304
299
|
reject(new Error(error));
|
|
305
300
|
}
|
|
306
301
|
},
|
|
307
302
|
onDisconnect ? (deviceId: string, interrupted: boolean, error: string) => {
|
|
308
|
-
// Remove from connected devices when disconnected
|
|
309
|
-
delete this._connectedDevices[deviceId];
|
|
310
303
|
onDisconnect(deviceId, interrupted, error);
|
|
311
304
|
} : undefined,
|
|
312
305
|
autoConnectAndroid ?? false,
|
|
@@ -321,8 +314,7 @@ export class BleNitroManager {
|
|
|
321
314
|
* @returns Promise resolving deviceId when connected
|
|
322
315
|
*/
|
|
323
316
|
public findAndConnect(deviceId: string, options?: { scanTimeout?: number, autoConnectAndroid?: boolean, onDisconnect?: DisconnectEventCallback, onFound?: (device: BLEDevice) => void }): Promise<string> {
|
|
324
|
-
|
|
325
|
-
if (isConnected) {
|
|
317
|
+
if (this.isConnected(deviceId)) {
|
|
326
318
|
return Promise.resolve(deviceId);
|
|
327
319
|
}
|
|
328
320
|
if (this._isScanning) {
|
|
@@ -351,13 +343,13 @@ export class BleNitroManager {
|
|
|
351
343
|
/**
|
|
352
344
|
* Disconnect from a Bluetooth device
|
|
353
345
|
* @param deviceId ID of the device to disconnect from
|
|
354
|
-
* @returns Promise resolving when disconnected
|
|
346
|
+
* @returns Promise resolving with devices uuid or mac address when disconnected
|
|
355
347
|
*/
|
|
356
|
-
public disconnect(deviceId: string): Promise<
|
|
348
|
+
public disconnect(deviceId: string): Promise<string> {
|
|
357
349
|
return new Promise((resolve, reject) => {
|
|
358
350
|
// Check if already disconnected
|
|
359
|
-
if (!this.
|
|
360
|
-
resolve();
|
|
351
|
+
if (!this.isConnected(deviceId)) {
|
|
352
|
+
resolve(deviceId);
|
|
361
353
|
return;
|
|
362
354
|
}
|
|
363
355
|
|
|
@@ -365,8 +357,7 @@ export class BleNitroManager {
|
|
|
365
357
|
deviceId,
|
|
366
358
|
(success: boolean, error: string) => {
|
|
367
359
|
if (success) {
|
|
368
|
-
|
|
369
|
-
resolve();
|
|
360
|
+
resolve(deviceId);
|
|
370
361
|
} else {
|
|
371
362
|
reject(new Error(error));
|
|
372
363
|
}
|
|
@@ -404,7 +395,7 @@ export class BleNitroManager {
|
|
|
404
395
|
public readRSSI(deviceId: string): Promise<number> {
|
|
405
396
|
return new Promise((resolve, reject) => {
|
|
406
397
|
// Check if connected first
|
|
407
|
-
if (!this.
|
|
398
|
+
if (!this.isConnected(deviceId)) {
|
|
408
399
|
reject(new Error('Device not connected'));
|
|
409
400
|
return;
|
|
410
401
|
}
|
|
@@ -430,7 +421,7 @@ export class BleNitroManager {
|
|
|
430
421
|
public discoverServices(deviceId: string): Promise<boolean> {
|
|
431
422
|
return new Promise((resolve, reject) => {
|
|
432
423
|
// Check if connected first
|
|
433
|
-
if (!this.
|
|
424
|
+
if (!this.isConnected(deviceId)) {
|
|
434
425
|
reject(new Error('Device not connected'));
|
|
435
426
|
return;
|
|
436
427
|
}
|
|
@@ -456,7 +447,7 @@ export class BleNitroManager {
|
|
|
456
447
|
public getServices(deviceId: string): Promise<string[]> {
|
|
457
448
|
return new Promise(async (resolve, reject) => {
|
|
458
449
|
// Check if connected first
|
|
459
|
-
if (!this.
|
|
450
|
+
if (!this.isConnected(deviceId)) {
|
|
460
451
|
reject(new Error('Device not connected'));
|
|
461
452
|
return;
|
|
462
453
|
}
|
|
@@ -481,7 +472,7 @@ export class BleNitroManager {
|
|
|
481
472
|
deviceId: string,
|
|
482
473
|
serviceId: string
|
|
483
474
|
): string[] {
|
|
484
|
-
if (!this.
|
|
475
|
+
if (!this.isConnected(deviceId)) {
|
|
485
476
|
throw new Error('Device not connected');
|
|
486
477
|
}
|
|
487
478
|
|
|
@@ -524,7 +515,7 @@ export class BleNitroManager {
|
|
|
524
515
|
): Promise<ByteArray> {
|
|
525
516
|
return new Promise((resolve, reject) => {
|
|
526
517
|
// Check if connected first
|
|
527
|
-
if (!this.
|
|
518
|
+
if (!this.isConnected(deviceId)) {
|
|
528
519
|
reject(new Error('Device not connected'));
|
|
529
520
|
return;
|
|
530
521
|
}
|
|
@@ -562,7 +553,7 @@ export class BleNitroManager {
|
|
|
562
553
|
): Promise<ByteArray> {
|
|
563
554
|
return new Promise((resolve, reject) => {
|
|
564
555
|
// Check if connected first
|
|
565
|
-
if (!this.
|
|
556
|
+
if (!this.isConnected(deviceId)) {
|
|
566
557
|
reject(new Error('Device not connected'));
|
|
567
558
|
return;
|
|
568
559
|
}
|
|
@@ -602,7 +593,7 @@ export class BleNitroManager {
|
|
|
602
593
|
): Promise<AsyncSubscription> {
|
|
603
594
|
return new Promise((resolve, reject) => {
|
|
604
595
|
// Check if connected first
|
|
605
|
-
if (!this.
|
|
596
|
+
if (!this.isConnected(deviceId)) {
|
|
606
597
|
reject(new Error('Device not connected'));
|
|
607
598
|
return;
|
|
608
599
|
}
|
|
@@ -650,7 +641,7 @@ export class BleNitroManager {
|
|
|
650
641
|
): Promise<void> {
|
|
651
642
|
return new Promise((resolve, reject) => {
|
|
652
643
|
// Check if connected first
|
|
653
|
-
if (!this.
|
|
644
|
+
if (!this.isConnected(deviceId)) {
|
|
654
645
|
reject(new Error('Device not connected'));
|
|
655
646
|
return;
|
|
656
647
|
}
|