react-native-ble-nitro 1.3.1 → 1.4.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 (37) hide show
  1. package/README.md +24 -0
  2. package/android/src/main/java/com/margelo/nitro/co/zyke/ble/BleNitroBleManager.kt +6 -0
  3. package/ios/BleNitroBleManager.swift +84 -2
  4. package/lib/commonjs/index.d.ts +4 -194
  5. package/lib/commonjs/index.d.ts.map +1 -1
  6. package/lib/commonjs/index.js +6 -441
  7. package/lib/commonjs/index.js.map +1 -1
  8. package/lib/commonjs/manager.d.ts +204 -0
  9. package/lib/commonjs/manager.d.ts.map +1 -0
  10. package/lib/commonjs/manager.js +469 -0
  11. package/lib/commonjs/manager.js.map +1 -0
  12. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts +2 -0
  13. package/lib/commonjs/specs/NativeBleNitro.nitro.d.ts.map +1 -1
  14. package/lib/index.d.ts +4 -194
  15. package/lib/index.js +3 -436
  16. package/lib/manager.d.ts +203 -0
  17. package/lib/manager.js +455 -0
  18. package/lib/specs/NativeBleNitro.nitro.d.ts +2 -0
  19. package/nitrogen/generated/android/BleNitroOnLoad.cpp +2 -0
  20. package/nitrogen/generated/android/c++/JFunc_void_std__vector_BLEDevice_.hpp +102 -0
  21. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.cpp +6 -1
  22. package/nitrogen/generated/android/c++/JHybridNativeBleNitroSpec.hpp +1 -0
  23. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/Func_void_std__vector_BLEDevice_.kt +81 -0
  24. package/nitrogen/generated/android/kotlin/com/margelo/nitro/co/zyke/ble/HybridNativeBleNitroSpec.kt +9 -0
  25. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.cpp +8 -0
  26. package/nitrogen/generated/ios/BleNitro-Swift-Cxx-Bridge.hpp +38 -16
  27. package/nitrogen/generated/ios/c++/HybridNativeBleNitroSpecSwift.hpp +15 -9
  28. package/nitrogen/generated/ios/swift/Func_void_std__vector_BLEDevice_.swift +47 -0
  29. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec.swift +1 -0
  30. package/nitrogen/generated/ios/swift/HybridNativeBleNitroSpec_cxx.swift +22 -0
  31. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.cpp +1 -0
  32. package/nitrogen/generated/shared/c++/HybridNativeBleNitroSpec.hpp +6 -5
  33. package/package.json +9 -1
  34. package/src/__tests__/index.test.ts +19 -15
  35. package/src/index.ts +24 -609
  36. package/src/manager.ts +627 -0
  37. package/src/specs/NativeBleNitro.nitro.ts +4 -0
package/lib/index.d.ts CHANGED
@@ -1,195 +1,5 @@
1
- export type ByteArray = number[];
2
- export interface ScanFilter {
3
- serviceUUIDs?: string[];
4
- rssiThreshold?: number;
5
- allowDuplicates?: boolean;
6
- androidScanMode?: AndroidScanMode;
1
+ import { BleNitroManager } from "./manager";
2
+ export { type ByteArray, type ScanFilter, type BLEDevice, type ScanCallback, type ManufacturerDataEntry, type ManufacturerData, type ConnectionCallback, type DisconnectEventCallback, type OperationCallback, type CharacteristicUpdateCallback, type Subscription, type BleNitroManager, type BleNitroManagerOptions, BLEState, AndroidScanMode, } from "./manager";
3
+ export declare class BleNitro extends BleNitroManager {
4
+ static instance(): BleNitroManager;
7
5
  }
8
- export interface ManufacturerDataEntry {
9
- id: string;
10
- data: ByteArray;
11
- }
12
- export interface ManufacturerData {
13
- companyIdentifiers: ManufacturerDataEntry[];
14
- }
15
- export interface BLEDevice {
16
- id: string;
17
- name: string;
18
- rssi: number;
19
- manufacturerData: ManufacturerData;
20
- serviceUUIDs: string[];
21
- isConnectable: boolean;
22
- }
23
- export type ScanCallback = (device: BLEDevice) => void;
24
- export type RestoreStateCallback = (connectedPeripherals: BLEDevice[]) => void;
25
- export type ConnectionCallback = (success: boolean, deviceId: string, error: string) => void;
26
- export type DisconnectEventCallback = (deviceId: string, interrupted: boolean, error: string) => void;
27
- export type OperationCallback = (success: boolean, error: string) => void;
28
- export type CharacteristicUpdateCallback = (characteristicId: string, data: ByteArray) => void;
29
- export type Subscription = {
30
- remove: () => void;
31
- };
32
- export declare enum BLEState {
33
- Unknown = "Unknown",
34
- Resetting = "Resetting",
35
- Unsupported = "Unsupported",
36
- Unauthorized = "Unauthorized",
37
- PoweredOff = "PoweredOff",
38
- PoweredOn = "PoweredOn"
39
- }
40
- export declare enum AndroidScanMode {
41
- LowLatency = "LowLatency",
42
- Balanced = "Balanced",
43
- LowPower = "LowPower",
44
- Opportunistic = "Opportunistic"
45
- }
46
- export type BleNitroOptions = {
47
- restoreStateIdentifier?: string;
48
- onRestoreState?: RestoreStateCallback;
49
- };
50
- export declare class BleNitro {
51
- private _isScanning;
52
- private _connectedDevices;
53
- static instance(): BleNitro;
54
- /**
55
- * Converts a 16- oder 32-Bit UUID to a 128-Bit UUID
56
- *
57
- * @param uuid 16-, 32- or 128-Bit UUID as string
58
- * @returns Full 128-Bit UUID
59
- */
60
- static normalizeGattUUID(uuid: string): string;
61
- static normalizeGattUUIDs(uuids: string[]): string[];
62
- /**
63
- * Start scanning for Bluetooth devices
64
- * @param filter Optional scan filter
65
- * @param callback Callback function called when a device is found
66
- * @returns Promise resolving to success state
67
- */
68
- startScan(filter: ScanFilter | undefined, callback: ScanCallback, onError?: (error: string) => void): void;
69
- /**
70
- * Stop scanning for Bluetooth devices
71
- * @returns Promise resolving to success state
72
- */
73
- stopScan(): void;
74
- /**
75
- * Check if currently scanning for devices
76
- * @returns Promise resolving to scanning state
77
- */
78
- isScanning(): boolean;
79
- /**
80
- * Get all currently connected devices
81
- * @param services Optional list of service UUIDs to filter by
82
- * @returns Array of connected devices
83
- */
84
- getConnectedDevices(services?: string[]): BLEDevice[];
85
- /**
86
- * Connect to a Bluetooth device
87
- * @param deviceId ID of the device to connect to
88
- * @param onDisconnect Optional callback for disconnect events
89
- * @returns Promise resolving when connected
90
- */
91
- connect(deviceId: string, onDisconnect?: DisconnectEventCallback): Promise<string>;
92
- /**
93
- * Disconnect from a Bluetooth device
94
- * @param deviceId ID of the device to disconnect from
95
- * @returns Promise resolving when disconnected
96
- */
97
- disconnect(deviceId: string): Promise<void>;
98
- /**
99
- * Check if connected to a device
100
- * @param deviceId ID of the device to check
101
- * @returns Promise resolving to connection state
102
- */
103
- isConnected(deviceId: string): boolean;
104
- /**
105
- * Request a new MTU size
106
- * @param deviceId ID of the device
107
- * @param mtu New MTU size, min is 23, max is 517
108
- * @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
109
- */
110
- requestMTU(deviceId: string, mtu: number): number;
111
- /**
112
- * Discover services for a connected device
113
- * @param deviceId ID of the device
114
- * @returns Promise resolving when services are discovered
115
- */
116
- discoverServices(deviceId: string): Promise<boolean>;
117
- /**
118
- * Get services for a connected device
119
- * @param deviceId ID of the device
120
- * @returns Promise resolving to array of service UUIDs
121
- */
122
- getServices(deviceId: string): Promise<string[]>;
123
- /**
124
- * Get characteristics for a service
125
- * @param deviceId ID of the device
126
- * @param serviceId ID of the service
127
- * @returns Promise resolving to array of characteristic UUIDs
128
- */
129
- getCharacteristics(deviceId: string, serviceId: string): string[];
130
- /**
131
- * Read a characteristic value
132
- * @param deviceId ID of the device
133
- * @param serviceId ID of the service
134
- * @param characteristicId ID of the characteristic
135
- * @returns Promise resolving to the characteristic data as ArrayBuffer
136
- */
137
- readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ByteArray>;
138
- /**
139
- * Write a value to a characteristic
140
- * @param deviceId ID of the device
141
- * @param serviceId ID of the service
142
- * @param characteristicId ID of the characteristic
143
- * @param data Data to write as ByteArray (number[])
144
- * @param withResponse Whether to wait for response
145
- * @returns Promise resolving when write is complete
146
- */
147
- writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: ByteArray, withResponse?: boolean): Promise<boolean>;
148
- /**
149
- * Subscribe to characteristic notifications
150
- * @param deviceId ID of the device
151
- * @param serviceId ID of the service
152
- * @param characteristicId ID of the characteristic
153
- * @param callback Callback function called when notification is received
154
- * @returns Promise resolving when subscription is complete
155
- */
156
- subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: CharacteristicUpdateCallback): Subscription;
157
- /**
158
- * Unsubscribe from characteristic notifications
159
- * @param deviceId ID of the device
160
- * @param serviceId ID of the service
161
- * @param characteristicId ID of the characteristic
162
- * @returns Promise resolving when unsubscription is complete
163
- */
164
- unsubscribeFromCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void>;
165
- /**
166
- * Check if Bluetooth is enabled
167
- * @returns Promise resolving to Bluetooth state
168
- */
169
- isBluetoothEnabled(): boolean;
170
- /**
171
- * Request to enable Bluetooth (Android only)
172
- * @returns Promise resolving when Bluetooth is enabled
173
- */
174
- requestBluetoothEnable(): Promise<boolean>;
175
- /**
176
- * Get the current Bluetooth state
177
- * @returns Promise resolving to Bluetooth state
178
- * @see BLEState
179
- */
180
- state(): BLEState;
181
- /**
182
- * Subscribe to Bluetooth state changes
183
- * @param callback Callback function called when state changes
184
- * @param emitInitial Whether to emit initial state callback
185
- * @returns Promise resolving when subscription is complete
186
- * @see BLEState
187
- */
188
- subscribeToStateChange(callback: (state: BLEState) => void, emitInitial?: boolean): Subscription;
189
- /**
190
- * Open Bluetooth settings
191
- * @returns Promise resolving when settings are opened
192
- */
193
- openSettings(): Promise<void>;
194
- }
195
- export declare const ble: BleNitro;
package/lib/index.js CHANGED
@@ -1,444 +1,11 @@
1
- import BleNitroNative from './specs/NativeBleNitro';
2
- import { AndroidScanMode as NativeAndroidScanMode, } from './specs/NativeBleNitro';
3
- export var BLEState;
4
- (function (BLEState) {
5
- BLEState["Unknown"] = "Unknown";
6
- BLEState["Resetting"] = "Resetting";
7
- BLEState["Unsupported"] = "Unsupported";
8
- BLEState["Unauthorized"] = "Unauthorized";
9
- BLEState["PoweredOff"] = "PoweredOff";
10
- BLEState["PoweredOn"] = "PoweredOn";
11
- })(BLEState || (BLEState = {}));
12
- ;
13
- export var AndroidScanMode;
14
- (function (AndroidScanMode) {
15
- AndroidScanMode["LowLatency"] = "LowLatency";
16
- AndroidScanMode["Balanced"] = "Balanced";
17
- AndroidScanMode["LowPower"] = "LowPower";
18
- AndroidScanMode["Opportunistic"] = "Opportunistic";
19
- })(AndroidScanMode || (AndroidScanMode = {}));
20
- function mapNativeBLEStateToBLEState(nativeState) {
21
- const map = {
22
- 0: BLEState.Unknown,
23
- 1: BLEState.Resetting,
24
- 2: BLEState.Unsupported,
25
- 3: BLEState.Unauthorized,
26
- 4: BLEState.PoweredOff,
27
- 5: BLEState.PoweredOn,
28
- };
29
- return map[nativeState];
30
- }
31
- function mapAndroidScanModeToNativeAndroidScanMode(scanMode) {
32
- const map = {
33
- LowLatency: NativeAndroidScanMode.LowLatency,
34
- Balanced: NativeAndroidScanMode.Balanced,
35
- LowPower: NativeAndroidScanMode.LowPower,
36
- Opportunistic: NativeAndroidScanMode.Opportunistic,
37
- };
38
- return map[scanMode];
39
- }
40
- function convertNativeBleDeviceToBleDevice(nativeBleDevice) {
41
- return {
42
- ...nativeBleDevice,
43
- serviceUUIDs: BleNitro.normalizeGattUUIDs(nativeBleDevice.serviceUUIDs),
44
- manufacturerData: {
45
- companyIdentifiers: nativeBleDevice.manufacturerData.companyIdentifiers.map(entry => ({
46
- id: entry.id,
47
- data: arrayBufferToByteArray(entry.data)
48
- }))
49
- }
50
- };
51
- }
52
- function arrayBufferToByteArray(buffer) {
53
- return Array.from(new Uint8Array(buffer));
54
- }
55
- function byteArrayToArrayBuffer(data) {
56
- return new Uint8Array(data).buffer;
57
- }
1
+ import { BleNitroManager } from "./manager";
2
+ export { BLEState, AndroidScanMode, } from "./manager";
58
3
  let _instance;
59
- export class BleNitro {
60
- constructor() {
61
- this._isScanning = false;
62
- this._connectedDevices = {};
63
- }
4
+ export class BleNitro extends BleNitroManager {
64
5
  static instance() {
65
6
  if (!_instance) {
66
7
  _instance = new BleNitro();
67
8
  }
68
9
  return _instance;
69
10
  }
70
- /**
71
- * Converts a 16- oder 32-Bit UUID to a 128-Bit UUID
72
- *
73
- * @param uuid 16-, 32- or 128-Bit UUID as string
74
- * @returns Full 128-Bit UUID
75
- */
76
- static normalizeGattUUID(uuid) {
77
- const cleanUuid = uuid.toLowerCase();
78
- // 128-Bit UUID → normalisieren
79
- if (cleanUuid.length === 36 && cleanUuid.includes("-")) {
80
- return cleanUuid;
81
- }
82
- // GATT-Service UUIDs
83
- // 16- oder 32-Bit UUID → 128-Bit UUID
84
- const padded = cleanUuid.padStart(8, "0");
85
- return `${padded}-0000-1000-8000-00805f9b34fb`;
86
- }
87
- static normalizeGattUUIDs(uuids) {
88
- return uuids.map((uuid) => BleNitro.normalizeGattUUID(uuid));
89
- }
90
- /**
91
- * Start scanning for Bluetooth devices
92
- * @param filter Optional scan filter
93
- * @param callback Callback function called when a device is found
94
- * @returns Promise resolving to success state
95
- */
96
- startScan(filter = {}, callback, onError) {
97
- if (this._isScanning) {
98
- return;
99
- }
100
- // Create native scan filter with defaults
101
- const nativeFilter = {
102
- serviceUUIDs: filter.serviceUUIDs || [],
103
- rssiThreshold: filter.rssiThreshold ?? -100,
104
- allowDuplicates: filter.allowDuplicates ?? false,
105
- androidScanMode: mapAndroidScanModeToNativeAndroidScanMode(filter.androidScanMode ?? AndroidScanMode.Balanced),
106
- };
107
- // Create callback wrapper
108
- const scanCallback = (device, error) => {
109
- if (error && !device) {
110
- this._isScanning = false;
111
- onError?.(error);
112
- return;
113
- }
114
- device = device; // eslint-disable-line @typescript-eslint/no-non-null-assertion
115
- // Convert manufacturer data to Uint8Arrays
116
- const convertedDevice = convertNativeBleDeviceToBleDevice(device);
117
- callback(convertedDevice);
118
- };
119
- // Start scan
120
- BleNitroNative.startScan(nativeFilter, scanCallback);
121
- this._isScanning = true;
122
- }
123
- /**
124
- * Stop scanning for Bluetooth devices
125
- * @returns Promise resolving to success state
126
- */
127
- stopScan() {
128
- if (!this._isScanning) {
129
- return;
130
- }
131
- BleNitroNative.stopScan();
132
- this._isScanning = false;
133
- }
134
- /**
135
- * Check if currently scanning for devices
136
- * @returns Promise resolving to scanning state
137
- */
138
- isScanning() {
139
- this._isScanning = BleNitroNative.isScanning();
140
- return this._isScanning;
141
- }
142
- /**
143
- * Get all currently connected devices
144
- * @param services Optional list of service UUIDs to filter by
145
- * @returns Array of connected devices
146
- */
147
- getConnectedDevices(services) {
148
- const devices = BleNitroNative.getConnectedDevices(services || []);
149
- // Normalize service UUIDs - manufacturer data already comes as ArrayBuffers
150
- return devices.map(device => convertNativeBleDeviceToBleDevice(device));
151
- }
152
- /**
153
- * Connect to a Bluetooth device
154
- * @param deviceId ID of the device to connect to
155
- * @param onDisconnect Optional callback for disconnect events
156
- * @returns Promise resolving when connected
157
- */
158
- connect(deviceId, onDisconnect) {
159
- return new Promise((resolve, reject) => {
160
- // Check if already connected
161
- if (this._connectedDevices[deviceId]) {
162
- resolve(deviceId);
163
- return;
164
- }
165
- BleNitroNative.connect(deviceId, (success, connectedDeviceId, error) => {
166
- if (success) {
167
- this._connectedDevices[deviceId] = true;
168
- resolve(connectedDeviceId);
169
- }
170
- else {
171
- reject(new Error(error));
172
- }
173
- }, onDisconnect ? (deviceId, interrupted, error) => {
174
- // Remove from connected devices when disconnected
175
- delete this._connectedDevices[deviceId];
176
- onDisconnect(deviceId, interrupted, error);
177
- } : undefined);
178
- });
179
- }
180
- /**
181
- * Disconnect from a Bluetooth device
182
- * @param deviceId ID of the device to disconnect from
183
- * @returns Promise resolving when disconnected
184
- */
185
- disconnect(deviceId) {
186
- return new Promise((resolve, reject) => {
187
- // Check if already disconnected
188
- if (!this._connectedDevices[deviceId]) {
189
- resolve();
190
- return;
191
- }
192
- BleNitroNative.disconnect(deviceId, (success, error) => {
193
- if (success) {
194
- delete this._connectedDevices[deviceId];
195
- resolve();
196
- }
197
- else {
198
- reject(new Error(error));
199
- }
200
- });
201
- });
202
- }
203
- /**
204
- * Check if connected to a device
205
- * @param deviceId ID of the device to check
206
- * @returns Promise resolving to connection state
207
- */
208
- isConnected(deviceId) {
209
- return BleNitroNative.isConnected(deviceId);
210
- }
211
- /**
212
- * Request a new MTU size
213
- * @param deviceId ID of the device
214
- * @param mtu New MTU size, min is 23, max is 517
215
- * @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
216
- */
217
- requestMTU(deviceId, mtu) {
218
- mtu = parseInt(mtu.toString(), 10);
219
- const deviceMtu = BleNitroNative.requestMTU(deviceId, mtu);
220
- return deviceMtu;
221
- }
222
- /**
223
- * Discover services for a connected device
224
- * @param deviceId ID of the device
225
- * @returns Promise resolving when services are discovered
226
- */
227
- discoverServices(deviceId) {
228
- return new Promise((resolve, reject) => {
229
- // Check if connected first
230
- if (!this._connectedDevices[deviceId]) {
231
- reject(new Error('Device not connected'));
232
- return;
233
- }
234
- BleNitroNative.discoverServices(deviceId, (success, error) => {
235
- if (success) {
236
- resolve(true);
237
- }
238
- else {
239
- reject(new Error(error));
240
- }
241
- });
242
- });
243
- }
244
- /**
245
- * Get services for a connected device
246
- * @param deviceId ID of the device
247
- * @returns Promise resolving to array of service UUIDs
248
- */
249
- getServices(deviceId) {
250
- return new Promise(async (resolve, reject) => {
251
- // Check if connected first
252
- if (!this._connectedDevices[deviceId]) {
253
- reject(new Error('Device not connected'));
254
- return;
255
- }
256
- const success = await this.discoverServices(deviceId);
257
- if (!success) {
258
- reject(new Error('Failed to discover services'));
259
- return;
260
- }
261
- const services = BleNitroNative.getServices(deviceId);
262
- resolve(BleNitro.normalizeGattUUIDs(services));
263
- });
264
- }
265
- /**
266
- * Get characteristics for a service
267
- * @param deviceId ID of the device
268
- * @param serviceId ID of the service
269
- * @returns Promise resolving to array of characteristic UUIDs
270
- */
271
- getCharacteristics(deviceId, serviceId) {
272
- if (!this._connectedDevices[deviceId]) {
273
- throw new Error('Device not connected');
274
- }
275
- const characteristics = BleNitroNative.getCharacteristics(deviceId, BleNitro.normalizeGattUUID(serviceId));
276
- return BleNitro.normalizeGattUUIDs(characteristics);
277
- }
278
- /**
279
- * Read a characteristic value
280
- * @param deviceId ID of the device
281
- * @param serviceId ID of the service
282
- * @param characteristicId ID of the characteristic
283
- * @returns Promise resolving to the characteristic data as ArrayBuffer
284
- */
285
- readCharacteristic(deviceId, serviceId, characteristicId) {
286
- return new Promise((resolve, reject) => {
287
- // Check if connected first
288
- if (!this._connectedDevices[deviceId]) {
289
- reject(new Error('Device not connected'));
290
- return;
291
- }
292
- BleNitroNative.readCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (success, data, error) => {
293
- if (success) {
294
- resolve(arrayBufferToByteArray(data));
295
- }
296
- else {
297
- reject(new Error(error));
298
- }
299
- });
300
- });
301
- }
302
- /**
303
- * Write a value to a characteristic
304
- * @param deviceId ID of the device
305
- * @param serviceId ID of the service
306
- * @param characteristicId ID of the characteristic
307
- * @param data Data to write as ByteArray (number[])
308
- * @param withResponse Whether to wait for response
309
- * @returns Promise resolving when write is complete
310
- */
311
- writeCharacteristic(deviceId, serviceId, characteristicId, data, withResponse = true) {
312
- return new Promise((resolve, reject) => {
313
- // Check if connected first
314
- if (!this._connectedDevices[deviceId]) {
315
- reject(new Error('Device not connected'));
316
- return;
317
- }
318
- BleNitroNative.writeCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), byteArrayToArrayBuffer(data), withResponse, (success, error) => {
319
- if (success) {
320
- resolve(true);
321
- }
322
- else {
323
- reject(new Error(error));
324
- }
325
- });
326
- });
327
- }
328
- /**
329
- * Subscribe to characteristic notifications
330
- * @param deviceId ID of the device
331
- * @param serviceId ID of the service
332
- * @param characteristicId ID of the characteristic
333
- * @param callback Callback function called when notification is received
334
- * @returns Promise resolving when subscription is complete
335
- */
336
- subscribeToCharacteristic(deviceId, serviceId, characteristicId, callback) {
337
- // Check if connected first
338
- if (!this._connectedDevices[deviceId]) {
339
- throw new Error('Device not connected');
340
- }
341
- let _success = false;
342
- BleNitroNative.subscribeToCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (charId, data) => {
343
- callback(charId, arrayBufferToByteArray(data));
344
- }, (success, error) => {
345
- _success = success;
346
- if (!success) {
347
- throw new Error(error);
348
- }
349
- });
350
- return {
351
- remove: () => {
352
- if (!_success) {
353
- return;
354
- }
355
- this.unsubscribeFromCharacteristic(deviceId, serviceId, characteristicId).catch(() => { });
356
- }
357
- };
358
- }
359
- /**
360
- * Unsubscribe from characteristic notifications
361
- * @param deviceId ID of the device
362
- * @param serviceId ID of the service
363
- * @param characteristicId ID of the characteristic
364
- * @returns Promise resolving when unsubscription is complete
365
- */
366
- unsubscribeFromCharacteristic(deviceId, serviceId, characteristicId) {
367
- return new Promise((resolve, reject) => {
368
- // Check if connected first
369
- if (!this._connectedDevices[deviceId]) {
370
- reject(new Error('Device not connected'));
371
- return;
372
- }
373
- BleNitroNative.unsubscribeFromCharacteristic(deviceId, BleNitro.normalizeGattUUID(serviceId), BleNitro.normalizeGattUUID(characteristicId), (success, error) => {
374
- if (success) {
375
- resolve();
376
- }
377
- else {
378
- reject(new Error(error));
379
- }
380
- });
381
- });
382
- }
383
- /**
384
- * Check if Bluetooth is enabled
385
- * @returns Promise resolving to Bluetooth state
386
- */
387
- isBluetoothEnabled() {
388
- return this.state() === BLEState.PoweredOn;
389
- }
390
- /**
391
- * Request to enable Bluetooth (Android only)
392
- * @returns Promise resolving when Bluetooth is enabled
393
- */
394
- requestBluetoothEnable() {
395
- return new Promise((resolve, reject) => {
396
- BleNitroNative.requestBluetoothEnable((success, error) => {
397
- if (success) {
398
- resolve(true);
399
- }
400
- else {
401
- reject(new Error(error));
402
- }
403
- });
404
- });
405
- }
406
- /**
407
- * Get the current Bluetooth state
408
- * @returns Promise resolving to Bluetooth state
409
- * @see BLEState
410
- */
411
- state() {
412
- return mapNativeBLEStateToBLEState(BleNitroNative.state());
413
- }
414
- /**
415
- * Subscribe to Bluetooth state changes
416
- * @param callback Callback function called when state changes
417
- * @param emitInitial Whether to emit initial state callback
418
- * @returns Promise resolving when subscription is complete
419
- * @see BLEState
420
- */
421
- subscribeToStateChange(callback, emitInitial = false) {
422
- if (emitInitial) {
423
- const state = this.state();
424
- callback(state);
425
- }
426
- BleNitroNative.subscribeToStateChange((nativeState) => {
427
- callback(mapNativeBLEStateToBLEState(nativeState));
428
- });
429
- return {
430
- remove: () => {
431
- BleNitroNative.unsubscribeFromStateChange();
432
- },
433
- };
434
- }
435
- /**
436
- * Open Bluetooth settings
437
- * @returns Promise resolving when settings are opened
438
- */
439
- openSettings() {
440
- return BleNitroNative.openSettings();
441
- }
442
11
  }
443
- // Singleton instance
444
- export const ble = BleNitro.instance();