react-native-ble-nitro 1.0.0-alpha.1 → 1.0.0-beta.1
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/android/src/main/kotlin/co/zyke/ble/BleNitroBleManager.kt +103 -0
- package/ios/BleNitroBleManager.swift +73 -0
- package/lib/BleManagerCompatFactory.d.ts +7 -5
- package/lib/BleManagerCompatFactory.js +6 -3
- package/lib/BleManagerFactory.d.ts +21 -3
- package/lib/BleManagerFactory.js +53 -2
- package/lib/compatibility/constants.d.ts +1 -1
- package/lib/compatibility/constants.js +1 -1
- package/lib/compatibility/deviceWrapper.d.ts +2 -2
- package/lib/compatibility/deviceWrapper.js +2 -2
- package/lib/compatibility/enums.d.ts +34 -39
- package/lib/compatibility/enums.js +126 -91
- package/lib/compatibility/index.d.ts +5 -4
- package/lib/compatibility/index.js +5 -5
- package/lib/compatibility/serviceData.d.ts +1 -1
- package/lib/errors/BleError.d.ts +2 -2
- package/lib/errors/BleError.js +1 -1
- package/lib/index.d.ts +10 -7
- package/lib/index.js +10 -6
- package/lib/specs/BleManager.nitro.d.ts +9 -1
- package/lib/specs/Characteristic.nitro.d.ts +1 -1
- package/lib/specs/Descriptor.nitro.d.ts +1 -1
- package/lib/specs/Device.nitro.d.ts +1 -1
- package/lib/specs/Service.nitro.d.ts +1 -1
- package/lib/utils/base64.d.ts +1 -1
- package/lib/utils/index.d.ts +2 -2
- package/lib/utils/index.js +2 -2
- package/lib/utils/uuid.d.ts +1 -1
- package/package.json +4 -3
- package/src/BleManagerCompatFactory.ts +11 -13
- package/src/BleManagerFactory.ts +68 -5
- package/src/__tests__/BleManager.test.ts +7 -7
- package/src/__tests__/compatibility/enums.test.ts +104 -94
- package/src/compatibility/constants.ts +1 -1
- package/src/compatibility/deviceWrapper.ts +4 -4
- package/src/compatibility/enums.ts +146 -116
- package/src/compatibility/index.ts +13 -5
- package/src/compatibility/serviceData.ts +1 -1
- package/src/errors/BleError.ts +2 -2
- package/src/index.ts +22 -7
- package/src/specs/BleManager.nitro.ts +17 -4
- package/src/specs/Characteristic.nitro.ts +1 -1
- package/src/specs/Descriptor.nitro.ts +1 -1
- package/src/specs/Device.nitro.ts +1 -1
- package/src/specs/Service.nitro.ts +1 -1
- package/src/utils/base64.ts +1 -1
- package/src/utils/index.ts +2 -2
- package/src/utils/uuid.ts +1 -1
|
@@ -57,6 +57,11 @@ class BleNitroBleManager(private val context: ReactApplicationContext) : HybridB
|
|
|
57
57
|
private var logLevel: LogLevel = LogLevel.None
|
|
58
58
|
private var isScanning = false
|
|
59
59
|
private var scanCallback: ScanCallback? = null
|
|
60
|
+
|
|
61
|
+
// State restoration
|
|
62
|
+
private var restoreIdentifier: String? = null
|
|
63
|
+
private var isInitialized = false
|
|
64
|
+
private var restoredState: BleRestoredState? = null
|
|
60
65
|
private var scanListener: ((NativeBleError?, NativeDevice?) -> Unit)? = null
|
|
61
66
|
private var stateChangeListener: ((State) -> Unit)? = null
|
|
62
67
|
|
|
@@ -100,6 +105,104 @@ class BleNitroBleManager(private val context: ReactApplicationContext) : HybridB
|
|
|
100
105
|
}
|
|
101
106
|
}
|
|
102
107
|
|
|
108
|
+
override fun initialize(options: BleManagerNitroOptions): Promise<Unit> = Promise.resolve {
|
|
109
|
+
options.restoreStateIdentifier?.let { restoreId ->
|
|
110
|
+
this.restoreIdentifier = restoreId
|
|
111
|
+
|
|
112
|
+
// Attempt to restore previous state from SharedPreferences
|
|
113
|
+
restoreConnectionState(restoreId)
|
|
114
|
+
|
|
115
|
+
Log.d(TAG, "BleNitro: Initialized with restore state identifier: $restoreId")
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.isInitialized = true
|
|
119
|
+
Log.d(TAG, "BleNitro: BLE Manager initialized")
|
|
120
|
+
Unit
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
override fun getRestoredState(): Promise<BleRestoredState?> = Promise.resolve {
|
|
124
|
+
restoredState
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private fun restoreConnectionState(restoreId: String) {
|
|
128
|
+
try {
|
|
129
|
+
val sharedPrefs = context.getSharedPreferences("BleNitro_$restoreId", Context.MODE_PRIVATE)
|
|
130
|
+
val connectedDevicesJson = sharedPrefs.getString("connected_devices", null)
|
|
131
|
+
|
|
132
|
+
connectedDevicesJson?.let { json ->
|
|
133
|
+
// Parse stored device IDs and attempt to reconnect
|
|
134
|
+
val deviceIds = json.split(",").filter { it.isNotEmpty() }
|
|
135
|
+
|
|
136
|
+
if (deviceIds.isNotEmpty()) {
|
|
137
|
+
Log.d(TAG, "BleNitro: Restoring ${deviceIds.size} devices")
|
|
138
|
+
|
|
139
|
+
deviceIds.forEach { deviceId ->
|
|
140
|
+
// Attempt to reconnect to previously connected devices
|
|
141
|
+
restoreDeviceConnection(deviceId)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Store restored state (initially empty, will be populated as devices reconnect)
|
|
145
|
+
val initialDevices = deviceIds.mapNotNull { deviceId ->
|
|
146
|
+
bluetoothAdapter?.getRemoteDevice(deviceId)?.let { device ->
|
|
147
|
+
createNativeDevice(device, null, -1) // Initial state without GATT
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (initialDevices.isNotEmpty()) {
|
|
152
|
+
this.restoredState = BleRestoredState(initialDevices)
|
|
153
|
+
Log.d(TAG, "BleNitro: Stored restored state with ${initialDevices.size} devices")
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
} catch (e: Exception) {
|
|
158
|
+
Log.e(TAG, "BleNitro: Error restoring connection state", e)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private fun restoreDeviceConnection(deviceId: String) {
|
|
163
|
+
try {
|
|
164
|
+
val bluetoothDevice = bluetoothAdapter?.getRemoteDevice(deviceId)
|
|
165
|
+
bluetoothDevice?.let { device ->
|
|
166
|
+
Log.d(TAG, "BleNitro: Attempting to restore connection to $deviceId")
|
|
167
|
+
|
|
168
|
+
val gatt = device.connectGatt(context, true, object : BluetoothGattCallback() {
|
|
169
|
+
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
|
|
170
|
+
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
|
171
|
+
connectedDevices[deviceId] = gatt!!
|
|
172
|
+
Log.d(TAG, "BleNitro: Successfully restored connection to $deviceId")
|
|
173
|
+
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
|
174
|
+
Log.d(TAG, "BleNitro: Failed to restore connection to $deviceId")
|
|
175
|
+
gatt?.close()
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
if (gatt != null) {
|
|
181
|
+
deviceCallbacks[deviceId] = gatt
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
} catch (e: Exception) {
|
|
185
|
+
Log.e(TAG, "BleNitro: Error restoring device connection for $deviceId", e)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private fun saveConnectionState() {
|
|
190
|
+
restoreIdentifier?.let { restoreId ->
|
|
191
|
+
try {
|
|
192
|
+
val sharedPrefs = context.getSharedPreferences("BleNitro_$restoreId", Context.MODE_PRIVATE)
|
|
193
|
+
val deviceIds = connectedDevices.keys.joinToString(",")
|
|
194
|
+
|
|
195
|
+
sharedPrefs.edit()
|
|
196
|
+
.putString("connected_devices", deviceIds)
|
|
197
|
+
.apply()
|
|
198
|
+
|
|
199
|
+
Log.d(TAG, "BleNitro: Saved connection state for ${connectedDevices.size} devices")
|
|
200
|
+
} catch (e: Exception) {
|
|
201
|
+
Log.e(TAG, "BleNitro: Error saving connection state", e)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
103
206
|
override fun setLogLevel(logLevel: LogLevel): Promise<LogLevel> = Promise.resolve {
|
|
104
207
|
this.logLevel = logLevel
|
|
105
208
|
logLevel
|
|
@@ -26,12 +26,28 @@ public class BleNitroBleManager: HybridBleManagerSpec, CBCentralManagerDelegate
|
|
|
26
26
|
private var characteristicMonitors: [String: ((_ error: NativeBleError?, _ characteristic: NativeCharacteristic?) -> Void)] = [:]
|
|
27
27
|
private var pendingOperations: [String: Any] = [:]
|
|
28
28
|
|
|
29
|
+
// State restoration
|
|
30
|
+
private var restoreIdentifier: String?
|
|
31
|
+
private var isInitialized = false
|
|
32
|
+
private var restoredState: BleRestoredState?
|
|
33
|
+
|
|
29
34
|
// MARK: - Initialization
|
|
30
35
|
public override init() {
|
|
31
36
|
super.init()
|
|
37
|
+
// Initialize with default options - will be reconfigured in initialize() method
|
|
32
38
|
self.centralManager = CBCentralManager(delegate: self, queue: nil)
|
|
33
39
|
}
|
|
34
40
|
|
|
41
|
+
private func reinitializeCentralManager() {
|
|
42
|
+
// Create CBCentralManager with state restoration if identifier is provided
|
|
43
|
+
var options: [String: Any] = [:]
|
|
44
|
+
if let restoreId = restoreIdentifier {
|
|
45
|
+
options[CBCentralManagerOptionRestoreIdentifierKey] = restoreId
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
self.centralManager = CBCentralManager(delegate: self, queue: nil, options: options.isEmpty ? nil : options)
|
|
49
|
+
}
|
|
50
|
+
|
|
35
51
|
public override var memorySize: Int {
|
|
36
52
|
return MemorySize.MemorySize_estimate(self)
|
|
37
53
|
}
|
|
@@ -51,6 +67,29 @@ public class BleNitroBleManager: HybridBleManagerSpec, CBCentralManagerDelegate
|
|
|
51
67
|
}
|
|
52
68
|
}
|
|
53
69
|
|
|
70
|
+
public func initialize(options: BleManagerNitroOptions) throws -> Promise<Void> {
|
|
71
|
+
return Promise.resolve(withBlock: {
|
|
72
|
+
if let restoreId = options.restoreStateIdentifier {
|
|
73
|
+
// Store the restore identifier
|
|
74
|
+
self.restoreIdentifier = restoreId
|
|
75
|
+
|
|
76
|
+
// Reinitialize the central manager with state restoration
|
|
77
|
+
self.reinitializeCentralManager()
|
|
78
|
+
|
|
79
|
+
print("BleNitro: Initialized with restore state identifier: \(restoreId)")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
self.isInitialized = true
|
|
83
|
+
print("BleNitro: BLE Manager initialized")
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public func getRestoredState() throws -> Promise<BleRestoredState?> {
|
|
88
|
+
return Promise.resolve(withBlock: {
|
|
89
|
+
return self.restoredState
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
54
93
|
public func setLogLevel(logLevel: LogLevel) throws -> Promise<LogLevel> {
|
|
55
94
|
return Promise.resolve(withBlock: {
|
|
56
95
|
self.logLevel = logLevel
|
|
@@ -491,6 +530,40 @@ extension BleNitroBleManager: CBPeripheralDelegate {
|
|
|
491
530
|
}
|
|
492
531
|
}
|
|
493
532
|
}
|
|
533
|
+
|
|
534
|
+
// MARK: - CBCentralManagerDelegate State Restoration
|
|
535
|
+
|
|
536
|
+
public func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
|
|
537
|
+
// Handle state restoration
|
|
538
|
+
if let peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral] {
|
|
539
|
+
print("BleNitro: Restoring \(peripherals.count) peripherals")
|
|
540
|
+
|
|
541
|
+
// Add restored peripherals to our connected devices
|
|
542
|
+
for peripheral in peripherals {
|
|
543
|
+
let deviceId = peripheral.identifier.uuidString
|
|
544
|
+
connectedDevices[deviceId] = peripheral
|
|
545
|
+
peripheral.delegate = self
|
|
546
|
+
|
|
547
|
+
print("BleNitro: Restored peripheral: \(deviceId)")
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// Store restored state for later retrieval
|
|
551
|
+
if !peripherals.isEmpty {
|
|
552
|
+
let restoredDevices = peripherals.map(createNativeDevice)
|
|
553
|
+
self.restoredState = BleRestoredState(connectedPeripherals: restoredDevices)
|
|
554
|
+
print("BleNitro: Stored restored state with \(restoredDevices.count) devices")
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
public func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
|
560
|
+
let newState = convertCBManagerStateToState(central.state)
|
|
561
|
+
|
|
562
|
+
// Notify state change listener if available
|
|
563
|
+
stateChangeListener?(newState)
|
|
564
|
+
|
|
565
|
+
print("BleNitro: Central manager state changed to: \(central.state.rawValue)")
|
|
566
|
+
}
|
|
494
567
|
}
|
|
495
568
|
|
|
496
569
|
/**
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { BleManagerOptions, UUID, DeviceId, TransactionId, ConnectionPriority, ConnectionOptions, ScanOptions, NativeService, NativeCharacteristic, NativeDescriptor, LogLevel, Subscription } from './specs/types';
|
|
8
8
|
import { DeviceWrapper } from './compatibility/deviceWrapper';
|
|
9
|
+
import { State as PlxState, LogLevel as PlxLogLevel } from './compatibility/enums';
|
|
9
10
|
/**
|
|
10
11
|
* BleManager wrapper that provides react-native-ble-plx compatibility
|
|
11
12
|
*/
|
|
@@ -13,13 +14,13 @@ export declare class BleManagerCompat {
|
|
|
13
14
|
private bleManager;
|
|
14
15
|
constructor(options?: BleManagerOptions);
|
|
15
16
|
destroy(): Promise<void>;
|
|
16
|
-
setLogLevel(logLevel: LogLevel | string): Promise<
|
|
17
|
-
logLevel(): Promise<
|
|
17
|
+
setLogLevel(logLevel: LogLevel | string): Promise<PlxLogLevel>;
|
|
18
|
+
logLevel(): Promise<PlxLogLevel>;
|
|
18
19
|
cancelTransaction(transactionId: TransactionId): Promise<void>;
|
|
19
20
|
enable(transactionId?: TransactionId): Promise<BleManagerCompat>;
|
|
20
21
|
disable(transactionId?: TransactionId): Promise<BleManagerCompat>;
|
|
21
|
-
state(): Promise<
|
|
22
|
-
onStateChange(listener: (newState:
|
|
22
|
+
state(): Promise<PlxState>;
|
|
23
|
+
onStateChange(listener: (newState: PlxState) => void, emitCurrentState?: boolean): Subscription;
|
|
23
24
|
startDeviceScan(uuids: UUID[] | null, options: ScanOptions | null, listener: (error: any | null, scannedDevice: DeviceWrapper | null) => void): Promise<void>;
|
|
24
25
|
stopDeviceScan(): Promise<void>;
|
|
25
26
|
connectToDevice(deviceIdentifier: DeviceId, options?: Partial<ConnectionOptions>): Promise<DeviceWrapper>;
|
|
@@ -37,7 +38,8 @@ export declare class BleManagerCompat {
|
|
|
37
38
|
readCharacteristicForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, transactionId?: TransactionId): Promise<NativeCharacteristic>;
|
|
38
39
|
writeCharacteristicWithResponseForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, base64Value: string, transactionId?: TransactionId): Promise<NativeCharacteristic>;
|
|
39
40
|
writeCharacteristicWithoutResponseForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, base64Value: string, transactionId?: TransactionId): Promise<NativeCharacteristic>;
|
|
40
|
-
monitorCharacteristicForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, listener: (error: any | null, characteristic: NativeCharacteristic | null) => void,
|
|
41
|
+
monitorCharacteristicForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, listener: (error: any | null, characteristic: NativeCharacteristic | null) => void, // TODO: COMPAT! use proper error type like in react-native-ble-plx
|
|
42
|
+
transactionId?: TransactionId, subscriptionType?: 'notification' | 'indication'): Subscription;
|
|
41
43
|
descriptorsForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID): Promise<NativeDescriptor[]>;
|
|
42
44
|
readDescriptorForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, descriptorUUID: UUID, transactionId?: TransactionId): Promise<NativeDescriptor>;
|
|
43
45
|
writeDescriptorForDevice(deviceIdentifier: DeviceId, serviceUUID: UUID, characteristicUUID: UUID, descriptorUUID: UUID, valueBase64: string, transactionId?: TransactionId): Promise<NativeDescriptor>;
|
|
@@ -50,7 +50,8 @@ export class BleManagerCompat {
|
|
|
50
50
|
}, emitCurrentState);
|
|
51
51
|
}
|
|
52
52
|
// Device scanning with compatibility wrappers
|
|
53
|
-
async startDeviceScan(uuids, options, listener
|
|
53
|
+
async startDeviceScan(uuids, options, listener // TODO: COMPAT! remove any and move to BleError as react-native-ble-plx uses this type as well!
|
|
54
|
+
) {
|
|
54
55
|
return await this.bleManager.startDeviceScan(uuids, options, (error, device) => {
|
|
55
56
|
listener(error, device ? new DeviceWrapper(this.createDeviceFromNative(device)) : null);
|
|
56
57
|
});
|
|
@@ -76,7 +77,8 @@ export class BleManagerCompat {
|
|
|
76
77
|
async isDeviceConnected(deviceIdentifier) {
|
|
77
78
|
return await this.bleManager.isDeviceConnected(deviceIdentifier);
|
|
78
79
|
}
|
|
79
|
-
onDeviceDisconnected(deviceIdentifier, listener
|
|
80
|
+
onDeviceDisconnected(deviceIdentifier, listener // TODO: COMPAT! use propper error type like in react-native-ble-plx!!!
|
|
81
|
+
) {
|
|
80
82
|
return this.bleManager.onDeviceDisconnected(deviceIdentifier, (error, device) => {
|
|
81
83
|
listener(error, device ? new DeviceWrapper(this.createDeviceFromNative(device)) : null);
|
|
82
84
|
});
|
|
@@ -125,7 +127,8 @@ export class BleManagerCompat {
|
|
|
125
127
|
async writeCharacteristicWithoutResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId) {
|
|
126
128
|
return await this.bleManager.writeCharacteristicWithoutResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId);
|
|
127
129
|
}
|
|
128
|
-
monitorCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, listener,
|
|
130
|
+
monitorCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, listener, // TODO: COMPAT! use proper error type like in react-native-ble-plx
|
|
131
|
+
transactionId, subscriptionType) {
|
|
129
132
|
const nitroSubscriptionType = subscriptionType
|
|
130
133
|
? normalizeCharacteristicSubscriptionType(subscriptionType)
|
|
131
134
|
: undefined;
|
|
@@ -1,12 +1,30 @@
|
|
|
1
|
-
import type { BleManager as BleManagerInterface } from './specs/BleManager.nitro';
|
|
2
|
-
import type { BleManagerOptions } from './specs/types';
|
|
1
|
+
import type { BleManager as BleManagerInterface } from './specs/BleManager.nitro.js';
|
|
2
|
+
import type { BleManagerOptions } from './specs/types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Creates a BleManager instance using Nitro Modules
|
|
5
5
|
* This function maintains compatibility with react-native-ble-plx's BleManager constructor
|
|
6
6
|
*/
|
|
7
7
|
export declare function createBleManager(options?: BleManagerOptions): BleManagerInterface;
|
|
8
|
+
/**
|
|
9
|
+
* Helper function to retrieve stored callbacks for a BleManager instance
|
|
10
|
+
* This is used internally when callbacks need to be invoked
|
|
11
|
+
*/
|
|
12
|
+
export declare function getStoredCallbacks(manager: BleManagerInterface): {
|
|
13
|
+
restoreStateFunction?: (restoredState: any) => void;
|
|
14
|
+
errorCodesToMessagesMapping?: {
|
|
15
|
+
[key: number]: string;
|
|
16
|
+
};
|
|
17
|
+
} | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Helper function to get custom error message if available
|
|
20
|
+
* @param manager The BleManager instance
|
|
21
|
+
* @param errorCode The BLE error code
|
|
22
|
+
* @param defaultMessage Default error message
|
|
23
|
+
* @returns Custom message if available, otherwise default message
|
|
24
|
+
*/
|
|
25
|
+
export declare function getCustomErrorMessage(manager: BleManagerInterface, errorCode: number, defaultMessage: string): string;
|
|
8
26
|
/**
|
|
9
27
|
* Legacy compatibility: Export a BleManager constructor function
|
|
10
28
|
* This maintains compatibility with code that imports { BleManager } from 'react-native-ble-plx'
|
|
11
29
|
*/
|
|
12
|
-
export declare const BleManager:
|
|
30
|
+
export declare const BleManager: (options?: BleManagerOptions) => BleManagerInterface;
|
package/lib/BleManagerFactory.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
// Store callbacks that can't be passed to Nitro
|
|
3
|
+
const storedCallbacks = new WeakMap();
|
|
2
4
|
/**
|
|
3
5
|
* Creates a BleManager instance using Nitro Modules
|
|
4
6
|
* This function maintains compatibility with react-native-ble-plx's BleManager constructor
|
|
@@ -9,10 +11,59 @@ export function createBleManager(options) {
|
|
|
9
11
|
throw new Error('Failed to create BleManager: Nitro module not found. ' +
|
|
10
12
|
'Make sure react-native-ble-nitro is properly installed and linked.');
|
|
11
13
|
}
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
+
// Initialize with options if provided
|
|
15
|
+
if (options) {
|
|
16
|
+
// Extract Nitro-compatible options
|
|
17
|
+
const nitroOptions = {
|
|
18
|
+
restoreStateIdentifier: options.restoreStateIdentifier,
|
|
19
|
+
};
|
|
20
|
+
// Store callbacks and mappings that can't be passed to Nitro
|
|
21
|
+
if (options.restoreStateFunction || options.errorCodesToMessagesMapping) {
|
|
22
|
+
storedCallbacks.set(BleManagerModule, {
|
|
23
|
+
restoreStateFunction: options.restoreStateFunction,
|
|
24
|
+
errorCodesToMessagesMapping: options.errorCodesToMessagesMapping,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// Note: initialize() is async but we need to maintain sync compatibility with react-native-ble-plx
|
|
28
|
+
// The initialization will happen asynchronously in the background
|
|
29
|
+
BleManagerModule.initialize(nitroOptions).then(async () => {
|
|
30
|
+
// Check for restored state and call the callback if available
|
|
31
|
+
if (options.restoreStateFunction) {
|
|
32
|
+
try {
|
|
33
|
+
const restoredState = await BleManagerModule.getRestoredState();
|
|
34
|
+
if (restoredState) {
|
|
35
|
+
options.restoreStateFunction(restoredState);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.warn('BleManager restore state callback failed:', error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}).catch(error => {
|
|
43
|
+
console.warn('BleManager initialization failed:', error);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
14
46
|
return BleManagerModule;
|
|
15
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Helper function to retrieve stored callbacks for a BleManager instance
|
|
50
|
+
* This is used internally when callbacks need to be invoked
|
|
51
|
+
*/
|
|
52
|
+
export function getStoredCallbacks(manager) {
|
|
53
|
+
return storedCallbacks.get(manager);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Helper function to get custom error message if available
|
|
57
|
+
* @param manager The BleManager instance
|
|
58
|
+
* @param errorCode The BLE error code
|
|
59
|
+
* @param defaultMessage Default error message
|
|
60
|
+
* @returns Custom message if available, otherwise default message
|
|
61
|
+
*/
|
|
62
|
+
export function getCustomErrorMessage(manager, errorCode, defaultMessage) {
|
|
63
|
+
const callbacks = storedCallbacks.get(manager);
|
|
64
|
+
const customMessage = callbacks?.errorCodesToMessagesMapping?.[errorCode];
|
|
65
|
+
return customMessage || defaultMessage;
|
|
66
|
+
}
|
|
16
67
|
/**
|
|
17
68
|
* Legacy compatibility: Export a BleManager constructor function
|
|
18
69
|
* This maintains compatibility with code that imports { BleManager } from 'react-native-ble-plx'
|
|
@@ -46,4 +46,4 @@ export declare const ConnectionPriority: {
|
|
|
46
46
|
readonly High: 1;
|
|
47
47
|
readonly LowPower: 2;
|
|
48
48
|
};
|
|
49
|
-
export { BleErrorCode, BleATTErrorCode, BleIOSErrorCode, BleAndroidErrorCode } from '../specs/types';
|
|
49
|
+
export { BleErrorCode, BleATTErrorCode, BleIOSErrorCode, BleAndroidErrorCode } from '../specs/types.js';
|
|
@@ -47,4 +47,4 @@ export const ConnectionPriority = {
|
|
|
47
47
|
LowPower: 2,
|
|
48
48
|
};
|
|
49
49
|
// Re-export all BLE error codes
|
|
50
|
-
export { BleErrorCode, BleATTErrorCode, BleIOSErrorCode, BleAndroidErrorCode } from '../specs/types';
|
|
50
|
+
export { BleErrorCode, BleATTErrorCode, BleIOSErrorCode, BleAndroidErrorCode } from '../specs/types.js';
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Wraps Nitro Device objects to provide the original react-native-ble-plx API
|
|
5
5
|
*/
|
|
6
|
-
import type { Device as NitroDevice } from '../specs/Device.nitro';
|
|
7
|
-
import type { UUID, Base64, DeviceId, TransactionId, ConnectionPriority, ConnectionOptions, NativeService, NativeCharacteristic, NativeDescriptor, Subscription } from '../specs/types';
|
|
6
|
+
import type { Device as NitroDevice } from '../specs/Device.nitro.js';
|
|
7
|
+
import type { UUID, Base64, DeviceId, TransactionId, ConnectionPriority, ConnectionOptions, NativeService, NativeCharacteristic, NativeDescriptor, Subscription } from '../specs/types.js';
|
|
8
8
|
/**
|
|
9
9
|
* Device wrapper that provides react-native-ble-plx compatibility
|
|
10
10
|
* Maps Nitro device properties to the expected API surface
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Wraps Nitro Device objects to provide the original react-native-ble-plx API
|
|
5
5
|
*/
|
|
6
|
-
import { serviceDataArrayToMap } from './serviceData';
|
|
7
|
-
import { normalizeCharacteristicSubscriptionType } from './enums';
|
|
6
|
+
import { serviceDataArrayToMap } from './serviceData.js';
|
|
7
|
+
import { normalizeCharacteristicSubscriptionType } from './enums.js';
|
|
8
8
|
/**
|
|
9
9
|
* Device wrapper that provides react-native-ble-plx compatibility
|
|
10
10
|
* Maps Nitro device properties to the expected API surface
|
|
@@ -1,43 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* React Native BLE Plx Compatible Enums and Types
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* These match the exact types from react-native-ble-plx for drop-in compatibility.
|
|
5
|
+
* This module provides conversion between Nitro's numeric enums and react-native-ble-plx types.
|
|
6
6
|
*/
|
|
7
|
-
import { State, LogLevel, CharacteristicSubscriptionType, RefreshGattMoment } from '../specs/types';
|
|
8
|
-
export declare
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
export declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export declare
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export declare function
|
|
32
|
-
export declare function
|
|
33
|
-
export declare function
|
|
34
|
-
export declare function
|
|
35
|
-
export declare function
|
|
36
|
-
export declare function
|
|
37
|
-
export declare function
|
|
38
|
-
export declare function stringToRefreshGattMoment(momentString: 'OnConnected'): RefreshGattMoment;
|
|
7
|
+
import { State as NitroState, LogLevel as NitroLogLevel, CharacteristicSubscriptionType as NitroCharacteristicSubscriptionType, RefreshGattMoment as NitroRefreshGattMoment } from '../specs/types.js';
|
|
8
|
+
export declare enum State {
|
|
9
|
+
Unknown = "Unknown",
|
|
10
|
+
Resetting = "Resetting",
|
|
11
|
+
Unsupported = "Unsupported",
|
|
12
|
+
Unauthorized = "Unauthorized",
|
|
13
|
+
PoweredOff = "PoweredOff",
|
|
14
|
+
PoweredOn = "PoweredOn"
|
|
15
|
+
}
|
|
16
|
+
export declare enum LogLevel {
|
|
17
|
+
None = "None",
|
|
18
|
+
Verbose = "Verbose",
|
|
19
|
+
Debug = "Debug",
|
|
20
|
+
Info = "Info",
|
|
21
|
+
Warning = "Warning",
|
|
22
|
+
Error = "Error"
|
|
23
|
+
}
|
|
24
|
+
export type CharacteristicSubscriptionType = 'notification' | 'indication';
|
|
25
|
+
export type RefreshGattMoment = 'OnConnected';
|
|
26
|
+
export declare function stateToString(state: NitroState): State;
|
|
27
|
+
export declare function logLevelToString(logLevel: NitroLogLevel): LogLevel;
|
|
28
|
+
export declare function characteristicSubscriptionTypeToString(type: NitroCharacteristicSubscriptionType): CharacteristicSubscriptionType;
|
|
29
|
+
export declare function refreshGattMomentToString(_moment: NitroRefreshGattMoment): RefreshGattMoment;
|
|
30
|
+
export declare function stringToState(stateString: State | string): NitroState;
|
|
31
|
+
export declare function stringToLogLevel(logLevelString: LogLevel | string): NitroLogLevel;
|
|
32
|
+
export declare function stringToCharacteristicSubscriptionType(typeString: CharacteristicSubscriptionType | string): NitroCharacteristicSubscriptionType;
|
|
33
|
+
export declare function stringToRefreshGattMoment(_momentString: RefreshGattMoment): NitroRefreshGattMoment;
|
|
34
|
+
export declare function normalizeState(state: NitroState | State | string): NitroState;
|
|
35
|
+
export declare function normalizeLogLevel(logLevel: NitroLogLevel | LogLevel | string): NitroLogLevel;
|
|
36
|
+
export declare function normalizeCharacteristicSubscriptionType(type: NitroCharacteristicSubscriptionType | CharacteristicSubscriptionType | string): NitroCharacteristicSubscriptionType;
|
|
37
|
+
export declare function normalizeRefreshGattMoment(_moment: NitroRefreshGattMoment | RefreshGattMoment | string): NitroRefreshGattMoment;
|
|
39
38
|
export declare function isStringEnumValue(value: any): boolean;
|
|
40
|
-
export declare function normalizeState(state: State | string): State;
|
|
41
|
-
export declare function normalizeLogLevel(logLevel: LogLevel | string): LogLevel;
|
|
42
|
-
export declare function normalizeCharacteristicSubscriptionType(type: CharacteristicSubscriptionType | 'notification' | 'indication'): CharacteristicSubscriptionType;
|
|
43
|
-
export declare function normalizeRefreshGattMoment(moment: RefreshGattMoment | 'OnConnected'): RefreshGattMoment;
|