react-native-ble-nitro 1.0.0 → 1.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.
- package/README.md +19 -15
- package/ios/BleNitroBleManager.swift +77 -43
- package/lib/commonjs/index.d.ts +15 -13
- package/lib/commonjs/index.d.ts.map +1 -1
- package/lib/commonjs/index.js +76 -139
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +13 -10
- package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
- package/lib/index.d.ts +15 -13
- package/lib/index.js +75 -138
- package/lib/specs/NativeBleNitro.nitro.d.ts +13 -10
- package/nitrogen/generated/android/BleNitroOnLoad.cpp +2 -8
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +90 -49
- package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +9 -10
- package/nitrogen/generated/android/c++/JOperationResult.hpp +58 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +11 -60
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/OperationResult.kt +32 -0
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +5 -29
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +69 -78
- package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +39 -24
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +9 -10
- package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +60 -112
- package/nitrogen/generated/ios/swift/OperationResult.swift +64 -0
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +0 -1
- package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +13 -11
- package/nitrogen/generated/shared/c++/OperationResult.hpp +72 -0
- package/package.json +1 -1
- package/src/__tests__/index.test.ts +19 -22
- package/src/index.ts +100 -156
- package/src/specs/NativeBleNitro.nitro.ts +14 -10
- package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +0 -74
- 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/lib/index.js
CHANGED
|
@@ -59,78 +59,54 @@ export class BleNitro {
|
|
|
59
59
|
* @returns Promise resolving to success state
|
|
60
60
|
*/
|
|
61
61
|
startScan(filter = {}, callback) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// Start scan
|
|
80
|
-
BleNitroNative.startScan(nativeFilter, scanCallback);
|
|
81
|
-
this._isScanning = true;
|
|
82
|
-
resolve(true);
|
|
83
|
-
});
|
|
62
|
+
if (this._isScanning) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Create native scan filter with defaults
|
|
66
|
+
const nativeFilter = {
|
|
67
|
+
serviceUUIDs: filter.serviceUUIDs || [],
|
|
68
|
+
rssiThreshold: filter.rssiThreshold ?? -100,
|
|
69
|
+
allowDuplicates: filter.allowDuplicates ?? false,
|
|
70
|
+
};
|
|
71
|
+
// Create callback wrapper
|
|
72
|
+
const scanCallback = (device) => {
|
|
73
|
+
device.serviceUUIDs = BleNitro.normalizeGattUUIDs(device.serviceUUIDs);
|
|
74
|
+
callback(device);
|
|
75
|
+
};
|
|
76
|
+
// Start scan
|
|
77
|
+
BleNitroNative.startScan(nativeFilter, scanCallback);
|
|
78
|
+
this._isScanning = true;
|
|
84
79
|
}
|
|
85
80
|
/**
|
|
86
81
|
* Stop scanning for Bluetooth devices
|
|
87
82
|
* @returns Promise resolving to success state
|
|
88
83
|
*/
|
|
89
84
|
stopScan() {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
BleNitroNative.stopScan((success, error) => {
|
|
97
|
-
if (success) {
|
|
98
|
-
this._isScanning = false;
|
|
99
|
-
resolve(true);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
reject(new Error(error));
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
});
|
|
85
|
+
if (!this._isScanning) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
BleNitroNative.stopScan();
|
|
106
89
|
}
|
|
107
90
|
/**
|
|
108
91
|
* Check if currently scanning for devices
|
|
109
92
|
* @returns Promise resolving to scanning state
|
|
110
93
|
*/
|
|
111
94
|
isScanning() {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this._isScanning = scanning;
|
|
115
|
-
resolve(scanning);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
95
|
+
this._isScanning = BleNitroNative.isScanning();
|
|
96
|
+
return this._isScanning;
|
|
118
97
|
}
|
|
119
98
|
/**
|
|
120
99
|
* Get all currently connected devices
|
|
121
|
-
* @
|
|
100
|
+
* @param services Optional list of service UUIDs to filter by
|
|
101
|
+
* @returns Array of connected devices
|
|
122
102
|
*/
|
|
123
|
-
getConnectedDevices() {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}));
|
|
131
|
-
resolve(normalizedDevices);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
103
|
+
getConnectedDevices(services) {
|
|
104
|
+
const devices = BleNitroNative.getConnectedDevices(services || []);
|
|
105
|
+
// Normalize service UUIDs for connected devices
|
|
106
|
+
return devices.map(device => ({
|
|
107
|
+
...device,
|
|
108
|
+
serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs)
|
|
109
|
+
}));
|
|
134
110
|
}
|
|
135
111
|
/**
|
|
136
112
|
* Connect to a Bluetooth device
|
|
@@ -169,13 +145,13 @@ export class BleNitro {
|
|
|
169
145
|
return new Promise((resolve, reject) => {
|
|
170
146
|
// Check if already disconnected
|
|
171
147
|
if (!this._connectedDevices[deviceId]) {
|
|
172
|
-
resolve(
|
|
148
|
+
resolve();
|
|
173
149
|
return;
|
|
174
150
|
}
|
|
175
151
|
BleNitroNative.disconnect(deviceId, (success, error) => {
|
|
176
152
|
if (success) {
|
|
177
153
|
delete this._connectedDevices[deviceId];
|
|
178
|
-
resolve(
|
|
154
|
+
resolve();
|
|
179
155
|
}
|
|
180
156
|
else {
|
|
181
157
|
reject(new Error(error));
|
|
@@ -189,12 +165,7 @@ export class BleNitro {
|
|
|
189
165
|
* @returns Promise resolving to connection state
|
|
190
166
|
*/
|
|
191
167
|
isConnected(deviceId) {
|
|
192
|
-
return
|
|
193
|
-
BleNitroNative.isConnected(deviceId, (connected) => {
|
|
194
|
-
this._connectedDevices[deviceId] = connected;
|
|
195
|
-
resolve(connected);
|
|
196
|
-
});
|
|
197
|
-
});
|
|
168
|
+
return BleNitroNative.isConnected(deviceId);
|
|
198
169
|
}
|
|
199
170
|
/**
|
|
200
171
|
* Discover services for a connected device
|
|
@@ -235,9 +206,8 @@ export class BleNitro {
|
|
|
235
206
|
reject(new Error('Failed to discover services'));
|
|
236
207
|
return;
|
|
237
208
|
}
|
|
238
|
-
BleNitroNative.getServices(deviceId
|
|
239
|
-
|
|
240
|
-
});
|
|
209
|
+
const services = BleNitroNative.getServices(deviceId);
|
|
210
|
+
resolve(BleNitro.normalizeGattUUIDs(services));
|
|
241
211
|
});
|
|
242
212
|
}
|
|
243
213
|
/**
|
|
@@ -247,16 +217,11 @@ export class BleNitro {
|
|
|
247
217
|
* @returns Promise resolving to array of characteristic UUIDs
|
|
248
218
|
*/
|
|
249
219
|
getCharacteristics(deviceId, serviceId) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
BleNitroNative.getCharacteristics(deviceId, BleNitro.normalizeGattUUID(serviceId), (characteristics) => {
|
|
257
|
-
resolve(BleNitro.normalizeGattUUIDs(characteristics));
|
|
258
|
-
});
|
|
259
|
-
});
|
|
220
|
+
if (!this._connectedDevices[deviceId]) {
|
|
221
|
+
throw new Error('Device not connected');
|
|
222
|
+
}
|
|
223
|
+
const characteristics = BleNitroNative.getCharacteristics(deviceId, BleNitro.normalizeGattUUID(serviceId));
|
|
224
|
+
return BleNitro.normalizeGattUUIDs(characteristics);
|
|
260
225
|
}
|
|
261
226
|
/**
|
|
262
227
|
* Read a characteristic value
|
|
@@ -317,29 +282,27 @@ export class BleNitro {
|
|
|
317
282
|
* @returns Promise resolving when subscription is complete
|
|
318
283
|
*/
|
|
319
284
|
subscribeToCharacteristic(deviceId, serviceId, characteristicId, callback) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
285
|
+
// Check if connected first
|
|
286
|
+
if (!this._connectedDevices[deviceId]) {
|
|
287
|
+
throw new Error('Device not connected');
|
|
288
|
+
}
|
|
289
|
+
let _success = false;
|
|
290
|
+
BleNitroNative.subscribeToCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (charId, data) => {
|
|
291
|
+
callback(charId, data);
|
|
292
|
+
}, (success, error) => {
|
|
293
|
+
_success = success;
|
|
294
|
+
if (!success) {
|
|
295
|
+
throw new Error(error);
|
|
325
296
|
}
|
|
326
|
-
BleNitroNative.subscribeToCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (charId, data) => {
|
|
327
|
-
callback(charId, data);
|
|
328
|
-
}, (success, error) => {
|
|
329
|
-
if (success) {
|
|
330
|
-
resolve({
|
|
331
|
-
remove: () => {
|
|
332
|
-
return new Promise((resolve, reject) => {
|
|
333
|
-
this.unsubscribeFromCharacteristic(deviceId, serviceId, characteristicId).then(resolve).catch(reject);
|
|
334
|
-
});
|
|
335
|
-
},
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
reject(new Error(error));
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
297
|
});
|
|
298
|
+
return {
|
|
299
|
+
remove: () => {
|
|
300
|
+
if (!_success) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
this.unsubscribeFromCharacteristic(deviceId, serviceId, characteristicId).catch(() => { });
|
|
304
|
+
}
|
|
305
|
+
};
|
|
343
306
|
}
|
|
344
307
|
/**
|
|
345
308
|
* Unsubscribe from characteristic notifications
|
|
@@ -370,14 +333,11 @@ export class BleNitro {
|
|
|
370
333
|
* @returns Promise resolving to Bluetooth state
|
|
371
334
|
*/
|
|
372
335
|
isBluetoothEnabled() {
|
|
373
|
-
return
|
|
374
|
-
BleNitroNative.isBluetoothEnabled((enabled) => {
|
|
375
|
-
resolve(enabled);
|
|
376
|
-
});
|
|
377
|
-
});
|
|
336
|
+
return this.state() === BLEState.PoweredOn;
|
|
378
337
|
}
|
|
379
338
|
/**
|
|
380
339
|
* Request to enable Bluetooth
|
|
340
|
+
* Only works on Android
|
|
381
341
|
* @returns Promise resolving when Bluetooth is enabled
|
|
382
342
|
*/
|
|
383
343
|
requestBluetoothEnable() {
|
|
@@ -398,11 +358,7 @@ export class BleNitro {
|
|
|
398
358
|
* @see BLEState
|
|
399
359
|
*/
|
|
400
360
|
state() {
|
|
401
|
-
return
|
|
402
|
-
BleNitroNative.state((state) => {
|
|
403
|
-
resolve(mapNativeBLEStateToBLEState(state));
|
|
404
|
-
});
|
|
405
|
-
});
|
|
361
|
+
return mapNativeBLEStateToBLEState(BleNitroNative.state());
|
|
406
362
|
}
|
|
407
363
|
/**
|
|
408
364
|
* Subscribe to Bluetooth state changes
|
|
@@ -412,37 +368,18 @@ export class BleNitro {
|
|
|
412
368
|
* @see BLEState
|
|
413
369
|
*/
|
|
414
370
|
subscribeToStateChange(callback, emitInitial = false) {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
BleNitroNative.subscribeToStateChange((nativeState) => {
|
|
423
|
-
callback(mapNativeBLEStateToBLEState(nativeState));
|
|
424
|
-
}, (success, error) => {
|
|
425
|
-
if (success) {
|
|
426
|
-
resolve({
|
|
427
|
-
remove: () => {
|
|
428
|
-
return new Promise((resolve, reject) => {
|
|
429
|
-
BleNitroNative.unsubscribeFromStateChange((success, error) => {
|
|
430
|
-
if (success) {
|
|
431
|
-
resolve();
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
reject(new Error(error));
|
|
435
|
-
}
|
|
436
|
-
});
|
|
437
|
-
});
|
|
438
|
-
},
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
else {
|
|
442
|
-
reject(new Error(error));
|
|
443
|
-
}
|
|
444
|
-
});
|
|
371
|
+
if (emitInitial) {
|
|
372
|
+
const state = this.state();
|
|
373
|
+
callback(state);
|
|
374
|
+
}
|
|
375
|
+
BleNitroNative.subscribeToStateChange((nativeState) => {
|
|
376
|
+
callback(mapNativeBLEStateToBLEState(nativeState));
|
|
445
377
|
});
|
|
378
|
+
return {
|
|
379
|
+
remove: () => {
|
|
380
|
+
BleNitroNative.unsubscribeFromStateChange();
|
|
381
|
+
},
|
|
382
|
+
};
|
|
446
383
|
}
|
|
447
384
|
/**
|
|
448
385
|
* Open Bluetooth settings
|
|
@@ -37,6 +37,10 @@ export type StateCallback = (state: BLEState) => void;
|
|
|
37
37
|
export type BooleanCallback = (result: boolean) => void;
|
|
38
38
|
export type StringArrayCallback = (result: string[]) => void;
|
|
39
39
|
export type ReadCharacteristicCallback = (success: boolean, data: number[], error: string) => void;
|
|
40
|
+
export type OperationResult = {
|
|
41
|
+
success: boolean;
|
|
42
|
+
error?: string;
|
|
43
|
+
};
|
|
40
44
|
/**
|
|
41
45
|
* Native BLE Nitro Module Specification
|
|
42
46
|
* Defines the interface between TypeScript and native implementations
|
|
@@ -46,23 +50,22 @@ export interface NativeBleNitro extends HybridObject<{
|
|
|
46
50
|
android: 'kotlin';
|
|
47
51
|
}> {
|
|
48
52
|
startScan(filter: ScanFilter, callback: ScanCallback): void;
|
|
49
|
-
stopScan(
|
|
50
|
-
isScanning(
|
|
51
|
-
getConnectedDevices(
|
|
53
|
+
stopScan(): boolean;
|
|
54
|
+
isScanning(): boolean;
|
|
55
|
+
getConnectedDevices(services: string[]): BLEDevice[];
|
|
52
56
|
connect(deviceId: string, callback: ConnectionCallback, disconnectCallback?: DisconnectionEventCallback): void;
|
|
53
57
|
disconnect(deviceId: string, callback: OperationCallback): void;
|
|
54
|
-
isConnected(deviceId: string
|
|
58
|
+
isConnected(deviceId: string): boolean;
|
|
55
59
|
discoverServices(deviceId: string, callback: OperationCallback): void;
|
|
56
|
-
getServices(deviceId: string
|
|
57
|
-
getCharacteristics(deviceId: string, serviceId: string
|
|
60
|
+
getServices(deviceId: string): string[];
|
|
61
|
+
getCharacteristics(deviceId: string, serviceId: string): string[];
|
|
58
62
|
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: ReadCharacteristicCallback): void;
|
|
59
63
|
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: number[], withResponse: boolean, callback: OperationCallback): void;
|
|
60
64
|
subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, updateCallback: CharacteristicCallback, resultCallback: OperationCallback): void;
|
|
61
65
|
unsubscribeFromCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: OperationCallback): void;
|
|
62
|
-
isBluetoothEnabled(callback: BooleanCallback): void;
|
|
63
66
|
requestBluetoothEnable(callback: OperationCallback): void;
|
|
64
|
-
state(
|
|
65
|
-
subscribeToStateChange(stateCallback: StateCallback
|
|
66
|
-
unsubscribeFromStateChange(
|
|
67
|
+
state(): BLEState;
|
|
68
|
+
subscribeToStateChange(stateCallback: StateCallback): OperationResult;
|
|
69
|
+
unsubscribeFromStateChange(): OperationResult;
|
|
67
70
|
openSettings(): Promise<void>;
|
|
68
71
|
}
|
|
@@ -17,12 +17,9 @@
|
|
|
17
17
|
|
|
18
18
|
#include "JHybridNativeBleNitroSpec.hpp"
|
|
19
19
|
#include "JFunc_void_BLEDevice.hpp"
|
|
20
|
-
#include "JFunc_void_bool_std__string.hpp"
|
|
21
|
-
#include "JFunc_void_bool.hpp"
|
|
22
|
-
#include "JFunc_void_std__vector_BLEDevice_.hpp"
|
|
23
20
|
#include "JFunc_void_bool_std__string_std__string.hpp"
|
|
24
21
|
#include "JFunc_void_std__string_bool_std__string.hpp"
|
|
25
|
-
#include "
|
|
22
|
+
#include "JFunc_void_bool_std__string.hpp"
|
|
26
23
|
#include "JFunc_void_bool_std__vector_double__std__string.hpp"
|
|
27
24
|
#include "JFunc_void_std__string_std__vector_double_.hpp"
|
|
28
25
|
#include "JFunc_void_BLEState.hpp"
|
|
@@ -39,12 +36,9 @@ int initialize(JavaVM* vm) {
|
|
|
39
36
|
// Register native JNI methods
|
|
40
37
|
margelo::nitro::co::zyke::ble::JHybridNativeBleNitroSpec::registerNatives();
|
|
41
38
|
margelo::nitro::co::zyke::ble::JFunc_void_BLEDevice_cxx::registerNatives();
|
|
42
|
-
margelo::nitro::co::zyke::ble::JFunc_void_bool_std__string_cxx::registerNatives();
|
|
43
|
-
margelo::nitro::co::zyke::ble::JFunc_void_bool_cxx::registerNatives();
|
|
44
|
-
margelo::nitro::co::zyke::ble::JFunc_void_std__vector_BLEDevice__cxx::registerNatives();
|
|
45
39
|
margelo::nitro::co::zyke::ble::JFunc_void_bool_std__string_std__string_cxx::registerNatives();
|
|
46
40
|
margelo::nitro::co::zyke::ble::JFunc_void_std__string_bool_std__string_cxx::registerNatives();
|
|
47
|
-
margelo::nitro::co::zyke::ble::
|
|
41
|
+
margelo::nitro::co::zyke::ble::JFunc_void_bool_std__string_cxx::registerNatives();
|
|
48
42
|
margelo::nitro::co::zyke::ble::JFunc_void_bool_std__vector_double__std__string_cxx::registerNatives();
|
|
49
43
|
margelo::nitro::co::zyke::ble::JFunc_void_std__string_std__vector_double__cxx::registerNatives();
|
|
50
44
|
margelo::nitro::co::zyke::ble::JFunc_void_BLEState_cxx::registerNatives();
|
|
@@ -7,8 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
#include "JHybridNativeBleNitroSpec.hpp"
|
|
9
9
|
|
|
10
|
-
// Forward declaration of `ScanFilter` to properly resolve imports.
|
|
11
|
-
namespace margelo::nitro::co::zyke::ble { struct ScanFilter; }
|
|
12
10
|
// Forward declaration of `BLEDevice` to properly resolve imports.
|
|
13
11
|
namespace margelo::nitro::co::zyke::ble { struct BLEDevice; }
|
|
14
12
|
// Forward declaration of `ManufacturerData` to properly resolve imports.
|
|
@@ -17,33 +15,36 @@ namespace margelo::nitro::co::zyke::ble { struct ManufacturerData; }
|
|
|
17
15
|
namespace margelo::nitro::co::zyke::ble { struct ManufacturerDataEntry; }
|
|
18
16
|
// Forward declaration of `BLEState` to properly resolve imports.
|
|
19
17
|
namespace margelo::nitro::co::zyke::ble { enum class BLEState; }
|
|
18
|
+
// Forward declaration of `OperationResult` to properly resolve imports.
|
|
19
|
+
namespace margelo::nitro::co::zyke::ble { struct OperationResult; }
|
|
20
|
+
// Forward declaration of `ScanFilter` to properly resolve imports.
|
|
21
|
+
namespace margelo::nitro::co::zyke::ble { struct ScanFilter; }
|
|
20
22
|
|
|
21
|
-
#include <NitroModules/Promise.hpp>
|
|
22
|
-
#include <NitroModules/JPromise.hpp>
|
|
23
|
-
#include "ScanFilter.hpp"
|
|
24
|
-
#include "JScanFilter.hpp"
|
|
25
|
-
#include <string>
|
|
26
|
-
#include <vector>
|
|
27
23
|
#include "BLEDevice.hpp"
|
|
28
|
-
#include <
|
|
29
|
-
#include "JFunc_void_BLEDevice.hpp"
|
|
24
|
+
#include <vector>
|
|
30
25
|
#include "JBLEDevice.hpp"
|
|
26
|
+
#include <string>
|
|
31
27
|
#include "ManufacturerData.hpp"
|
|
32
28
|
#include "JManufacturerData.hpp"
|
|
33
29
|
#include "ManufacturerDataEntry.hpp"
|
|
34
30
|
#include "JManufacturerDataEntry.hpp"
|
|
35
|
-
#include "
|
|
36
|
-
#include "
|
|
37
|
-
#include "
|
|
38
|
-
#include "
|
|
31
|
+
#include "BLEState.hpp"
|
|
32
|
+
#include "JBLEState.hpp"
|
|
33
|
+
#include "OperationResult.hpp"
|
|
34
|
+
#include "JOperationResult.hpp"
|
|
39
35
|
#include <optional>
|
|
36
|
+
#include <NitroModules/Promise.hpp>
|
|
37
|
+
#include <NitroModules/JPromise.hpp>
|
|
38
|
+
#include "ScanFilter.hpp"
|
|
39
|
+
#include "JScanFilter.hpp"
|
|
40
|
+
#include <functional>
|
|
41
|
+
#include "JFunc_void_BLEDevice.hpp"
|
|
42
|
+
#include "JFunc_void_bool_std__string_std__string.hpp"
|
|
40
43
|
#include "JFunc_void_std__string_bool_std__string.hpp"
|
|
41
|
-
#include "
|
|
44
|
+
#include "JFunc_void_bool_std__string.hpp"
|
|
42
45
|
#include "JFunc_void_bool_std__vector_double__std__string.hpp"
|
|
43
46
|
#include "JFunc_void_std__string_std__vector_double_.hpp"
|
|
44
|
-
#include "BLEState.hpp"
|
|
45
47
|
#include "JFunc_void_BLEState.hpp"
|
|
46
|
-
#include "JBLEState.hpp"
|
|
47
48
|
|
|
48
49
|
namespace margelo::nitro::co::zyke::ble {
|
|
49
50
|
|
|
@@ -75,17 +76,37 @@ namespace margelo::nitro::co::zyke::ble {
|
|
|
75
76
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JScanFilter> /* filter */, jni::alias_ref<JFunc_void_BLEDevice::javaobject> /* callback */)>("startScan_cxx");
|
|
76
77
|
method(_javaPart, JScanFilter::fromCpp(filter), JFunc_void_BLEDevice_cxx::fromCpp(callback));
|
|
77
78
|
}
|
|
78
|
-
|
|
79
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
80
|
-
method(_javaPart
|
|
81
|
-
|
|
82
|
-
void JHybridNativeBleNitroSpec::isScanning(const std::function<void(bool /* result */)>& callback) {
|
|
83
|
-
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_bool::javaobject> /* callback */)>("isScanning_cxx");
|
|
84
|
-
method(_javaPart, JFunc_void_bool_cxx::fromCpp(callback));
|
|
79
|
+
bool JHybridNativeBleNitroSpec::stopScan() {
|
|
80
|
+
static const auto method = javaClassStatic()->getMethod<jboolean()>("stopScan");
|
|
81
|
+
auto __result = method(_javaPart);
|
|
82
|
+
return static_cast<bool>(__result);
|
|
85
83
|
}
|
|
86
|
-
|
|
87
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
88
|
-
method(_javaPart
|
|
84
|
+
bool JHybridNativeBleNitroSpec::isScanning() {
|
|
85
|
+
static const auto method = javaClassStatic()->getMethod<jboolean()>("isScanning");
|
|
86
|
+
auto __result = method(_javaPart);
|
|
87
|
+
return static_cast<bool>(__result);
|
|
88
|
+
}
|
|
89
|
+
std::vector<BLEDevice> JHybridNativeBleNitroSpec::getConnectedDevices(const std::vector<std::string>& services) {
|
|
90
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JArrayClass<JBLEDevice>>(jni::alias_ref<jni::JArrayClass<jni::JString>> /* services */)>("getConnectedDevices");
|
|
91
|
+
auto __result = method(_javaPart, [&]() {
|
|
92
|
+
size_t __size = services.size();
|
|
93
|
+
jni::local_ref<jni::JArrayClass<jni::JString>> __array = jni::JArrayClass<jni::JString>::newArray(__size);
|
|
94
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
95
|
+
const auto& __element = services[__i];
|
|
96
|
+
__array->setElement(__i, *jni::make_jstring(__element));
|
|
97
|
+
}
|
|
98
|
+
return __array;
|
|
99
|
+
}());
|
|
100
|
+
return [&]() {
|
|
101
|
+
size_t __size = __result->size();
|
|
102
|
+
std::vector<BLEDevice> __vector;
|
|
103
|
+
__vector.reserve(__size);
|
|
104
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
105
|
+
auto __element = __result->getElement(__i);
|
|
106
|
+
__vector.push_back(__element->toCpp());
|
|
107
|
+
}
|
|
108
|
+
return __vector;
|
|
109
|
+
}();
|
|
89
110
|
}
|
|
90
111
|
void JHybridNativeBleNitroSpec::connect(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* deviceId */, const std::string& /* error */)>& callback, const std::optional<std::function<void(const std::string& /* deviceId */, bool /* interrupted */, const std::string& /* error */)>>& disconnectCallback) {
|
|
91
112
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<JFunc_void_bool_std__string_std__string::javaobject> /* callback */, jni::alias_ref<JFunc_void_std__string_bool_std__string::javaobject> /* disconnectCallback */)>("connect_cxx");
|
|
@@ -95,21 +116,42 @@ namespace margelo::nitro::co::zyke::ble {
|
|
|
95
116
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<JFunc_void_bool_std__string::javaobject> /* callback */)>("disconnect_cxx");
|
|
96
117
|
method(_javaPart, jni::make_jstring(deviceId), JFunc_void_bool_std__string_cxx::fromCpp(callback));
|
|
97
118
|
}
|
|
98
|
-
|
|
99
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
100
|
-
method(_javaPart, jni::make_jstring(deviceId)
|
|
119
|
+
bool JHybridNativeBleNitroSpec::isConnected(const std::string& deviceId) {
|
|
120
|
+
static const auto method = javaClassStatic()->getMethod<jboolean(jni::alias_ref<jni::JString> /* deviceId */)>("isConnected");
|
|
121
|
+
auto __result = method(_javaPart, jni::make_jstring(deviceId));
|
|
122
|
+
return static_cast<bool>(__result);
|
|
101
123
|
}
|
|
102
124
|
void JHybridNativeBleNitroSpec::discoverServices(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) {
|
|
103
125
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<JFunc_void_bool_std__string::javaobject> /* callback */)>("discoverServices_cxx");
|
|
104
126
|
method(_javaPart, jni::make_jstring(deviceId), JFunc_void_bool_std__string_cxx::fromCpp(callback));
|
|
105
127
|
}
|
|
106
|
-
|
|
107
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
108
|
-
method(_javaPart, jni::make_jstring(deviceId)
|
|
128
|
+
std::vector<std::string> JHybridNativeBleNitroSpec::getServices(const std::string& deviceId) {
|
|
129
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JArrayClass<jni::JString>>(jni::alias_ref<jni::JString> /* deviceId */)>("getServices");
|
|
130
|
+
auto __result = method(_javaPart, jni::make_jstring(deviceId));
|
|
131
|
+
return [&]() {
|
|
132
|
+
size_t __size = __result->size();
|
|
133
|
+
std::vector<std::string> __vector;
|
|
134
|
+
__vector.reserve(__size);
|
|
135
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
136
|
+
auto __element = __result->getElement(__i);
|
|
137
|
+
__vector.push_back(__element->toStdString());
|
|
138
|
+
}
|
|
139
|
+
return __vector;
|
|
140
|
+
}();
|
|
109
141
|
}
|
|
110
|
-
|
|
111
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
112
|
-
method(_javaPart, jni::make_jstring(deviceId), jni::make_jstring(serviceId)
|
|
142
|
+
std::vector<std::string> JHybridNativeBleNitroSpec::getCharacteristics(const std::string& deviceId, const std::string& serviceId) {
|
|
143
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<jni::JArrayClass<jni::JString>>(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<jni::JString> /* serviceId */)>("getCharacteristics");
|
|
144
|
+
auto __result = method(_javaPart, jni::make_jstring(deviceId), jni::make_jstring(serviceId));
|
|
145
|
+
return [&]() {
|
|
146
|
+
size_t __size = __result->size();
|
|
147
|
+
std::vector<std::string> __vector;
|
|
148
|
+
__vector.reserve(__size);
|
|
149
|
+
for (size_t __i = 0; __i < __size; __i++) {
|
|
150
|
+
auto __element = __result->getElement(__i);
|
|
151
|
+
__vector.push_back(__element->toStdString());
|
|
152
|
+
}
|
|
153
|
+
return __vector;
|
|
154
|
+
}();
|
|
113
155
|
}
|
|
114
156
|
void JHybridNativeBleNitroSpec::readCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(bool /* success */, const std::vector<double>& /* data */, const std::string& /* error */)>& callback) {
|
|
115
157
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<jni::JString> /* serviceId */, jni::alias_ref<jni::JString> /* characteristicId */, jni::alias_ref<JFunc_void_bool_std__vector_double__std__string::javaobject> /* callback */)>("readCharacteristic_cxx");
|
|
@@ -132,25 +174,24 @@ namespace margelo::nitro::co::zyke::ble {
|
|
|
132
174
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* deviceId */, jni::alias_ref<jni::JString> /* serviceId */, jni::alias_ref<jni::JString> /* characteristicId */, jni::alias_ref<JFunc_void_bool_std__string::javaobject> /* callback */)>("unsubscribeFromCharacteristic_cxx");
|
|
133
175
|
method(_javaPart, jni::make_jstring(deviceId), jni::make_jstring(serviceId), jni::make_jstring(characteristicId), JFunc_void_bool_std__string_cxx::fromCpp(callback));
|
|
134
176
|
}
|
|
135
|
-
void JHybridNativeBleNitroSpec::isBluetoothEnabled(const std::function<void(bool /* result */)>& callback) {
|
|
136
|
-
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_bool::javaobject> /* callback */)>("isBluetoothEnabled_cxx");
|
|
137
|
-
method(_javaPart, JFunc_void_bool_cxx::fromCpp(callback));
|
|
138
|
-
}
|
|
139
177
|
void JHybridNativeBleNitroSpec::requestBluetoothEnable(const std::function<void(bool /* success */, const std::string& /* error */)>& callback) {
|
|
140
178
|
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JFunc_void_bool_std__string::javaobject> /* callback */)>("requestBluetoothEnable_cxx");
|
|
141
179
|
method(_javaPart, JFunc_void_bool_std__string_cxx::fromCpp(callback));
|
|
142
180
|
}
|
|
143
|
-
|
|
144
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
145
|
-
method(_javaPart
|
|
181
|
+
BLEState JHybridNativeBleNitroSpec::state() {
|
|
182
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JBLEState>()>("state");
|
|
183
|
+
auto __result = method(_javaPart);
|
|
184
|
+
return __result->toCpp();
|
|
146
185
|
}
|
|
147
|
-
|
|
148
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
149
|
-
method(_javaPart, JFunc_void_BLEState_cxx::fromCpp(stateCallback)
|
|
186
|
+
OperationResult JHybridNativeBleNitroSpec::subscribeToStateChange(const std::function<void(BLEState /* state */)>& stateCallback) {
|
|
187
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JOperationResult>(jni::alias_ref<JFunc_void_BLEState::javaobject> /* stateCallback */)>("subscribeToStateChange_cxx");
|
|
188
|
+
auto __result = method(_javaPart, JFunc_void_BLEState_cxx::fromCpp(stateCallback));
|
|
189
|
+
return __result->toCpp();
|
|
150
190
|
}
|
|
151
|
-
|
|
152
|
-
static const auto method = javaClassStatic()->getMethod<
|
|
153
|
-
method(_javaPart
|
|
191
|
+
OperationResult JHybridNativeBleNitroSpec::unsubscribeFromStateChange() {
|
|
192
|
+
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JOperationResult>()>("unsubscribeFromStateChange");
|
|
193
|
+
auto __result = method(_javaPart);
|
|
194
|
+
return __result->toCpp();
|
|
154
195
|
}
|
|
155
196
|
std::shared_ptr<Promise<void>> JHybridNativeBleNitroSpec::openSettings() {
|
|
156
197
|
static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>()>("openSettings");
|
|
@@ -54,24 +54,23 @@ namespace margelo::nitro::co::zyke::ble {
|
|
|
54
54
|
public:
|
|
55
55
|
// Methods
|
|
56
56
|
void startScan(const ScanFilter& filter, const std::function<void(const BLEDevice& /* device */)>& callback) override;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
bool stopScan() override;
|
|
58
|
+
bool isScanning() override;
|
|
59
|
+
std::vector<BLEDevice> getConnectedDevices(const std::vector<std::string>& services) override;
|
|
60
60
|
void connect(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* deviceId */, const std::string& /* error */)>& callback, const std::optional<std::function<void(const std::string& /* deviceId */, bool /* interrupted */, const std::string& /* error */)>>& disconnectCallback) override;
|
|
61
61
|
void disconnect(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) override;
|
|
62
|
-
|
|
62
|
+
bool isConnected(const std::string& deviceId) override;
|
|
63
63
|
void discoverServices(const std::string& deviceId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) override;
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
std::vector<std::string> getServices(const std::string& deviceId) override;
|
|
65
|
+
std::vector<std::string> getCharacteristics(const std::string& deviceId, const std::string& serviceId) override;
|
|
66
66
|
void readCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(bool /* success */, const std::vector<double>& /* data */, const std::string& /* error */)>& callback) override;
|
|
67
67
|
void writeCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::vector<double>& data, bool withResponse, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) override;
|
|
68
68
|
void subscribeToCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(const std::string& /* characteristicId */, const std::vector<double>& /* data */)>& updateCallback, const std::function<void(bool /* success */, const std::string& /* error */)>& resultCallback) override;
|
|
69
69
|
void unsubscribeFromCharacteristic(const std::string& deviceId, const std::string& serviceId, const std::string& characteristicId, const std::function<void(bool /* success */, const std::string& /* error */)>& callback) override;
|
|
70
|
-
void isBluetoothEnabled(const std::function<void(bool /* result */)>& callback) override;
|
|
71
70
|
void requestBluetoothEnable(const std::function<void(bool /* success */, const std::string& /* error */)>& callback) override;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
BLEState state() override;
|
|
72
|
+
OperationResult subscribeToStateChange(const std::function<void(BLEState /* state */)>& stateCallback) override;
|
|
73
|
+
OperationResult unsubscribeFromStateChange() override;
|
|
75
74
|
std::shared_ptr<Promise<void>> openSettings() override;
|
|
76
75
|
|
|
77
76
|
private:
|