react-native-ble-nitro 1.3.0 → 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 +32 -10
  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 -188
  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 -188
  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 +24 -21
  35. package/src/index.ts +24 -600
  36. package/src/manager.ts +627 -0
  37. package/src/specs/NativeBleNitro.nitro.ts +4 -0
package/lib/index.d.ts CHANGED
@@ -1,189 +1,5 @@
1
- export interface ScanFilter {
2
- serviceUUIDs?: string[];
3
- rssiThreshold?: number;
4
- allowDuplicates?: boolean;
5
- 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;
6
5
  }
7
- export interface ManufacturerDataEntry {
8
- id: string;
9
- data: ArrayBuffer;
10
- }
11
- export interface ManufacturerData {
12
- companyIdentifiers: ManufacturerDataEntry[];
13
- }
14
- export interface BLEDevice {
15
- id: string;
16
- name: string;
17
- rssi: number;
18
- manufacturerData: ManufacturerData;
19
- serviceUUIDs: string[];
20
- isConnectable: boolean;
21
- }
22
- export type ScanCallback = (device: BLEDevice) => void;
23
- export type ConnectionCallback = (success: boolean, deviceId: string, error: string) => void;
24
- export type DisconnectEventCallback = (deviceId: string, interrupted: boolean, error: string) => void;
25
- export type OperationCallback = (success: boolean, error: string) => void;
26
- export type CharacteristicUpdateCallback = (characteristicId: string, data: ArrayBuffer) => void;
27
- export type Subscription = {
28
- remove: () => void;
29
- };
30
- export declare enum BLEState {
31
- Unknown = "Unknown",
32
- Resetting = "Resetting",
33
- Unsupported = "Unsupported",
34
- Unauthorized = "Unauthorized",
35
- PoweredOff = "PoweredOff",
36
- PoweredOn = "PoweredOn"
37
- }
38
- export declare enum AndroidScanMode {
39
- LowLatency = "LowLatency",
40
- Balanced = "Balanced",
41
- LowPower = "LowPower",
42
- Opportunistic = "Opportunistic"
43
- }
44
- export declare class BleNitro {
45
- private _isScanning;
46
- private _connectedDevices;
47
- static instance(): BleNitro;
48
- /**
49
- * Converts a 16- oder 32-Bit UUID to a 128-Bit UUID
50
- *
51
- * @param uuid 16-, 32- or 128-Bit UUID as string
52
- * @returns Full 128-Bit UUID
53
- */
54
- static normalizeGattUUID(uuid: string): string;
55
- static normalizeGattUUIDs(uuids: string[]): string[];
56
- /**
57
- * Start scanning for Bluetooth devices
58
- * @param filter Optional scan filter
59
- * @param callback Callback function called when a device is found
60
- * @returns Promise resolving to success state
61
- */
62
- startScan(filter: ScanFilter | undefined, callback: ScanCallback, onError?: (error: string) => void): void;
63
- /**
64
- * Stop scanning for Bluetooth devices
65
- * @returns Promise resolving to success state
66
- */
67
- stopScan(): void;
68
- /**
69
- * Check if currently scanning for devices
70
- * @returns Promise resolving to scanning state
71
- */
72
- isScanning(): boolean;
73
- /**
74
- * Get all currently connected devices
75
- * @param services Optional list of service UUIDs to filter by
76
- * @returns Array of connected devices
77
- */
78
- getConnectedDevices(services?: string[]): BLEDevice[];
79
- /**
80
- * Connect to a Bluetooth device
81
- * @param deviceId ID of the device to connect to
82
- * @param onDisconnect Optional callback for disconnect events
83
- * @returns Promise resolving when connected
84
- */
85
- connect(deviceId: string, onDisconnect?: DisconnectEventCallback): Promise<string>;
86
- /**
87
- * Disconnect from a Bluetooth device
88
- * @param deviceId ID of the device to disconnect from
89
- * @returns Promise resolving when disconnected
90
- */
91
- disconnect(deviceId: string): Promise<void>;
92
- /**
93
- * Check if connected to a device
94
- * @param deviceId ID of the device to check
95
- * @returns Promise resolving to connection state
96
- */
97
- isConnected(deviceId: string): boolean;
98
- /**
99
- * Request a new MTU size
100
- * @param deviceId ID of the device
101
- * @param mtu New MTU size, min is 23, max is 517
102
- * @returns On Android: new MTU size; on iOS: current MTU size as it is handled by iOS itself; on error: -1
103
- */
104
- requestMTU(deviceId: string, mtu: number): number;
105
- /**
106
- * Discover services for a connected device
107
- * @param deviceId ID of the device
108
- * @returns Promise resolving when services are discovered
109
- */
110
- discoverServices(deviceId: string): Promise<boolean>;
111
- /**
112
- * Get services for a connected device
113
- * @param deviceId ID of the device
114
- * @returns Promise resolving to array of service UUIDs
115
- */
116
- getServices(deviceId: string): Promise<string[]>;
117
- /**
118
- * Get characteristics for a service
119
- * @param deviceId ID of the device
120
- * @param serviceId ID of the service
121
- * @returns Promise resolving to array of characteristic UUIDs
122
- */
123
- getCharacteristics(deviceId: string, serviceId: string): string[];
124
- /**
125
- * Read a characteristic value
126
- * @param deviceId ID of the device
127
- * @param serviceId ID of the service
128
- * @param characteristicId ID of the characteristic
129
- * @returns Promise resolving to the characteristic data as ArrayBuffer
130
- */
131
- readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer>;
132
- /**
133
- * Write a value to a characteristic
134
- * @param deviceId ID of the device
135
- * @param serviceId ID of the service
136
- * @param characteristicId ID of the characteristic
137
- * @param data Data to write as ArrayBuffer
138
- * @param withResponse Whether to wait for response
139
- * @returns Promise resolving when write is complete
140
- */
141
- writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: ArrayBuffer, withResponse?: boolean): Promise<boolean>;
142
- /**
143
- * Subscribe to characteristic notifications
144
- * @param deviceId ID of the device
145
- * @param serviceId ID of the service
146
- * @param characteristicId ID of the characteristic
147
- * @param callback Callback function called when notification is received
148
- * @returns Promise resolving when subscription is complete
149
- */
150
- subscribeToCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: CharacteristicUpdateCallback): Subscription;
151
- /**
152
- * Unsubscribe from characteristic notifications
153
- * @param deviceId ID of the device
154
- * @param serviceId ID of the service
155
- * @param characteristicId ID of the characteristic
156
- * @returns Promise resolving when unsubscription is complete
157
- */
158
- unsubscribeFromCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void>;
159
- /**
160
- * Check if Bluetooth is enabled
161
- * @returns Promise resolving to Bluetooth state
162
- */
163
- isBluetoothEnabled(): boolean;
164
- /**
165
- * Request to enable Bluetooth (Android only)
166
- * @returns Promise resolving when Bluetooth is enabled
167
- */
168
- requestBluetoothEnable(): Promise<boolean>;
169
- /**
170
- * Get the current Bluetooth state
171
- * @returns Promise resolving to Bluetooth state
172
- * @see BLEState
173
- */
174
- state(): BLEState;
175
- /**
176
- * Subscribe to Bluetooth state changes
177
- * @param callback Callback function called when state changes
178
- * @param emitInitial Whether to emit initial state callback
179
- * @returns Promise resolving when subscription is complete
180
- * @see BLEState
181
- */
182
- subscribeToStateChange(callback: (state: BLEState) => void, emitInitial?: boolean): Subscription;
183
- /**
184
- * Open Bluetooth settings
185
- * @returns Promise resolving when settings are opened
186
- */
187
- openSettings(): Promise<void>;
188
- }
189
- 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
- }
1
+ import { BleNitroManager } from "./manager";
2
+ export { BLEState, AndroidScanMode, } from "./manager";
40
3
  let _instance;
41
- export class BleNitro {
42
- constructor() {
43
- this._isScanning = false;
44
- this._connectedDevices = {};
45
- }
4
+ export class BleNitro extends BleNitroManager {
46
5
  static instance() {
47
6
  if (!_instance) {
48
7
  _instance = new BleNitro();
49
8
  }
50
9
  return _instance;
51
10
  }
52
- /**
53
- * Converts a 16- oder 32-Bit UUID to a 128-Bit UUID
54
- *
55
- * @param uuid 16-, 32- or 128-Bit UUID as string
56
- * @returns Full 128-Bit UUID
57
- */
58
- static normalizeGattUUID(uuid) {
59
- const cleanUuid = uuid.toLowerCase();
60
- // 128-Bit UUID → normalisieren
61
- if (cleanUuid.length === 36 && cleanUuid.includes("-")) {
62
- return cleanUuid;
63
- }
64
- // GATT-Service UUIDs
65
- // 16- oder 32-Bit UUID → 128-Bit UUID
66
- const padded = cleanUuid.padStart(8, "0");
67
- return `${padded}-0000-1000-8000-00805f9b34fb`;
68
- }
69
- static normalizeGattUUIDs(uuids) {
70
- return uuids.map((uuid) => BleNitro.normalizeGattUUID(uuid));
71
- }
72
- /**
73
- * Start scanning for Bluetooth devices
74
- * @param filter Optional scan filter
75
- * @param callback Callback function called when a device is found
76
- * @returns Promise resolving to success state
77
- */
78
- startScan(filter = {}, callback, onError) {
79
- if (this._isScanning) {
80
- return;
81
- }
82
- // Create native scan filter with defaults
83
- const nativeFilter = {
84
- serviceUUIDs: filter.serviceUUIDs || [],
85
- rssiThreshold: filter.rssiThreshold ?? -100,
86
- allowDuplicates: filter.allowDuplicates ?? false,
87
- androidScanMode: mapAndroidScanModeToNativeAndroidScanMode(filter.androidScanMode ?? AndroidScanMode.Balanced),
88
- };
89
- // Create callback wrapper
90
- const scanCallback = (device, error) => {
91
- if (error && !device) {
92
- this._isScanning = false;
93
- onError?.(error);
94
- return;
95
- }
96
- device = device; // eslint-disable-line @typescript-eslint/no-non-null-assertion
97
- // Convert manufacturer data to Uint8Arrays
98
- const convertedDevice = {
99
- ...device,
100
- serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
101
- manufacturerData: {
102
- companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
103
- id: entry.id,
104
- data: entry.data
105
- }))
106
- }
107
- };
108
- callback(convertedDevice);
109
- };
110
- // Start scan
111
- BleNitroNative.startScan(nativeFilter, scanCallback);
112
- this._isScanning = true;
113
- }
114
- /**
115
- * Stop scanning for Bluetooth devices
116
- * @returns Promise resolving to success state
117
- */
118
- stopScan() {
119
- if (!this._isScanning) {
120
- return;
121
- }
122
- BleNitroNative.stopScan();
123
- this._isScanning = false;
124
- }
125
- /**
126
- * Check if currently scanning for devices
127
- * @returns Promise resolving to scanning state
128
- */
129
- isScanning() {
130
- this._isScanning = BleNitroNative.isScanning();
131
- return this._isScanning;
132
- }
133
- /**
134
- * Get all currently connected devices
135
- * @param services Optional list of service UUIDs to filter by
136
- * @returns Array of connected devices
137
- */
138
- getConnectedDevices(services) {
139
- const devices = BleNitroNative.getConnectedDevices(services || []);
140
- // Normalize service UUIDs - manufacturer data already comes as ArrayBuffers
141
- return devices.map(device => ({
142
- ...device,
143
- serviceUUIDs: BleNitro.normalizeGattUUIDs(device.serviceUUIDs),
144
- manufacturerData: {
145
- companyIdentifiers: device.manufacturerData.companyIdentifiers.map(entry => ({
146
- id: entry.id,
147
- data: entry.data
148
- }))
149
- }
150
- }));
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(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 ArrayBuffer
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), 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, 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();