react-native-ble-nitro 1.6.0 → 1.7.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/README.md +27 -11
- package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManager.kt +8 -3
- package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManagerFactory.kt +21 -0
- package/ios/BleNitroBleManager.swift +36 -23
- package/ios/BleNitroBleManagerFactory.swift +19 -0
- package/lib/commonjs/index.d.ts +2 -5
- package/lib/commonjs/index.d.ts.map +1 -1
- package/lib/commonjs/index.js +7 -15
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/manager.d.ts +9 -0
- package/lib/commonjs/manager.d.ts.map +1 -1
- package/lib/commonjs/manager.js +44 -30
- package/lib/commonjs/manager.js.map +1 -1
- package/lib/commonjs/singleton.d.ts +10 -0
- package/lib/commonjs/singleton.d.ts.map +1 -0
- package/lib/commonjs/singleton.js +20 -0
- package/lib/commonjs/singleton.js.map +1 -0
- package/lib/commonjs/specs/NativeBleNitro.d.ts +1 -1
- package/lib/commonjs/specs/NativeBleNitro.d.ts.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.js +1 -0
- package/lib/commonjs/specs/NativeBleNitro.js.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +2 -0
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.js.map +1 -1
- package/lib/commonjs/specs/NativeBleNitroFactory.d.ts +5 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.d.ts.map +1 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.js +23 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.js.map +1 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.nitro.d.ts +9 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.nitro.d.ts.map +1 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.nitro.js +3 -0
- package/lib/commonjs/specs/NativeBleNitroFactory.nitro.js.map +1 -0
- package/lib/index.d.ts +2 -5
- package/lib/index.js +2 -11
- package/lib/manager.d.ts +9 -0
- package/lib/manager.js +38 -25
- package/lib/singleton.d.ts +9 -0
- package/lib/singleton.js +15 -0
- package/lib/specs/NativeBleNitro.d.ts +1 -1
- package/lib/specs/NativeBleNitro.js +1 -0
- package/lib/specs/NativeBleNitro.nitro.d.ts +2 -0
- package/lib/specs/NativeBleNitroFactory.d.ts +4 -0
- package/lib/specs/NativeBleNitroFactory.js +6 -0
- package/lib/specs/NativeBleNitroFactory.nitro.d.ts +8 -0
- package/lib/specs/NativeBleNitroFactory.nitro.js +1 -0
- package/nitro.json +4 -0
- package/nitrogen/generated/android/BleNitro+autolinking.cmake +2 -0
- package/nitrogen/generated/android/BleNitroOnLoad.cpp +11 -0
- package/nitrogen/generated/android/c++/JBLEDevice.hpp +6 -2
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +12 -12
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroFactorySpec.cpp +71 -0
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroFactorySpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +11 -3
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/BLEDevice.kt +4 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +9 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroFactorySpec.kt +57 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +5 -1
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +44 -9
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +5 -0
- package/nitrogen/generated/ios/BleNitroAutolinking.mm +8 -0
- package/nitrogen/generated/ios/BleNitroAutolinking.swift +15 -0
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroFactorySpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroFactorySpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +9 -3
- package/nitrogen/generated/ios/swift/BLEDevice.swift +13 -2
- package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +5 -5
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroFactorySpec.swift +49 -0
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroFactorySpec_cxx.swift +149 -0
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +22 -1
- package/nitrogen/generated/shared/c++/BLEDevice.hpp +6 -2
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroFactorySpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroFactorySpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +4 -3
- package/package.json +9 -1
- package/src/__tests__/index.test.ts +36 -26
- package/src/index.ts +2 -13
- package/src/manager.ts +41 -25
- package/src/singleton.ts +17 -0
- package/src/specs/NativeBleNitro.nitro.ts +2 -0
- package/src/specs/NativeBleNitro.ts +2 -1
- package/src/specs/NativeBleNitroFactory.nitro.ts +6 -0
- package/src/specs/NativeBleNitroFactory.ts +9 -0
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
// Mock the native module import
|
|
2
|
+
const mockNativeInstance = {
|
|
3
|
+
setRestoreStateCallback: jest.fn(),
|
|
4
|
+
startScan: jest.fn(),
|
|
5
|
+
stopScan: jest.fn(),
|
|
6
|
+
isScanning: jest.fn(),
|
|
7
|
+
connect: jest.fn(),
|
|
8
|
+
disconnect: jest.fn(),
|
|
9
|
+
isConnected: jest.fn(),
|
|
10
|
+
requestMTU: jest.fn(),
|
|
11
|
+
readRSSI: jest.fn(),
|
|
12
|
+
discoverServices: jest.fn(),
|
|
13
|
+
getServices: jest.fn(),
|
|
14
|
+
getCharacteristics: jest.fn(),
|
|
15
|
+
readCharacteristic: jest.fn(),
|
|
16
|
+
writeCharacteristic: jest.fn(),
|
|
17
|
+
subscribeToCharacteristic: jest.fn(),
|
|
18
|
+
unsubscribeFromCharacteristic: jest.fn(),
|
|
19
|
+
getConnectedDevices: jest.fn(),
|
|
20
|
+
requestBluetoothEnable: jest.fn(),
|
|
21
|
+
state: jest.fn(),
|
|
22
|
+
subscribeToStateChange: jest.fn(),
|
|
23
|
+
unsubscribeFromStateChange: jest.fn(),
|
|
24
|
+
openSettings: jest.fn(),
|
|
25
|
+
restoreStateIdentifier: null,
|
|
26
|
+
};
|
|
27
|
+
|
|
2
28
|
jest.mock('../specs/NativeBleNitro', () => ({
|
|
3
29
|
__esModule: true,
|
|
4
|
-
default:
|
|
5
|
-
|
|
6
|
-
startScan: jest.fn(),
|
|
7
|
-
stopScan: jest.fn(),
|
|
8
|
-
isScanning: jest.fn(),
|
|
9
|
-
connect: jest.fn(),
|
|
10
|
-
disconnect: jest.fn(),
|
|
11
|
-
isConnected: jest.fn(),
|
|
12
|
-
requestMTU: jest.fn(),
|
|
13
|
-
readRSSI: jest.fn(),
|
|
14
|
-
discoverServices: jest.fn(),
|
|
15
|
-
getServices: jest.fn(),
|
|
16
|
-
getCharacteristics: jest.fn(),
|
|
17
|
-
readCharacteristic: jest.fn(),
|
|
18
|
-
writeCharacteristic: jest.fn(),
|
|
19
|
-
subscribeToCharacteristic: jest.fn(),
|
|
20
|
-
unsubscribeFromCharacteristic: jest.fn(),
|
|
21
|
-
getConnectedDevices: jest.fn(),
|
|
22
|
-
requestBluetoothEnable: jest.fn(),
|
|
23
|
-
state: jest.fn(),
|
|
24
|
-
subscribeToStateChange: jest.fn(),
|
|
25
|
-
unsubscribeFromStateChange: jest.fn(),
|
|
26
|
-
openSettings: jest.fn(),
|
|
27
|
-
},
|
|
28
|
-
BLEState: {
|
|
30
|
+
default: mockNativeInstance,
|
|
31
|
+
BLEState: {
|
|
29
32
|
Unknown: 0,
|
|
30
33
|
Resetting: 1,
|
|
31
34
|
Unsupported: 2,
|
|
@@ -41,10 +44,17 @@ jest.mock('../specs/NativeBleNitro', () => ({
|
|
|
41
44
|
},
|
|
42
45
|
}));
|
|
43
46
|
|
|
47
|
+
jest.mock('../specs/NativeBleNitroFactory', () => ({
|
|
48
|
+
__esModule: true,
|
|
49
|
+
default: {
|
|
50
|
+
create: jest.fn(() => mockNativeInstance),
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
|
|
44
54
|
import { BleNitro } from '../index';
|
|
45
55
|
|
|
46
56
|
// Get reference to the mocked module
|
|
47
|
-
const mockNative =
|
|
57
|
+
const mockNative = mockNativeInstance;
|
|
48
58
|
|
|
49
59
|
// Get BLE instance
|
|
50
60
|
const BleManager = BleNitro.instance();
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { BleNitroManager } from "./manager";
|
|
2
|
-
|
|
3
1
|
export {
|
|
4
2
|
type ByteArray,
|
|
5
3
|
type ScanFilter,
|
|
@@ -12,19 +10,10 @@ export {
|
|
|
12
10
|
type OperationCallback,
|
|
13
11
|
type CharacteristicUpdateCallback,
|
|
14
12
|
type Subscription,
|
|
15
|
-
type BleNitroManager,
|
|
16
13
|
type BleNitroManagerOptions,
|
|
17
14
|
BLEState,
|
|
18
15
|
AndroidScanMode,
|
|
16
|
+
BleNitroManager,
|
|
19
17
|
} from "./manager";
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export class BleNitro extends BleNitroManager {
|
|
24
|
-
public static instance() {
|
|
25
|
-
if (!_instance) {
|
|
26
|
-
_instance = new BleNitro();
|
|
27
|
-
}
|
|
28
|
-
return _instance;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
19
|
+
export { BleNitro } from './singleton';
|
package/src/manager.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BleNitroNativeFactory, { NativeBleNitro } from './specs/NativeBleNitroFactory';
|
|
2
2
|
import {
|
|
3
3
|
ScanFilter as NativeScanFilter,
|
|
4
4
|
BLEDevice as NativeBLEDevice,
|
|
@@ -32,6 +32,7 @@ export interface BLEDevice {
|
|
|
32
32
|
manufacturerData: ManufacturerData;
|
|
33
33
|
serviceUUIDs: string[];
|
|
34
34
|
isConnectable: boolean;
|
|
35
|
+
isConnected: boolean;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
export type ScanCallback = (device: BLEDevice) => void;
|
|
@@ -73,6 +74,7 @@ export enum AndroidScanMode {
|
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
export type BleNitroManagerOptions = {
|
|
77
|
+
restoreIdentifier?: string;
|
|
76
78
|
onRestoredState?: RestoreStateCallback;
|
|
77
79
|
};
|
|
78
80
|
|
|
@@ -123,16 +125,24 @@ export class BleNitroManager {
|
|
|
123
125
|
private _isScanning: boolean = false;
|
|
124
126
|
private _connectedDevices: { [deviceId: string]: boolean } = {};
|
|
125
127
|
|
|
126
|
-
private _restoredStateCallback: RestoreStateCallback | null
|
|
128
|
+
private _restoredStateCallback: RestoreStateCallback | null;
|
|
127
129
|
private _restoredState: BLEDevice[] | null = null;
|
|
130
|
+
private _restoreStateIdentifier: string | null = null;
|
|
131
|
+
|
|
132
|
+
private Instance: NativeBleNitro;
|
|
128
133
|
|
|
129
134
|
constructor(options?: BleNitroManagerOptions) {
|
|
130
|
-
this._restoredStateCallback = options?.onRestoredState
|
|
131
|
-
|
|
135
|
+
this._restoredStateCallback = options?.onRestoredState ?? null;
|
|
136
|
+
this._restoreStateIdentifier = options?.restoreIdentifier ?? null;
|
|
137
|
+
this.Instance = BleNitroNativeFactory.create(options?.restoreIdentifier, (peripherals: NativeBLEDevice[]) => this.onNativeRestoreStateCallback(peripherals));
|
|
132
138
|
}
|
|
133
139
|
|
|
134
140
|
private onNativeRestoreStateCallback(peripherals: NativeBLEDevice[]) {
|
|
141
|
+
if (!this._restoreStateIdentifier) return;
|
|
135
142
|
const bleDevices = peripherals.map((peripheral) => convertNativeBleDeviceToBleDevice(peripheral));
|
|
143
|
+
bleDevices.forEach((device) => {
|
|
144
|
+
this._connectedDevices[device.id] = device.isConnected;
|
|
145
|
+
});
|
|
136
146
|
if (this._restoredStateCallback) {
|
|
137
147
|
this._restoredStateCallback(bleDevices);
|
|
138
148
|
} else {
|
|
@@ -140,7 +150,13 @@ export class BleNitroManager {
|
|
|
140
150
|
}
|
|
141
151
|
}
|
|
142
152
|
|
|
153
|
+
/**
|
|
154
|
+
*
|
|
155
|
+
* Registers callback and returns restored peripheral state in it. Not working from 1.7.x upwards for singleton implementation!
|
|
156
|
+
* @deprecated This method is deprecated and will be removed in 2.x, use onRestoredState option in BleNitroManageroptions instead!
|
|
157
|
+
*/
|
|
143
158
|
public onRestoredState(callback: RestoreStateCallback) {
|
|
159
|
+
if (!this._restoreStateIdentifier) return;
|
|
144
160
|
if (this._restoredState) {
|
|
145
161
|
callback(this._restoredState);
|
|
146
162
|
this._restoredState = null;
|
|
@@ -209,7 +225,7 @@ export class BleNitroManager {
|
|
|
209
225
|
};
|
|
210
226
|
|
|
211
227
|
// Start scan
|
|
212
|
-
|
|
228
|
+
this.Instance.startScan(nativeFilter, scanCallback);
|
|
213
229
|
this._isScanning = true;
|
|
214
230
|
}
|
|
215
231
|
|
|
@@ -222,7 +238,7 @@ export class BleNitroManager {
|
|
|
222
238
|
return;
|
|
223
239
|
}
|
|
224
240
|
|
|
225
|
-
|
|
241
|
+
this.Instance.stopScan();
|
|
226
242
|
this._isScanning = false;
|
|
227
243
|
}
|
|
228
244
|
|
|
@@ -231,7 +247,7 @@ export class BleNitroManager {
|
|
|
231
247
|
* @returns Boolean indicating if currently scanning
|
|
232
248
|
*/
|
|
233
249
|
public isScanning(): boolean {
|
|
234
|
-
this._isScanning =
|
|
250
|
+
this._isScanning = this.Instance.isScanning();
|
|
235
251
|
return this._isScanning;
|
|
236
252
|
}
|
|
237
253
|
|
|
@@ -241,7 +257,7 @@ export class BleNitroManager {
|
|
|
241
257
|
* @returns Array of connected devices
|
|
242
258
|
*/
|
|
243
259
|
public getConnectedDevices(services?: string[]): BLEDevice[] {
|
|
244
|
-
const devices =
|
|
260
|
+
const devices = this.Instance.getConnectedDevices(services || []);
|
|
245
261
|
// Normalize service UUIDs - manufacturer data already comes as ArrayBuffers
|
|
246
262
|
return devices.map(device => convertNativeBleDeviceToBleDevice(device));
|
|
247
263
|
}
|
|
@@ -264,7 +280,7 @@ export class BleNitroManager {
|
|
|
264
280
|
return;
|
|
265
281
|
}
|
|
266
282
|
|
|
267
|
-
|
|
283
|
+
this.Instance.connect(
|
|
268
284
|
deviceId,
|
|
269
285
|
(success: boolean, connectedDeviceId: string, error: string) => {
|
|
270
286
|
if (success) {
|
|
@@ -330,7 +346,7 @@ export class BleNitroManager {
|
|
|
330
346
|
return;
|
|
331
347
|
}
|
|
332
348
|
|
|
333
|
-
|
|
349
|
+
this.Instance.disconnect(
|
|
334
350
|
deviceId,
|
|
335
351
|
(success: boolean, error: string) => {
|
|
336
352
|
if (success) {
|
|
@@ -350,7 +366,7 @@ export class BleNitroManager {
|
|
|
350
366
|
* @returns Boolean indicating if device is connected
|
|
351
367
|
*/
|
|
352
368
|
public isConnected(deviceId: string): boolean {
|
|
353
|
-
return
|
|
369
|
+
return this.Instance.isConnected(deviceId);
|
|
354
370
|
}
|
|
355
371
|
|
|
356
372
|
/**
|
|
@@ -361,7 +377,7 @@ export class BleNitroManager {
|
|
|
361
377
|
*/
|
|
362
378
|
public requestMTU(deviceId: string, mtu: number): number {
|
|
363
379
|
mtu = parseInt(mtu.toString(), 10);
|
|
364
|
-
const deviceMtu =
|
|
380
|
+
const deviceMtu = this.Instance.requestMTU(deviceId, mtu);
|
|
365
381
|
return deviceMtu;
|
|
366
382
|
}
|
|
367
383
|
|
|
@@ -378,7 +394,7 @@ export class BleNitroManager {
|
|
|
378
394
|
return;
|
|
379
395
|
}
|
|
380
396
|
|
|
381
|
-
|
|
397
|
+
this.Instance.readRSSI(
|
|
382
398
|
deviceId,
|
|
383
399
|
(success: boolean, rssi: number, error: string) => {
|
|
384
400
|
if (success) {
|
|
@@ -404,7 +420,7 @@ export class BleNitroManager {
|
|
|
404
420
|
return;
|
|
405
421
|
}
|
|
406
422
|
|
|
407
|
-
|
|
423
|
+
this.Instance.discoverServices(
|
|
408
424
|
deviceId,
|
|
409
425
|
(success: boolean, error: string) => {
|
|
410
426
|
if (success) {
|
|
@@ -435,7 +451,7 @@ export class BleNitroManager {
|
|
|
435
451
|
reject(new Error('Failed to discover services'));
|
|
436
452
|
return;
|
|
437
453
|
}
|
|
438
|
-
const services =
|
|
454
|
+
const services = this.Instance.getServices(deviceId);
|
|
439
455
|
resolve(BleNitroManager.normalizeGattUUIDs(services));
|
|
440
456
|
});
|
|
441
457
|
}
|
|
@@ -454,7 +470,7 @@ export class BleNitroManager {
|
|
|
454
470
|
throw new Error('Device not connected');
|
|
455
471
|
}
|
|
456
472
|
|
|
457
|
-
const characteristics =
|
|
473
|
+
const characteristics = this.Instance.getCharacteristics(
|
|
458
474
|
deviceId,
|
|
459
475
|
BleNitroManager.normalizeGattUUID(serviceId),
|
|
460
476
|
);
|
|
@@ -498,7 +514,7 @@ export class BleNitroManager {
|
|
|
498
514
|
return;
|
|
499
515
|
}
|
|
500
516
|
|
|
501
|
-
|
|
517
|
+
this.Instance.readCharacteristic(
|
|
502
518
|
deviceId,
|
|
503
519
|
BleNitroManager.normalizeGattUUID(serviceId),
|
|
504
520
|
BleNitroManager.normalizeGattUUID(characteristicId),
|
|
@@ -536,7 +552,7 @@ export class BleNitroManager {
|
|
|
536
552
|
return;
|
|
537
553
|
}
|
|
538
554
|
|
|
539
|
-
|
|
555
|
+
this.Instance.writeCharacteristic(
|
|
540
556
|
deviceId,
|
|
541
557
|
BleNitroManager.normalizeGattUUID(serviceId),
|
|
542
558
|
BleNitroManager.normalizeGattUUID(characteristicId),
|
|
@@ -576,7 +592,7 @@ export class BleNitroManager {
|
|
|
576
592
|
|
|
577
593
|
let _success = false;
|
|
578
594
|
|
|
579
|
-
|
|
595
|
+
this.Instance.subscribeToCharacteristic(
|
|
580
596
|
deviceId,
|
|
581
597
|
BleNitroManager.normalizeGattUUID(serviceId),
|
|
582
598
|
BleNitroManager.normalizeGattUUID(characteristicId),
|
|
@@ -624,7 +640,7 @@ export class BleNitroManager {
|
|
|
624
640
|
return;
|
|
625
641
|
}
|
|
626
642
|
|
|
627
|
-
|
|
643
|
+
this.Instance.unsubscribeFromCharacteristic(
|
|
628
644
|
deviceId,
|
|
629
645
|
BleNitroManager.normalizeGattUUID(serviceId),
|
|
630
646
|
BleNitroManager.normalizeGattUUID(characteristicId),
|
|
@@ -653,7 +669,7 @@ export class BleNitroManager {
|
|
|
653
669
|
*/
|
|
654
670
|
public requestBluetoothEnable(): Promise<boolean> {
|
|
655
671
|
return new Promise((resolve, reject) => {
|
|
656
|
-
|
|
672
|
+
this.Instance.requestBluetoothEnable(
|
|
657
673
|
(success: boolean, error: string) => {
|
|
658
674
|
if (success) {
|
|
659
675
|
resolve(true);
|
|
@@ -671,7 +687,7 @@ export class BleNitroManager {
|
|
|
671
687
|
* @see BLEState
|
|
672
688
|
*/
|
|
673
689
|
public state(): BLEState {
|
|
674
|
-
return mapNativeBLEStateToBLEState(
|
|
690
|
+
return mapNativeBLEStateToBLEState(this.Instance.state());
|
|
675
691
|
}
|
|
676
692
|
|
|
677
693
|
/**
|
|
@@ -687,13 +703,13 @@ export class BleNitroManager {
|
|
|
687
703
|
callback(state);
|
|
688
704
|
}
|
|
689
705
|
|
|
690
|
-
|
|
706
|
+
this.Instance.subscribeToStateChange((nativeState: NativeBLEState) => {
|
|
691
707
|
callback(mapNativeBLEStateToBLEState(nativeState));
|
|
692
708
|
});
|
|
693
709
|
|
|
694
710
|
return {
|
|
695
711
|
remove: () => {
|
|
696
|
-
|
|
712
|
+
this.Instance.unsubscribeFromStateChange();
|
|
697
713
|
},
|
|
698
714
|
};
|
|
699
715
|
}
|
|
@@ -703,6 +719,6 @@ export class BleNitroManager {
|
|
|
703
719
|
* @returns Promise resolving when settings are opened
|
|
704
720
|
*/
|
|
705
721
|
public openSettings(): Promise<void> {
|
|
706
|
-
return
|
|
722
|
+
return this.Instance.openSettings();
|
|
707
723
|
}
|
|
708
724
|
}
|
package/src/singleton.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BleNitroManager } from "./manager";
|
|
2
|
+
|
|
3
|
+
let _instance: BleNitroManager;
|
|
4
|
+
|
|
5
|
+
export class BleNitro extends BleNitroManager {
|
|
6
|
+
/**
|
|
7
|
+
* Get a singleton instance of BleNitro, will create one if it does not exist.
|
|
8
|
+
* Singleton implementation does not allow to use state restoration on iOS!
|
|
9
|
+
* @returns {BleNitroManager} An instance of BleNitro
|
|
10
|
+
*/
|
|
11
|
+
public static instance(): BleNitroManager {
|
|
12
|
+
if (!_instance) {
|
|
13
|
+
_instance = new BleNitro();
|
|
14
|
+
}
|
|
15
|
+
return _instance;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -29,6 +29,7 @@ export interface BLEDevice {
|
|
|
29
29
|
manufacturerData: ManufacturerData;
|
|
30
30
|
serviceUUIDs: string[];
|
|
31
31
|
isConnectable: boolean;
|
|
32
|
+
isConnected: boolean;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
export enum AndroidScanMode {
|
|
@@ -70,6 +71,7 @@ export type OperationResult = {
|
|
|
70
71
|
*/
|
|
71
72
|
export interface NativeBleNitro extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
|
|
72
73
|
// ios only
|
|
74
|
+
restoreStateIdentifier?: string;
|
|
73
75
|
setRestoreStateCallback(callback: RestoreCallback): void;
|
|
74
76
|
|
|
75
77
|
// Scanning operations
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
-
import
|
|
2
|
+
import { NativeBleNitro } from './NativeBleNitro.nitro';
|
|
3
3
|
|
|
4
4
|
// Export the native implementation
|
|
5
5
|
const NativeBleNitroImpl = NitroModules.createHybridObject<NativeBleNitro>('NativeBleNitro');
|
|
6
6
|
|
|
7
|
+
// export default NativeBleNitroImpl;
|
|
7
8
|
export default NativeBleNitroImpl;
|
|
8
9
|
export * from './NativeBleNitro.nitro';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { HybridObject } from "react-native-nitro-modules";
|
|
2
|
+
import { BLEDevice, NativeBleNitro } from "./NativeBleNitro.nitro";
|
|
3
|
+
|
|
4
|
+
export interface NativeBleNitroFactory extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
|
|
5
|
+
create(nativeRestoreStateIdentifier?: string, restoreStateCallback?: (peripherals: BLEDevice[]) => void): NativeBleNitro;
|
|
6
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
import { NativeBleNitroFactory } from './NativeBleNitroFactory.nitro';
|
|
3
|
+
|
|
4
|
+
// Export the native implementation
|
|
5
|
+
const NativeBleNitroFactoryImpl = NitroModules.createHybridObject<NativeBleNitroFactory>('NativeBleNitroFactory');
|
|
6
|
+
|
|
7
|
+
// export default NativeBleNitroImpl;
|
|
8
|
+
export default NativeBleNitroFactoryImpl;
|
|
9
|
+
export * from './NativeBleNitro.nitro';
|