react-native-ble-nitro 1.0.0 → 1.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/README.md +25 -17
- package/ios/BleNitroBleManager.swift +126 -57
- package/ios/BlePeripheralDelegate.swift +36 -8
- package/lib/commonjs/index.d.ts +29 -20
- package/lib/commonjs/index.d.ts.map +1 -1
- package/lib/commonjs/index.js +113 -143
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +19 -14
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.js.map +1 -1
- package/lib/index.d.ts +29 -20
- package/lib/index.js +112 -142
- package/lib/specs/NativeBleNitro.nitro.d.ts +19 -14
- package/nitrogen/generated/android/BleNitroOnLoad.cpp +6 -12
- package/nitrogen/generated/android/c++/JBLEDevice.hpp +3 -0
- package/nitrogen/generated/android/c++/JFunc_void_BLEDevice.hpp +3 -0
- package/nitrogen/generated/android/c++/JFunc_void_bool_std__shared_ptr_ArrayBuffer__std__string.hpp +78 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string_std__shared_ptr_ArrayBuffer_.hpp +78 -0
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +111 -65
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +13 -13
- package/nitrogen/generated/android/c++/JManufacturerData.hpp +3 -0
- package/nitrogen/generated/android/c++/JManufacturerDataEntry.hpp +7 -15
- package/nitrogen/generated/android/c++/JOperationResult.hpp +58 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/{Func_void_bool_std__vector_double__std__string.kt → Func_void_bool_std__shared_ptr_ArrayBuffer__std__string.kt} +12 -12
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/{Func_void_std__string_std__vector_double_.kt → Func_void_std__string_std__shared_ptr_ArrayBuffer_.kt} +12 -12
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +20 -65
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/ManufacturerDataEntry.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/OperationResult.kt +32 -0
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +15 -39
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +108 -113
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +6 -0
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +57 -28
- package/nitrogen/generated/ios/swift/{Func_void_bool_std__vector_double__std__string.swift → Func_void_bool_std__shared_ptr_ArrayBuffer__std__string.swift} +11 -11
- package/nitrogen/generated/ios/swift/{Func_void_std__string_std__vector_double_.swift → Func_void_std__string_std__shared_ptr_ArrayBuffer_.swift} +11 -11
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +13 -13
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +84 -136
- package/nitrogen/generated/ios/swift/ManufacturerDataEntry.swift +5 -17
- package/nitrogen/generated/ios/swift/OperationResult.swift +64 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +20 -14
- package/nitrogen/generated/shared/c++/ManufacturerDataEntry.hpp +8 -7
- package/nitrogen/generated/shared/c++/OperationResult.hpp +72 -0
- package/package.json +1 -1
- package/react-native.config.js +10 -2
- package/src/__tests__/index.test.ts +19 -22
- package/src/index.ts +147 -164
- package/src/specs/NativeBleNitro.nitro.ts +22 -14
- package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +0 -74
- package/nitrogen/generated/android/c++/JFunc_void_bool_std__vector_double__std__string.hpp +0 -86
- package/nitrogen/generated/android/c++/JFunc_void_std__string_std__vector_double_.hpp +0 -86
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +0 -99
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +0 -93
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_bool.kt +0 -81
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +0 -81
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_std__string_.kt +0 -81
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +0 -47
- package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +0 -47
- package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +0 -47
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
BLEState as NativeBLEState,
|
|
6
6
|
} from './specs/NativeBleNitro';
|
|
7
7
|
|
|
8
|
+
export type ByteArray = number[];
|
|
9
|
+
|
|
8
10
|
export interface ScanFilter {
|
|
9
11
|
serviceUUIDs?: string[];
|
|
10
12
|
rssiThreshold?: number;
|
|
@@ -13,7 +15,7 @@ export interface ScanFilter {
|
|
|
13
15
|
|
|
14
16
|
export interface ManufacturerDataEntry {
|
|
15
17
|
id: string;
|
|
16
|
-
data:
|
|
18
|
+
data: ByteArray;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export interface ManufacturerData {
|
|
@@ -43,11 +45,11 @@ export type DisconnectEventCallback = (
|
|
|
43
45
|
export type OperationCallback = (success: boolean, error: string) => void;
|
|
44
46
|
export type CharacteristicUpdateCallback = (
|
|
45
47
|
characteristicId: string,
|
|
46
|
-
data:
|
|
48
|
+
data: ByteArray
|
|
47
49
|
) => void;
|
|
48
50
|
|
|
49
51
|
export type Subscription = {
|
|
50
|
-
remove: () =>
|
|
52
|
+
remove: () => void;
|
|
51
53
|
};
|
|
52
54
|
|
|
53
55
|
export enum BLEState {
|
|
@@ -71,6 +73,14 @@ function mapNativeBLEStateToBLEState(nativeState: NativeBLEState): BLEState {
|
|
|
71
73
|
return map[nativeState];
|
|
72
74
|
}
|
|
73
75
|
|
|
76
|
+
function arrayBufferToByteArray(buffer: ArrayBuffer): ByteArray {
|
|
77
|
+
return Array.from(new Uint8Array(buffer));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function byteArrayToArrayBuffer(data: ByteArray): ArrayBuffer {
|
|
81
|
+
return new Uint8Array(data).buffer;
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
let _instance: BleNitro;
|
|
75
85
|
|
|
76
86
|
export class BleNitro {
|
|
@@ -117,85 +127,78 @@ export class BleNitro {
|
|
|
117
127
|
public startScan(
|
|
118
128
|
filter: ScanFilter = {},
|
|
119
129
|
callback: ScanCallback
|
|
120
|
-
):
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
resolve(true);
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Create native scan filter with defaults
|
|
129
|
-
const nativeFilter: NativeScanFilter = {
|
|
130
|
-
serviceUUIDs: filter.serviceUUIDs || [],
|
|
131
|
-
rssiThreshold: filter.rssiThreshold ?? -100,
|
|
132
|
-
allowDuplicates: filter.allowDuplicates ?? false,
|
|
133
|
-
};
|
|
130
|
+
): void {
|
|
131
|
+
if (this._isScanning) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
134
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
// Create native scan filter with defaults
|
|
136
|
+
const nativeFilter: NativeScanFilter = {
|
|
137
|
+
serviceUUIDs: filter.serviceUUIDs || [],
|
|
138
|
+
rssiThreshold: filter.rssiThreshold ?? -100,
|
|
139
|
+
allowDuplicates: filter.allowDuplicates ?? false,
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// Create callback wrapper
|
|
143
|
+
const scanCallback = (device: NativeBLEDevice) => {
|
|
144
|
+
// Convert manufacturer data to Uint8Arrays
|
|
145
|
+
const convertedDevice: BLEDevice = {
|
|
146
|
+
...device,
|
|
147
|
+
serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
|
|
148
|
+
manufacturerData: {
|
|
149
|
+
companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
|
|
150
|
+
id: entry.id,
|
|
151
|
+
data: arrayBufferToByteArray(entry.data)
|
|
152
|
+
}))
|
|
153
|
+
}
|
|
139
154
|
};
|
|
155
|
+
callback(convertedDevice);
|
|
156
|
+
};
|
|
140
157
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
resolve(true);
|
|
145
|
-
});
|
|
158
|
+
// Start scan
|
|
159
|
+
BleNitroNative.startScan(nativeFilter, scanCallback);
|
|
160
|
+
this._isScanning = true;
|
|
146
161
|
}
|
|
147
162
|
|
|
148
163
|
/**
|
|
149
164
|
* Stop scanning for Bluetooth devices
|
|
150
165
|
* @returns Promise resolving to success state
|
|
151
166
|
*/
|
|
152
|
-
public stopScan():
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
resolve(true);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
167
|
+
public stopScan(): void {
|
|
168
|
+
if (!this._isScanning) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
159
171
|
|
|
160
|
-
|
|
161
|
-
if (success) {
|
|
162
|
-
this._isScanning = false;
|
|
163
|
-
resolve(true);
|
|
164
|
-
} else {
|
|
165
|
-
reject(new Error(error));
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
});
|
|
172
|
+
BleNitroNative.stopScan();
|
|
169
173
|
}
|
|
170
174
|
|
|
171
175
|
/**
|
|
172
176
|
* Check if currently scanning for devices
|
|
173
177
|
* @returns Promise resolving to scanning state
|
|
174
178
|
*/
|
|
175
|
-
public isScanning():
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
this._isScanning = scanning;
|
|
179
|
-
resolve(scanning);
|
|
180
|
-
});
|
|
181
|
-
});
|
|
179
|
+
public isScanning(): boolean {
|
|
180
|
+
this._isScanning = BleNitroNative.isScanning();
|
|
181
|
+
return this._isScanning;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
/**
|
|
185
185
|
* Get all currently connected devices
|
|
186
|
-
* @
|
|
186
|
+
* @param services Optional list of service UUIDs to filter by
|
|
187
|
+
* @returns Array of connected devices
|
|
187
188
|
*/
|
|
188
|
-
public getConnectedDevices():
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
189
|
+
public getConnectedDevices(services?: string[]): BLEDevice[] {
|
|
190
|
+
const devices = BleNitroNative.getConnectedDevices(services || []);
|
|
191
|
+
// Normalize service UUIDs - manufacturer data already comes as ArrayBuffers
|
|
192
|
+
return devices.map(device => ({
|
|
193
|
+
...device,
|
|
194
|
+
serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
|
|
195
|
+
manufacturerData: {
|
|
196
|
+
companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
|
|
197
|
+
id: entry.id,
|
|
198
|
+
data: arrayBufferToByteArray(entry.data)
|
|
199
|
+
}))
|
|
200
|
+
}
|
|
201
|
+
}));
|
|
199
202
|
}
|
|
200
203
|
|
|
201
204
|
/**
|
|
@@ -239,11 +242,11 @@ export class BleNitro {
|
|
|
239
242
|
* @param deviceId ID of the device to disconnect from
|
|
240
243
|
* @returns Promise resolving when disconnected
|
|
241
244
|
*/
|
|
242
|
-
public disconnect(deviceId: string): Promise<
|
|
245
|
+
public disconnect(deviceId: string): Promise<void> {
|
|
243
246
|
return new Promise((resolve, reject) => {
|
|
244
247
|
// Check if already disconnected
|
|
245
248
|
if (!this._connectedDevices[deviceId]) {
|
|
246
|
-
resolve(
|
|
249
|
+
resolve();
|
|
247
250
|
return;
|
|
248
251
|
}
|
|
249
252
|
|
|
@@ -252,7 +255,7 @@ export class BleNitro {
|
|
|
252
255
|
(success: boolean, error: string) => {
|
|
253
256
|
if (success) {
|
|
254
257
|
delete this._connectedDevices[deviceId];
|
|
255
|
-
resolve(
|
|
258
|
+
resolve();
|
|
256
259
|
} else {
|
|
257
260
|
reject(new Error(error));
|
|
258
261
|
}
|
|
@@ -266,13 +269,20 @@ export class BleNitro {
|
|
|
266
269
|
* @param deviceId ID of the device to check
|
|
267
270
|
* @returns Promise resolving to connection state
|
|
268
271
|
*/
|
|
269
|
-
public isConnected(deviceId: string):
|
|
270
|
-
return
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
272
|
+
public isConnected(deviceId: string): boolean {
|
|
273
|
+
return BleNitroNative.isConnected(deviceId);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Request a new MTU size
|
|
278
|
+
* @param deviceId ID of the device
|
|
279
|
+
* @param mtu New MTU size, min is 23, max is 517
|
|
280
|
+
* @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
|
|
281
|
+
*/
|
|
282
|
+
public requestMTU(deviceId: string, mtu: number): number {
|
|
283
|
+
mtu = parseInt(mtu.toString(), 10);
|
|
284
|
+
const deviceMtu = BleNitroNative.requestMTU(deviceId, mtu);
|
|
285
|
+
return deviceMtu;
|
|
276
286
|
}
|
|
277
287
|
|
|
278
288
|
/**
|
|
@@ -319,9 +329,8 @@ export class BleNitro {
|
|
|
319
329
|
reject(new Error('Failed to discover services'));
|
|
320
330
|
return;
|
|
321
331
|
}
|
|
322
|
-
BleNitroNative.getServices(deviceId
|
|
323
|
-
|
|
324
|
-
});
|
|
332
|
+
const services = BleNitroNative.getServices(deviceId);
|
|
333
|
+
resolve(BleNitro.normalizeGattUUIDs(services));
|
|
325
334
|
});
|
|
326
335
|
}
|
|
327
336
|
|
|
@@ -334,22 +343,16 @@ export class BleNitro {
|
|
|
334
343
|
public getCharacteristics(
|
|
335
344
|
deviceId: string,
|
|
336
345
|
serviceId: string
|
|
337
|
-
):
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
reject(new Error('Device not connected'));
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
346
|
+
): string[] {
|
|
347
|
+
if (!this._connectedDevices[deviceId]) {
|
|
348
|
+
throw new Error('Device not connected');
|
|
349
|
+
}
|
|
344
350
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
);
|
|
352
|
-
});
|
|
351
|
+
const characteristics = BleNitroNative.getCharacteristics(
|
|
352
|
+
deviceId,
|
|
353
|
+
BleNitro.normalizeGattUUID(serviceId),
|
|
354
|
+
);
|
|
355
|
+
return BleNitro.normalizeGattUUIDs(characteristics);
|
|
353
356
|
}
|
|
354
357
|
|
|
355
358
|
/**
|
|
@@ -357,13 +360,13 @@ export class BleNitro {
|
|
|
357
360
|
* @param deviceId ID of the device
|
|
358
361
|
* @param serviceId ID of the service
|
|
359
362
|
* @param characteristicId ID of the characteristic
|
|
360
|
-
* @returns Promise resolving to the characteristic data as
|
|
363
|
+
* @returns Promise resolving to the characteristic data as ByteArray
|
|
361
364
|
*/
|
|
362
365
|
public readCharacteristic(
|
|
363
366
|
deviceId: string,
|
|
364
367
|
serviceId: string,
|
|
365
368
|
characteristicId: string
|
|
366
|
-
): Promise<
|
|
369
|
+
): Promise<ByteArray> {
|
|
367
370
|
return new Promise((resolve, reject) => {
|
|
368
371
|
// Check if connected first
|
|
369
372
|
if (!this._connectedDevices[deviceId]) {
|
|
@@ -375,9 +378,9 @@ export class BleNitro {
|
|
|
375
378
|
deviceId,
|
|
376
379
|
BleNitro.normalizeGattUUID(serviceId),
|
|
377
380
|
BleNitro.normalizeGattUUID(characteristicId),
|
|
378
|
-
(success: boolean, data:
|
|
381
|
+
(success: boolean, data: ArrayBuffer, error: string) => {
|
|
379
382
|
if (success) {
|
|
380
|
-
resolve(data);
|
|
383
|
+
resolve(arrayBufferToByteArray(data));
|
|
381
384
|
} else {
|
|
382
385
|
reject(new Error(error));
|
|
383
386
|
}
|
|
@@ -391,7 +394,7 @@ export class BleNitro {
|
|
|
391
394
|
* @param deviceId ID of the device
|
|
392
395
|
* @param serviceId ID of the service
|
|
393
396
|
* @param characteristicId ID of the characteristic
|
|
394
|
-
* @param data Data to write as
|
|
397
|
+
* @param data Data to write as ByteArray(number[])
|
|
395
398
|
* @param withResponse Whether to wait for response
|
|
396
399
|
* @returns Promise resolving when write is complete
|
|
397
400
|
*/
|
|
@@ -399,7 +402,7 @@ export class BleNitro {
|
|
|
399
402
|
deviceId: string,
|
|
400
403
|
serviceId: string,
|
|
401
404
|
characteristicId: string,
|
|
402
|
-
data:
|
|
405
|
+
data: ByteArray,
|
|
403
406
|
withResponse: boolean = true
|
|
404
407
|
): Promise<boolean> {
|
|
405
408
|
return new Promise((resolve, reject) => {
|
|
@@ -409,11 +412,13 @@ export class BleNitro {
|
|
|
409
412
|
return;
|
|
410
413
|
}
|
|
411
414
|
|
|
415
|
+
const dataAsArrayBuffer = byteArrayToArrayBuffer(data);
|
|
416
|
+
|
|
412
417
|
BleNitroNative.writeCharacteristic(
|
|
413
418
|
deviceId,
|
|
414
419
|
BleNitro.normalizeGattUUID(serviceId),
|
|
415
420
|
BleNitro.normalizeGattUUID(characteristicId),
|
|
416
|
-
|
|
421
|
+
dataAsArrayBuffer,
|
|
417
422
|
withResponse,
|
|
418
423
|
(success: boolean, error: string) => {
|
|
419
424
|
if (success) {
|
|
@@ -439,40 +444,41 @@ export class BleNitro {
|
|
|
439
444
|
serviceId: string,
|
|
440
445
|
characteristicId: string,
|
|
441
446
|
callback: CharacteristicUpdateCallback
|
|
442
|
-
):
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
447
|
+
): Subscription {
|
|
448
|
+
// Check if connected first
|
|
449
|
+
if (!this._connectedDevices[deviceId]) {
|
|
450
|
+
throw new Error('Device not connected');
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
let _success = false;
|
|
454
|
+
|
|
455
|
+
BleNitroNative.subscribeToCharacteristic(
|
|
456
|
+
deviceId,
|
|
457
|
+
BleNitro.normalizeGattUUID(serviceId),
|
|
458
|
+
BleNitro.normalizeGattUUID(characteristicId),
|
|
459
|
+
(charId: string, data: ArrayBuffer) => {
|
|
460
|
+
callback(charId, arrayBufferToByteArray(data));
|
|
461
|
+
},
|
|
462
|
+
(success, error) => {
|
|
463
|
+
_success = success;
|
|
464
|
+
if (!success) {
|
|
465
|
+
throw new Error(error);
|
|
466
|
+
}
|
|
448
467
|
}
|
|
468
|
+
);
|
|
449
469
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
(charId: string, data: number[]) => {
|
|
455
|
-
callback(charId, data);
|
|
456
|
-
},
|
|
457
|
-
(success: boolean, error: string) => {
|
|
458
|
-
if (success) {
|
|
459
|
-
resolve({
|
|
460
|
-
remove: () => {
|
|
461
|
-
return new Promise((resolve, reject) => {
|
|
462
|
-
this.unsubscribeFromCharacteristic(
|
|
463
|
-
deviceId,
|
|
464
|
-
serviceId,
|
|
465
|
-
characteristicId
|
|
466
|
-
).then(resolve).catch(reject);
|
|
467
|
-
});
|
|
468
|
-
},
|
|
469
|
-
});
|
|
470
|
-
} else {
|
|
471
|
-
reject(new Error(error));
|
|
472
|
-
}
|
|
470
|
+
return {
|
|
471
|
+
remove: () => {
|
|
472
|
+
if (!_success) {
|
|
473
|
+
return;
|
|
473
474
|
}
|
|
474
|
-
|
|
475
|
-
|
|
475
|
+
this.unsubscribeFromCharacteristic(
|
|
476
|
+
deviceId,
|
|
477
|
+
serviceId,
|
|
478
|
+
characteristicId
|
|
479
|
+
).catch(() => {});
|
|
480
|
+
}
|
|
481
|
+
};
|
|
476
482
|
}
|
|
477
483
|
|
|
478
484
|
/**
|
|
@@ -513,16 +519,12 @@ export class BleNitro {
|
|
|
513
519
|
* Check if Bluetooth is enabled
|
|
514
520
|
* @returns Promise resolving to Bluetooth state
|
|
515
521
|
*/
|
|
516
|
-
public isBluetoothEnabled():
|
|
517
|
-
return
|
|
518
|
-
BleNitroNative.isBluetoothEnabled((enabled: boolean) => {
|
|
519
|
-
resolve(enabled);
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
+
public isBluetoothEnabled(): boolean {
|
|
523
|
+
return this.state() === BLEState.PoweredOn;
|
|
522
524
|
}
|
|
523
525
|
|
|
524
526
|
/**
|
|
525
|
-
* Request to enable Bluetooth
|
|
527
|
+
* Request to enable Bluetooth (Android only)
|
|
526
528
|
* @returns Promise resolving when Bluetooth is enabled
|
|
527
529
|
*/
|
|
528
530
|
public requestBluetoothEnable(): Promise<boolean> {
|
|
@@ -544,12 +546,8 @@ export class BleNitro {
|
|
|
544
546
|
* @returns Promise resolving to Bluetooth state
|
|
545
547
|
* @see BLEState
|
|
546
548
|
*/
|
|
547
|
-
public state():
|
|
548
|
-
return
|
|
549
|
-
BleNitroNative.state((state: NativeBLEState) => {
|
|
550
|
-
resolve(mapNativeBLEStateToBLEState(state));
|
|
551
|
-
});
|
|
552
|
-
});
|
|
549
|
+
public state(): BLEState {
|
|
550
|
+
return mapNativeBLEStateToBLEState(BleNitroNative.state());
|
|
553
551
|
}
|
|
554
552
|
|
|
555
553
|
/**
|
|
@@ -559,36 +557,21 @@ export class BleNitro {
|
|
|
559
557
|
* @returns Promise resolving when subscription is complete
|
|
560
558
|
* @see BLEState
|
|
561
559
|
*/
|
|
562
|
-
public subscribeToStateChange(callback: (state: BLEState) => void, emitInitial = false):
|
|
563
|
-
return new Promise(async (resolve, reject) => {
|
|
560
|
+
public subscribeToStateChange(callback: (state: BLEState) => void, emitInitial = false): Subscription {
|
|
564
561
|
if (emitInitial) {
|
|
565
|
-
const state =
|
|
566
|
-
return BLEState.Unknown;
|
|
567
|
-
});
|
|
562
|
+
const state = this.state();
|
|
568
563
|
callback(state);
|
|
569
564
|
}
|
|
565
|
+
|
|
570
566
|
BleNitroNative.subscribeToStateChange((nativeState: NativeBLEState) => {
|
|
571
567
|
callback(mapNativeBLEStateToBLEState(nativeState));
|
|
572
|
-
}, (success: boolean, error: string) => {
|
|
573
|
-
if (success) {
|
|
574
|
-
resolve({
|
|
575
|
-
remove: () => {
|
|
576
|
-
return new Promise((resolve, reject) => {
|
|
577
|
-
BleNitroNative.unsubscribeFromStateChange((success, error) => {
|
|
578
|
-
if (success) {
|
|
579
|
-
resolve();
|
|
580
|
-
} else {
|
|
581
|
-
reject(new Error(error));
|
|
582
|
-
}
|
|
583
|
-
});
|
|
584
|
-
});
|
|
585
|
-
},
|
|
586
|
-
});
|
|
587
|
-
} else {
|
|
588
|
-
reject(new Error(error));
|
|
589
|
-
}
|
|
590
568
|
});
|
|
591
|
-
|
|
569
|
+
|
|
570
|
+
return {
|
|
571
|
+
remove: () => {
|
|
572
|
+
BleNitroNative.unsubscribeFromStateChange();
|
|
573
|
+
},
|
|
574
|
+
};
|
|
592
575
|
}
|
|
593
576
|
|
|
594
577
|
/**
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { HybridObject } from 'react-native-nitro-modules';
|
|
2
2
|
|
|
3
|
+
// Type alias for BLE data - ArrayBuffers for efficient binary data handling
|
|
4
|
+
export type BLEValue = ArrayBuffer;
|
|
5
|
+
|
|
3
6
|
// Nitro constraint: Use numeric enums instead of string unions
|
|
4
7
|
export enum BLEState {
|
|
5
8
|
Unknown = 0,
|
|
@@ -12,7 +15,7 @@ export enum BLEState {
|
|
|
12
15
|
|
|
13
16
|
export interface ManufacturerDataEntry {
|
|
14
17
|
id: string;
|
|
15
|
-
data:
|
|
18
|
+
data: BLEValue;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
export interface ManufacturerData {
|
|
@@ -39,11 +42,16 @@ export type DevicesCallback = (devices: BLEDevice[]) => void;
|
|
|
39
42
|
export type ConnectionCallback = (success: boolean, deviceId: string, error: string) => void;
|
|
40
43
|
export type DisconnectionEventCallback = (deviceId: string, interrupted: boolean, error: string) => void;
|
|
41
44
|
export type OperationCallback = (success: boolean, error: string) => void;
|
|
42
|
-
export type CharacteristicCallback = (characteristicId: string, data:
|
|
45
|
+
export type CharacteristicCallback = (characteristicId: string, data: BLEValue) => void;
|
|
43
46
|
export type StateCallback = (state: BLEState) => void;
|
|
44
47
|
export type BooleanCallback = (result: boolean) => void;
|
|
45
48
|
export type StringArrayCallback = (result: string[]) => void;
|
|
46
|
-
export type ReadCharacteristicCallback = (success: boolean, data:
|
|
49
|
+
export type ReadCharacteristicCallback = (success: boolean, data: BLEValue, error: string) => void;
|
|
50
|
+
|
|
51
|
+
export type OperationResult = {
|
|
52
|
+
success: boolean;
|
|
53
|
+
error?: string;
|
|
54
|
+
};
|
|
47
55
|
|
|
48
56
|
/**
|
|
49
57
|
* Native BLE Nitro Module Specification
|
|
@@ -52,33 +60,33 @@ export type ReadCharacteristicCallback = (success: boolean, data: number[], erro
|
|
|
52
60
|
export interface NativeBleNitro extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
|
|
53
61
|
// Scanning operations
|
|
54
62
|
startScan(filter: ScanFilter, callback: ScanCallback): void;
|
|
55
|
-
stopScan(
|
|
56
|
-
isScanning(
|
|
63
|
+
stopScan(): boolean;
|
|
64
|
+
isScanning(): boolean;
|
|
57
65
|
|
|
58
66
|
// Device discovery
|
|
59
|
-
getConnectedDevices(
|
|
67
|
+
getConnectedDevices(services: string[]): BLEDevice[];
|
|
60
68
|
|
|
61
69
|
// Connection management
|
|
62
70
|
connect(deviceId: string, callback: ConnectionCallback, disconnectCallback?: DisconnectionEventCallback): void;
|
|
63
71
|
disconnect(deviceId: string, callback: OperationCallback): void;
|
|
64
|
-
isConnected(deviceId: string
|
|
72
|
+
isConnected(deviceId: string): boolean;
|
|
73
|
+
requestMTU(deviceId: string, mtu: number): number;
|
|
65
74
|
|
|
66
75
|
// Service discovery
|
|
67
76
|
discoverServices(deviceId: string, callback: OperationCallback): void;
|
|
68
|
-
getServices(deviceId: string
|
|
69
|
-
getCharacteristics(deviceId: string, serviceId: string
|
|
77
|
+
getServices(deviceId: string): string[];
|
|
78
|
+
getCharacteristics(deviceId: string, serviceId: string): string[];
|
|
70
79
|
|
|
71
80
|
// Characteristic operations
|
|
72
81
|
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: ReadCharacteristicCallback): void;
|
|
73
|
-
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data:
|
|
82
|
+
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: BLEValue, withResponse: boolean, callback: OperationCallback): void;
|
|
74
83
|
subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, updateCallback: CharacteristicCallback, resultCallback: OperationCallback): void;
|
|
75
84
|
unsubscribeFromCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: OperationCallback): void;
|
|
76
85
|
|
|
77
86
|
// Bluetooth state management
|
|
78
|
-
isBluetoothEnabled(callback: BooleanCallback): void;
|
|
79
87
|
requestBluetoothEnable(callback: OperationCallback): void;
|
|
80
|
-
state(
|
|
81
|
-
subscribeToStateChange(stateCallback: StateCallback
|
|
82
|
-
unsubscribeFromStateChange(
|
|
88
|
+
state(): BLEState;
|
|
89
|
+
subscribeToStateChange(stateCallback: StateCallback): OperationResult;
|
|
90
|
+
unsubscribeFromStateChange(): OperationResult;
|
|
83
91
|
openSettings(): Promise<void>;
|
|
84
92
|
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
///
|
|
2
|
-
/// JFunc_void_bool.hpp
|
|
3
|
-
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
-
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright © 2025 Marc Rousavy @ Margelo
|
|
6
|
-
///
|
|
7
|
-
|
|
8
|
-
#pragma once
|
|
9
|
-
|
|
10
|
-
#include <fbjni/fbjni.h>
|
|
11
|
-
#include <functional>
|
|
12
|
-
|
|
13
|
-
#include <functional>
|
|
14
|
-
|
|
15
|
-
namespace margelo::nitro::co::zyke::ble {
|
|
16
|
-
|
|
17
|
-
using namespace facebook;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Represents the Java/Kotlin callback `(result: Boolean) -> Unit`.
|
|
21
|
-
* This can be passed around between C++ and Java/Kotlin.
|
|
22
|
-
*/
|
|
23
|
-
struct JFunc_void_bool: public jni::JavaClass<JFunc_void_bool> {
|
|
24
|
-
public:
|
|
25
|
-
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/co/zyke/ble/Func_void_bool;";
|
|
26
|
-
|
|
27
|
-
public:
|
|
28
|
-
/**
|
|
29
|
-
* Invokes the function this `JFunc_void_bool` instance holds through JNI.
|
|
30
|
-
*/
|
|
31
|
-
void invoke(bool result) const {
|
|
32
|
-
static const auto method = javaClassStatic()->getMethod<void(jboolean /* result */)>("invoke");
|
|
33
|
-
method(self(), result);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* An implementation of Func_void_bool that is backed by a C++ implementation (using `std::function<...>`)
|
|
39
|
-
*/
|
|
40
|
-
struct JFunc_void_bool_cxx final: public jni::HybridClass<JFunc_void_bool_cxx, JFunc_void_bool> {
|
|
41
|
-
public:
|
|
42
|
-
static jni::local_ref<JFunc_void_bool::javaobject> fromCpp(const std::function<void(bool /* result */)>& func) {
|
|
43
|
-
return JFunc_void_bool_cxx::newObjectCxxArgs(func);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public:
|
|
47
|
-
/**
|
|
48
|
-
* Invokes the C++ `std::function<...>` this `JFunc_void_bool_cxx` instance holds.
|
|
49
|
-
*/
|
|
50
|
-
void invoke_cxx(jboolean result) {
|
|
51
|
-
_func(static_cast<bool>(result));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public:
|
|
55
|
-
[[nodiscard]]
|
|
56
|
-
inline const std::function<void(bool /* result */)>& getFunction() const {
|
|
57
|
-
return _func;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public:
|
|
61
|
-
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/co/zyke/ble/Func_void_bool_cxx;";
|
|
62
|
-
static void registerNatives() {
|
|
63
|
-
registerHybrid({makeNativeMethod("invoke_cxx", JFunc_void_bool_cxx::invoke_cxx)});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private:
|
|
67
|
-
explicit JFunc_void_bool_cxx(const std::function<void(bool /* result */)>& func): _func(func) { }
|
|
68
|
-
|
|
69
|
-
private:
|
|
70
|
-
friend HybridBase;
|
|
71
|
-
std::function<void(bool /* result */)> _func;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
} // namespace margelo::nitro::co::zyke::ble
|