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.
Files changed (58) hide show
  1. package/README.md +25 -17
  2. package/ios/BleNitroBleManager.swift +126 -57
  3. package/ios/BlePeripheralDelegate.swift +36 -8
  4. package/lib/commonjs/index.d.ts +29 -20
  5. package/lib/commonjs/index.d.ts.map +1 -1
  6. package/lib/commonjs/index.js +113 -143
  7. package/lib/commonjs/index.js.map +1 -1
  8. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +19 -14
  9. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
  10. package/lib/commonjs/specs/NativeBleNitro.nitro.js.map +1 -1
  11. package/lib/index.d.ts +29 -20
  12. package/lib/index.js +112 -142
  13. package/lib/specs/NativeBleNitro.nitro.d.ts +19 -14
  14. package/nitrogen/generated/android/BleNitroOnLoad.cpp +6 -12
  15. package/nitrogen/generated/android/c++/JBLEDevice.hpp +3 -0
  16. package/nitrogen/generated/android/c++/JFunc_void_BLEDevice.hpp +3 -0
  17. package/nitrogen/generated/android/c++/JFunc_void_bool_std__shared_ptr_ArrayBuffer__std__string.hpp +78 -0
  18. package/nitrogen/generated/android/c++/JFunc_void_std__string_std__shared_ptr_ArrayBuffer_.hpp +78 -0
  19. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +111 -65
  20. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +13 -13
  21. package/nitrogen/generated/android/c++/JManufacturerData.hpp +3 -0
  22. package/nitrogen/generated/android/c++/JManufacturerDataEntry.hpp +7 -15
  23. package/nitrogen/generated/android/c++/JOperationResult.hpp +58 -0
  24. 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
  25. 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
  26. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +20 -65
  27. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/ManufacturerDataEntry.kt +1 -1
  28. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/OperationResult.kt +32 -0
  29. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +15 -39
  30. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +108 -113
  31. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Umbrella.hpp +6 -0
  32. package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +57 -28
  33. 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
  34. package/nitrogen/generated/ios/swift/{Func_void_std__string_std__vector_double_.swift → Func_void_std__string_std__shared_ptr_ArrayBuffer_.swift} +11 -11
  35. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +13 -13
  36. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +84 -136
  37. package/nitrogen/generated/ios/swift/ManufacturerDataEntry.swift +5 -17
  38. package/nitrogen/generated/ios/swift/OperationResult.swift +64 -0
  39. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -1
  40. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +20 -14
  41. package/nitrogen/generated/shared/c++/ManufacturerDataEntry.hpp +8 -7
  42. package/nitrogen/generated/shared/c++/OperationResult.hpp +72 -0
  43. package/package.json +1 -1
  44. package/react-native.config.js +10 -2
  45. package/src/__tests__/index.test.ts +19 -22
  46. package/src/index.ts +147 -164
  47. package/src/specs/NativeBleNitro.nitro.ts +22 -14
  48. package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +0 -74
  49. package/nitrogen/generated/android/c++/JFunc_void_bool_std__vector_double__std__string.hpp +0 -86
  50. package/nitrogen/generated/android/c++/JFunc_void_std__string_std__vector_double_.hpp +0 -86
  51. package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +0 -99
  52. package/nitrogen/generated/android/c++/JFunc_void_std__vector_std__string_.hpp +0 -93
  53. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_bool.kt +0 -81
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +0 -81
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_std__string_.kt +0 -81
  56. package/nitrogen/generated/ios/swift/Func_void_bool.swift +0 -47
  57. package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +0 -47
  58. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +0 -47
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export type ByteArray = number[];
1
2
  export interface ScanFilter {
2
3
  serviceUUIDs?: string[];
3
4
  rssiThreshold?: number;
@@ -5,7 +6,7 @@ export interface ScanFilter {
5
6
  }
6
7
  export interface ManufacturerDataEntry {
7
8
  id: string;
8
- data: number[];
9
+ data: ByteArray;
9
10
  }
10
11
  export interface ManufacturerData {
11
12
  companyIdentifiers: ManufacturerDataEntry[];
@@ -22,9 +23,9 @@ export type ScanCallback = (device: BLEDevice) => void;
22
23
  export type ConnectionCallback = (success: boolean, deviceId: string, error: string) => void;
23
24
  export type DisconnectEventCallback = (deviceId: string, interrupted: boolean, error: string) => void;
24
25
  export type OperationCallback = (success: boolean, error: string) => void;
25
- export type CharacteristicUpdateCallback = (characteristicId: string, data: number[]) => void;
26
+ export type CharacteristicUpdateCallback = (characteristicId: string, data: ByteArray) => void;
26
27
  export type Subscription = {
27
- remove: () => Promise<void>;
28
+ remove: () => void;
28
29
  };
29
30
  export declare enum BLEState {
30
31
  Unknown = "Unknown",
@@ -52,22 +53,23 @@ export declare class BleNitro {
52
53
  * @param callback Callback function called when a device is found
53
54
  * @returns Promise resolving to success state
54
55
  */
55
- startScan(filter: ScanFilter | undefined, callback: ScanCallback): Promise<boolean>;
56
+ startScan(filter: ScanFilter | undefined, callback: ScanCallback): void;
56
57
  /**
57
58
  * Stop scanning for Bluetooth devices
58
59
  * @returns Promise resolving to success state
59
60
  */
60
- stopScan(): Promise<boolean>;
61
+ stopScan(): void;
61
62
  /**
62
63
  * Check if currently scanning for devices
63
64
  * @returns Promise resolving to scanning state
64
65
  */
65
- isScanning(): Promise<boolean>;
66
+ isScanning(): boolean;
66
67
  /**
67
68
  * Get all currently connected devices
68
- * @returns Promise resolving to array of connected devices
69
+ * @param services Optional list of service UUIDs to filter by
70
+ * @returns Array of connected devices
69
71
  */
70
- getConnectedDevices(): Promise<BLEDevice[]>;
72
+ getConnectedDevices(services?: string[]): BLEDevice[];
71
73
  /**
72
74
  * Connect to a Bluetooth device
73
75
  * @param deviceId ID of the device to connect to
@@ -80,13 +82,20 @@ export declare class BleNitro {
80
82
  * @param deviceId ID of the device to disconnect from
81
83
  * @returns Promise resolving when disconnected
82
84
  */
83
- disconnect(deviceId: string): Promise<boolean>;
85
+ disconnect(deviceId: string): Promise<void>;
84
86
  /**
85
87
  * Check if connected to a device
86
88
  * @param deviceId ID of the device to check
87
89
  * @returns Promise resolving to connection state
88
90
  */
89
- isConnected(deviceId: string): Promise<boolean>;
91
+ isConnected(deviceId: string): boolean;
92
+ /**
93
+ * Request a new MTU size
94
+ * @param deviceId ID of the device
95
+ * @param mtu New MTU size, min is 23, max is 517
96
+ * @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
97
+ */
98
+ requestMTU(deviceId: string, mtu: number): number;
90
99
  /**
91
100
  * Discover services for a connected device
92
101
  * @param deviceId ID of the device
@@ -105,25 +114,25 @@ export declare class BleNitro {
105
114
  * @param serviceId ID of the service
106
115
  * @returns Promise resolving to array of characteristic UUIDs
107
116
  */
108
- getCharacteristics(deviceId: string, serviceId: string): Promise<string[]>;
117
+ getCharacteristics(deviceId: string, serviceId: string): string[];
109
118
  /**
110
119
  * Read a characteristic value
111
120
  * @param deviceId ID of the device
112
121
  * @param serviceId ID of the service
113
122
  * @param characteristicId ID of the characteristic
114
- * @returns Promise resolving to the characteristic data as byte array
123
+ * @returns Promise resolving to the characteristic data as ByteArray
115
124
  */
116
- readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<number[]>;
125
+ readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ByteArray>;
117
126
  /**
118
127
  * Write a value to a characteristic
119
128
  * @param deviceId ID of the device
120
129
  * @param serviceId ID of the service
121
130
  * @param characteristicId ID of the characteristic
122
- * @param data Data to write as an array of bytes
131
+ * @param data Data to write as ByteArray(number[])
123
132
  * @param withResponse Whether to wait for response
124
133
  * @returns Promise resolving when write is complete
125
134
  */
126
- writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: number[], withResponse?: boolean): Promise<boolean>;
135
+ writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: ByteArray, withResponse?: boolean): Promise<boolean>;
127
136
  /**
128
137
  * Subscribe to characteristic notifications
129
138
  * @param deviceId ID of the device
@@ -132,7 +141,7 @@ export declare class BleNitro {
132
141
  * @param callback Callback function called when notification is received
133
142
  * @returns Promise resolving when subscription is complete
134
143
  */
135
- subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: CharacteristicUpdateCallback): Promise<Subscription>;
144
+ subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: CharacteristicUpdateCallback): Subscription;
136
145
  /**
137
146
  * Unsubscribe from characteristic notifications
138
147
  * @param deviceId ID of the device
@@ -145,9 +154,9 @@ export declare class BleNitro {
145
154
  * Check if Bluetooth is enabled
146
155
  * @returns Promise resolving to Bluetooth state
147
156
  */
148
- isBluetoothEnabled(): Promise<boolean>;
157
+ isBluetoothEnabled(): boolean;
149
158
  /**
150
- * Request to enable Bluetooth
159
+ * Request to enable Bluetooth (Android only)
151
160
  * @returns Promise resolving when Bluetooth is enabled
152
161
  */
153
162
  requestBluetoothEnable(): Promise<boolean>;
@@ -156,7 +165,7 @@ export declare class BleNitro {
156
165
  * @returns Promise resolving to Bluetooth state
157
166
  * @see BLEState
158
167
  */
159
- state(): Promise<BLEState>;
168
+ state(): BLEState;
160
169
  /**
161
170
  * Subscribe to Bluetooth state changes
162
171
  * @param callback Callback function called when state changes
@@ -164,7 +173,7 @@ export declare class BleNitro {
164
173
  * @returns Promise resolving when subscription is complete
165
174
  * @see BLEState
166
175
  */
167
- subscribeToStateChange(callback: (state: BLEState) => void, emitInitial?: boolean): Promise<Subscription>;
176
+ subscribeToStateChange(callback: (state: BLEState) => void, emitInitial?: boolean): Subscription;
168
177
  /**
169
178
  * Open Bluetooth settings
170
179
  * @returns Promise resolving when settings are opened
package/lib/index.js CHANGED
@@ -20,6 +20,12 @@ function mapNativeBLEStateToBLEState(nativeState) {
20
20
  };
21
21
  return map[nativeState];
22
22
  }
23
+ function arrayBufferToByteArray(buffer) {
24
+ return Array.from(new Uint8Array(buffer));
25
+ }
26
+ function byteArrayToArrayBuffer(data) {
27
+ return new Uint8Array(data).buffer;
28
+ }
23
29
  let _instance;
24
30
  export class BleNitro {
25
31
  constructor() {
@@ -59,78 +65,70 @@ export class BleNitro {
59
65
  * @returns Promise resolving to success state
60
66
  */
61
67
  startScan(filter = {}, callback) {
62
- return new Promise((resolve) => {
63
- // Don't start scanning if already scanning
64
- if (this._isScanning) {
65
- resolve(true);
66
- return;
67
- }
68
- // Create native scan filter with defaults
69
- const nativeFilter = {
70
- serviceUUIDs: filter.serviceUUIDs || [],
71
- rssiThreshold: filter.rssiThreshold ?? -100,
72
- allowDuplicates: filter.allowDuplicates ?? false,
73
- };
74
- // Create callback wrapper
75
- const scanCallback = (device) => {
76
- device.serviceUUIDs = BleNitro.normalizeGattUUIDs(device.serviceUUIDs);
77
- callback(device);
68
+ if (this._isScanning) {
69
+ return;
70
+ }
71
+ // Create native scan filter with defaults
72
+ const nativeFilter = {
73
+ serviceUUIDs: filter.serviceUUIDs || [],
74
+ rssiThreshold: filter.rssiThreshold ?? -100,
75
+ allowDuplicates: filter.allowDuplicates ?? false,
76
+ };
77
+ // Create callback wrapper
78
+ const scanCallback = (device) => {
79
+ // Convert manufacturer data to Uint8Arrays
80
+ const convertedDevice = {
81
+ ...device,
82
+ serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
83
+ manufacturerData: {
84
+ companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
85
+ id: entry.id,
86
+ data: arrayBufferToByteArray(entry.data)
87
+ }))
88
+ }
78
89
  };
79
- // Start scan
80
- BleNitroNative.startScan(nativeFilter, scanCallback);
81
- this._isScanning = true;
82
- resolve(true);
83
- });
90
+ callback(convertedDevice);
91
+ };
92
+ // Start scan
93
+ BleNitroNative.startScan(nativeFilter, scanCallback);
94
+ this._isScanning = true;
84
95
  }
85
96
  /**
86
97
  * Stop scanning for Bluetooth devices
87
98
  * @returns Promise resolving to success state
88
99
  */
89
100
  stopScan() {
90
- return new Promise((resolve, reject) => {
91
- // Don't stop scanning if not scanning
92
- if (!this._isScanning) {
93
- resolve(true);
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
- });
101
+ if (!this._isScanning) {
102
+ return;
103
+ }
104
+ BleNitroNative.stopScan();
106
105
  }
107
106
  /**
108
107
  * Check if currently scanning for devices
109
108
  * @returns Promise resolving to scanning state
110
109
  */
111
110
  isScanning() {
112
- return new Promise((resolve) => {
113
- BleNitroNative.isScanning((scanning) => {
114
- this._isScanning = scanning;
115
- resolve(scanning);
116
- });
117
- });
111
+ this._isScanning = BleNitroNative.isScanning();
112
+ return this._isScanning;
118
113
  }
119
114
  /**
120
115
  * Get all currently connected devices
121
- * @returns Promise resolving to array of connected devices
116
+ * @param services Optional list of service UUIDs to filter by
117
+ * @returns Array of connected devices
122
118
  */
123
- getConnectedDevices() {
124
- return new Promise((resolve) => {
125
- BleNitroNative.getConnectedDevices((devices) => {
126
- // Normalize service UUIDs for connected devices
127
- const normalizedDevices = devices.map(device => ({
128
- ...device,
129
- serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs)
130
- }));
131
- resolve(normalizedDevices);
132
- });
133
- });
119
+ getConnectedDevices(services) {
120
+ const devices = BleNitroNative.getConnectedDevices(services || []);
121
+ // Normalize service UUIDs - manufacturer data already comes as ArrayBuffers
122
+ return devices.map(device => ({
123
+ ...device,
124
+ serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
125
+ manufacturerData: {
126
+ companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
127
+ id: entry.id,
128
+ data: arrayBufferToByteArray(entry.data)
129
+ }))
130
+ }
131
+ }));
134
132
  }
135
133
  /**
136
134
  * Connect to a Bluetooth device
@@ -169,13 +167,13 @@ export class BleNitro {
169
167
  return new Promise((resolve, reject) => {
170
168
  // Check if already disconnected
171
169
  if (!this._connectedDevices[deviceId]) {
172
- resolve(true);
170
+ resolve();
173
171
  return;
174
172
  }
175
173
  BleNitroNative.disconnect(deviceId, (success, error) => {
176
174
  if (success) {
177
175
  delete this._connectedDevices[deviceId];
178
- resolve(true);
176
+ resolve();
179
177
  }
180
178
  else {
181
179
  reject(new Error(error));
@@ -189,12 +187,18 @@ export class BleNitro {
189
187
  * @returns Promise resolving to connection state
190
188
  */
191
189
  isConnected(deviceId) {
192
- return new Promise((resolve) => {
193
- BleNitroNative.isConnected(deviceId, (connected) => {
194
- this._connectedDevices[deviceId] = connected;
195
- resolve(connected);
196
- });
197
- });
190
+ return BleNitroNative.isConnected(deviceId);
191
+ }
192
+ /**
193
+ * Request a new MTU size
194
+ * @param deviceId ID of the device
195
+ * @param mtu New MTU size, min is 23, max is 517
196
+ * @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
197
+ */
198
+ requestMTU(deviceId, mtu) {
199
+ mtu = parseInt(mtu.toString(), 10);
200
+ const deviceMtu = BleNitroNative.requestMTU(deviceId, mtu);
201
+ return deviceMtu;
198
202
  }
199
203
  /**
200
204
  * Discover services for a connected device
@@ -235,9 +239,8 @@ export class BleNitro {
235
239
  reject(new Error('Failed to discover services'));
236
240
  return;
237
241
  }
238
- BleNitroNative.getServices(deviceId, (services) => {
239
- resolve(BleNitro.normalizeGattUUIDs(services));
240
- });
242
+ const services = BleNitroNative.getServices(deviceId);
243
+ resolve(BleNitro.normalizeGattUUIDs(services));
241
244
  });
242
245
  }
243
246
  /**
@@ -247,23 +250,18 @@ export class BleNitro {
247
250
  * @returns Promise resolving to array of characteristic UUIDs
248
251
  */
249
252
  getCharacteristics(deviceId, serviceId) {
250
- return new Promise((resolve, reject) => {
251
- // Check if connected first
252
- if (!this._connectedDevices[deviceId]) {
253
- reject(new Error('Device not connected'));
254
- return;
255
- }
256
- BleNitroNative.getCharacteristics(deviceId, BleNitro.normalizeGattUUID(serviceId), (characteristics) => {
257
- resolve(BleNitro.normalizeGattUUIDs(characteristics));
258
- });
259
- });
253
+ if (!this._connectedDevices[deviceId]) {
254
+ throw new Error('Device not connected');
255
+ }
256
+ const characteristics = BleNitroNative.getCharacteristics(deviceId, BleNitro.normalizeGattUUID(serviceId));
257
+ return BleNitro.normalizeGattUUIDs(characteristics);
260
258
  }
261
259
  /**
262
260
  * Read a characteristic value
263
261
  * @param deviceId ID of the device
264
262
  * @param serviceId ID of the service
265
263
  * @param characteristicId ID of the characteristic
266
- * @returns Promise resolving to the characteristic data as byte array
264
+ * @returns Promise resolving to the characteristic data as ByteArray
267
265
  */
268
266
  readCharacteristic(deviceId, serviceId, characteristicId) {
269
267
  return new Promise((resolve, reject) => {
@@ -274,7 +272,7 @@ export class BleNitro {
274
272
  }
275
273
  BleNitroNative.readCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (success, data, error) => {
276
274
  if (success) {
277
- resolve(data);
275
+ resolve(arrayBufferToByteArray(data));
278
276
  }
279
277
  else {
280
278
  reject(new Error(error));
@@ -287,7 +285,7 @@ export class BleNitro {
287
285
  * @param deviceId ID of the device
288
286
  * @param serviceId ID of the service
289
287
  * @param characteristicId ID of the characteristic
290
- * @param data Data to write as an array of bytes
288
+ * @param data Data to write as ByteArray(number[])
291
289
  * @param withResponse Whether to wait for response
292
290
  * @returns Promise resolving when write is complete
293
291
  */
@@ -298,7 +296,8 @@ export class BleNitro {
298
296
  reject(new Error('Device not connected'));
299
297
  return;
300
298
  }
301
- BleNitroNative.writeCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), data, withResponse, (success, error) => {
299
+ const dataAsArrayBuffer = byteArrayToArrayBuffer(data);
300
+ BleNitroNative.writeCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), dataAsArrayBuffer, withResponse, (success, error) => {
302
301
  if (success) {
303
302
  resolve(true);
304
303
  }
@@ -317,29 +316,27 @@ export class BleNitro {
317
316
  * @returns Promise resolving when subscription is complete
318
317
  */
319
318
  subscribeToCharacteristic(deviceId, serviceId, characteristicId, callback) {
320
- return new Promise((resolve, reject) => {
321
- // Check if connected first
322
- if (!this._connectedDevices[deviceId]) {
323
- reject(new Error('Device not connected'));
324
- return;
319
+ // Check if connected first
320
+ if (!this._connectedDevices[deviceId]) {
321
+ throw new Error('Device not connected');
322
+ }
323
+ let _success = false;
324
+ BleNitroNative.subscribeToCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (charId, data) => {
325
+ callback(charId, arrayBufferToByteArray(data));
326
+ }, (success, error) => {
327
+ _success = success;
328
+ if (!success) {
329
+ throw new Error(error);
325
330
  }
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
331
  });
332
+ return {
333
+ remove: () => {
334
+ if (!_success) {
335
+ return;
336
+ }
337
+ this.unsubscribeFromCharacteristic(deviceId, serviceId, characteristicId).catch(() => { });
338
+ }
339
+ };
343
340
  }
344
341
  /**
345
342
  * Unsubscribe from characteristic notifications
@@ -370,14 +367,10 @@ export class BleNitro {
370
367
  * @returns Promise resolving to Bluetooth state
371
368
  */
372
369
  isBluetoothEnabled() {
373
- return new Promise((resolve) => {
374
- BleNitroNative.isBluetoothEnabled((enabled) => {
375
- resolve(enabled);
376
- });
377
- });
370
+ return this.state() === BLEState.PoweredOn;
378
371
  }
379
372
  /**
380
- * Request to enable Bluetooth
373
+ * Request to enable Bluetooth (Android only)
381
374
  * @returns Promise resolving when Bluetooth is enabled
382
375
  */
383
376
  requestBluetoothEnable() {
@@ -398,11 +391,7 @@ export class BleNitro {
398
391
  * @see BLEState
399
392
  */
400
393
  state() {
401
- return new Promise((resolve) => {
402
- BleNitroNative.state((state) => {
403
- resolve(mapNativeBLEStateToBLEState(state));
404
- });
405
- });
394
+ return mapNativeBLEStateToBLEState(BleNitroNative.state());
406
395
  }
407
396
  /**
408
397
  * Subscribe to Bluetooth state changes
@@ -412,37 +401,18 @@ export class BleNitro {
412
401
  * @see BLEState
413
402
  */
414
403
  subscribeToStateChange(callback, emitInitial = false) {
415
- return new Promise(async (resolve, reject) => {
416
- if (emitInitial) {
417
- const state = await this.state().catch(() => {
418
- return BLEState.Unknown;
419
- });
420
- callback(state);
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
- });
404
+ if (emitInitial) {
405
+ const state = this.state();
406
+ callback(state);
407
+ }
408
+ BleNitroNative.subscribeToStateChange((nativeState) => {
409
+ callback(mapNativeBLEStateToBLEState(nativeState));
445
410
  });
411
+ return {
412
+ remove: () => {
413
+ BleNitroNative.unsubscribeFromStateChange();
414
+ },
415
+ };
446
416
  }
447
417
  /**
448
418
  * Open Bluetooth settings
@@ -1,4 +1,5 @@
1
1
  import { HybridObject } from 'react-native-nitro-modules';
2
+ export type BLEValue = ArrayBuffer;
2
3
  export declare enum BLEState {
3
4
  Unknown = 0,
4
5
  Resetting = 1,
@@ -9,7 +10,7 @@ export declare enum BLEState {
9
10
  }
10
11
  export interface ManufacturerDataEntry {
11
12
  id: string;
12
- data: number[];
13
+ data: BLEValue;
13
14
  }
14
15
  export interface ManufacturerData {
15
16
  companyIdentifiers: ManufacturerDataEntry[];
@@ -32,11 +33,15 @@ export type DevicesCallback = (devices: BLEDevice[]) => void;
32
33
  export type ConnectionCallback = (success: boolean, deviceId: string, error: string) => void;
33
34
  export type DisconnectionEventCallback = (deviceId: string, interrupted: boolean, error: string) => void;
34
35
  export type OperationCallback = (success: boolean, error: string) => void;
35
- export type CharacteristicCallback = (characteristicId: string, data: number[]) => void;
36
+ export type CharacteristicCallback = (characteristicId: string, data: BLEValue) => void;
36
37
  export type StateCallback = (state: BLEState) => void;
37
38
  export type BooleanCallback = (result: boolean) => void;
38
39
  export type StringArrayCallback = (result: string[]) => void;
39
- export type ReadCharacteristicCallback = (success: boolean, data: number[], error: string) => void;
40
+ export type ReadCharacteristicCallback = (success: boolean, data: BLEValue, error: string) => void;
41
+ export type OperationResult = {
42
+ success: boolean;
43
+ error?: string;
44
+ };
40
45
  /**
41
46
  * Native BLE Nitro Module Specification
42
47
  * Defines the interface between TypeScript and native implementations
@@ -46,23 +51,23 @@ export interface NativeBleNitro extends HybridObject<{
46
51
  android: 'kotlin';
47
52
  }> {
48
53
  startScan(filter: ScanFilter, callback: ScanCallback): void;
49
- stopScan(callback: OperationCallback): void;
50
- isScanning(callback: BooleanCallback): void;
51
- getConnectedDevices(callback: DevicesCallback): void;
54
+ stopScan(): boolean;
55
+ isScanning(): boolean;
56
+ getConnectedDevices(services: string[]): BLEDevice[];
52
57
  connect(deviceId: string, callback: ConnectionCallback, disconnectCallback?: DisconnectionEventCallback): void;
53
58
  disconnect(deviceId: string, callback: OperationCallback): void;
54
- isConnected(deviceId: string, callback: BooleanCallback): void;
59
+ isConnected(deviceId: string): boolean;
60
+ requestMTU(deviceId: string, mtu: number): number;
55
61
  discoverServices(deviceId: string, callback: OperationCallback): void;
56
- getServices(deviceId: string, callback: StringArrayCallback): void;
57
- getCharacteristics(deviceId: string, serviceId: string, callback: StringArrayCallback): void;
62
+ getServices(deviceId: string): string[];
63
+ getCharacteristics(deviceId: string, serviceId: string): string[];
58
64
  readCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: ReadCharacteristicCallback): void;
59
- writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: number[], withResponse: boolean, callback: OperationCallback): void;
65
+ writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: BLEValue, withResponse: boolean, callback: OperationCallback): void;
60
66
  subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, updateCallback: CharacteristicCallback, resultCallback: OperationCallback): void;
61
67
  unsubscribeFromCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: OperationCallback): void;
62
- isBluetoothEnabled(callback: BooleanCallback): void;
63
68
  requestBluetoothEnable(callback: OperationCallback): void;
64
- state(callback: StateCallback): void;
65
- subscribeToStateChange(stateCallback: StateCallback, resultCallback: OperationCallback): void;
66
- unsubscribeFromStateChange(resultCallback: OperationCallback): void;
69
+ state(): BLEState;
70
+ subscribeToStateChange(stateCallback: StateCallback): OperationResult;
71
+ unsubscribeFromStateChange(): OperationResult;
67
72
  openSettings(): Promise<void>;
68
73
  }
@@ -17,14 +17,11 @@
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 "JFunc_void_std__vector_std__string_.hpp"
26
- #include "JFunc_void_bool_std__vector_double__std__string.hpp"
27
- #include "JFunc_void_std__string_std__vector_double_.hpp"
22
+ #include "JFunc_void_bool_std__string.hpp"
23
+ #include "JFunc_void_bool_std__shared_ptr_ArrayBuffer__std__string.hpp"
24
+ #include "JFunc_void_std__string_std__shared_ptr_ArrayBuffer_.hpp"
28
25
  #include "JFunc_void_BLEState.hpp"
29
26
  #include <NitroModules/DefaultConstructableObject.hpp>
30
27
 
@@ -39,14 +36,11 @@ 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::JFunc_void_std__vector_std__string__cxx::registerNatives();
48
- margelo::nitro::co::zyke::ble::JFunc_void_bool_std__vector_double__std__string_cxx::registerNatives();
49
- margelo::nitro::co::zyke::ble::JFunc_void_std__string_std__vector_double__cxx::registerNatives();
41
+ margelo::nitro::co::zyke::ble::JFunc_void_bool_std__string_cxx::registerNatives();
42
+ margelo::nitro::co::zyke::ble::JFunc_void_bool_std__shared_ptr_ArrayBuffer__std__string_cxx::registerNatives();
43
+ margelo::nitro::co::zyke::ble::JFunc_void_std__string_std__shared_ptr_ArrayBuffer__cxx::registerNatives();
50
44
  margelo::nitro::co::zyke::ble::JFunc_void_BLEState_cxx::registerNatives();
51
45
 
52
46
  // Register Nitro Hybrid Objects
@@ -14,6 +14,9 @@
14
14
  #include "JManufacturerDataEntry.hpp"
15
15
  #include "ManufacturerData.hpp"
16
16
  #include "ManufacturerDataEntry.hpp"
17
+ #include <NitroModules/ArrayBuffer.hpp>
18
+ #include <NitroModules/JArrayBuffer.hpp>
19
+ #include <NitroModules/JUnit.hpp>
17
20
  #include <string>
18
21
  #include <vector>
19
22
 
@@ -19,6 +19,9 @@
19
19
  #include "ManufacturerDataEntry.hpp"
20
20
  #include <vector>
21
21
  #include "JManufacturerDataEntry.hpp"
22
+ #include <NitroModules/ArrayBuffer.hpp>
23
+ #include <NitroModules/JArrayBuffer.hpp>
24
+ #include <NitroModules/JUnit.hpp>
22
25
 
23
26
  namespace margelo::nitro::co::zyke::ble {
24
27