UNPKG

react-native-ble-nitro

Version:

High-performance React Native BLE library built on Nitro Modules - drop-in replacement for react-native-ble-plx

192 lines (191 loc) 9.89 kB
/** * BleManager Compatibility Factory * * Creates BleManager instances with full react-native-ble-plx compatibility * by wrapping the Nitro implementation with compatibility shims */ import { createBleManager } from './BleManagerFactory'; import { DeviceWrapper } from './compatibility/deviceWrapper'; import { stateToString, logLevelToString, normalizeLogLevel, normalizeCharacteristicSubscriptionType } from './compatibility/enums'; /** * BleManager wrapper that provides react-native-ble-plx compatibility */ export class BleManagerCompat { constructor(options) { this.bleManager = createBleManager(options); } // Lifecycle async destroy() { return await this.bleManager.destroy(); } // Common operations with compatibility async setLogLevel(logLevel) { const normalizedLogLevel = normalizeLogLevel(logLevel); const result = await this.bleManager.setLogLevel(normalizedLogLevel); return logLevelToString(result); } async logLevel() { const result = await this.bleManager.logLevel(); return logLevelToString(result); } async cancelTransaction(transactionId) { return await this.bleManager.cancelTransaction(transactionId); } // State management with string conversion async enable(transactionId) { await this.bleManager.enable(transactionId); return this; } async disable(transactionId) { await this.bleManager.disable(transactionId); return this; } async state() { const result = await this.bleManager.state(); return stateToString(result); } onStateChange(listener, emitCurrentState) { return this.bleManager.onStateChange((state) => { listener(stateToString(state)); }, emitCurrentState); } // Device scanning with compatibility wrappers async startDeviceScan(uuids, options, listener) { return await this.bleManager.startDeviceScan(uuids, options, (error, device) => { listener(error, device ? new DeviceWrapper(this.createDeviceFromNative(device)) : null); }); } async stopDeviceScan() { return await this.bleManager.stopDeviceScan(); } // Connection management async connectToDevice(deviceIdentifier, options) { // Provide defaults for Nitro's required fields const connectionOptions = { autoConnect: options?.autoConnect ?? false, requestMTU: options?.requestMTU ?? 23, timeout: options?.timeout ?? 0, }; const result = await this.bleManager.connectToDevice(deviceIdentifier, connectionOptions); return new DeviceWrapper(this.createDeviceFromNative(result)); } async cancelDeviceConnection(deviceIdentifier) { const result = await this.bleManager.cancelDeviceConnection(deviceIdentifier); return new DeviceWrapper(this.createDeviceFromNative(result)); } async isDeviceConnected(deviceIdentifier) { return await this.bleManager.isDeviceConnected(deviceIdentifier); } onDeviceDisconnected(deviceIdentifier, listener) { return this.bleManager.onDeviceDisconnected(deviceIdentifier, (error, device) => { listener(error, device ? new DeviceWrapper(this.createDeviceFromNative(device)) : null); }); } // Device discovery async devices(deviceIdentifiers) { const result = await this.bleManager.devices(deviceIdentifiers); return result.map(device => new DeviceWrapper(this.createDeviceFromNative(device))); } async connectedDevices(serviceUUIDs) { const result = await this.bleManager.connectedDevices(serviceUUIDs); return result.map(device => new DeviceWrapper(this.createDeviceFromNative(device))); } // RSSI and MTU operations async readRSSIForDevice(deviceIdentifier, transactionId) { const result = await this.bleManager.readRSSIForDevice(deviceIdentifier, transactionId); return new DeviceWrapper(this.createDeviceFromNative(result)); } async requestMTUForDevice(deviceIdentifier, mtu, transactionId) { const result = await this.bleManager.requestMTUForDevice(deviceIdentifier, mtu, transactionId); return new DeviceWrapper(this.createDeviceFromNative(result)); } async requestConnectionPriorityForDevice(deviceIdentifier, connectionPriority, transactionId) { const result = await this.bleManager.requestConnectionPriorityForDevice(deviceIdentifier, connectionPriority, transactionId); return new DeviceWrapper(this.createDeviceFromNative(result)); } // Service discovery async discoverAllServicesAndCharacteristicsForDevice(deviceIdentifier, transactionId) { const result = await this.bleManager.discoverAllServicesAndCharacteristicsForDevice(deviceIdentifier, transactionId); return new DeviceWrapper(this.createDeviceFromNative(result)); } // Service operations async servicesForDevice(deviceIdentifier) { return await this.bleManager.servicesForDevice(deviceIdentifier); } // Characteristic operations async characteristicsForDevice(deviceIdentifier, serviceUUID) { return await this.bleManager.characteristicsForDevice(deviceIdentifier, serviceUUID); } async readCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, transactionId) { return await this.bleManager.readCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, transactionId); } async writeCharacteristicWithResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId) { return await this.bleManager.writeCharacteristicWithResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId); } async writeCharacteristicWithoutResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId) { return await this.bleManager.writeCharacteristicWithoutResponseForDevice(deviceIdentifier, serviceUUID, characteristicUUID, base64Value, transactionId); } monitorCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, listener, transactionId, subscriptionType) { const nitroSubscriptionType = subscriptionType ? normalizeCharacteristicSubscriptionType(subscriptionType) : undefined; return this.bleManager.monitorCharacteristicForDevice(deviceIdentifier, serviceUUID, characteristicUUID, listener, transactionId, nitroSubscriptionType); } // Descriptor operations async descriptorsForDevice(deviceIdentifier, serviceUUID, characteristicUUID) { return await this.bleManager.descriptorsForDevice(deviceIdentifier, serviceUUID, characteristicUUID); } async readDescriptorForDevice(deviceIdentifier, serviceUUID, characteristicUUID, descriptorUUID, transactionId) { return await this.bleManager.readDescriptorForDevice(deviceIdentifier, serviceUUID, characteristicUUID, descriptorUUID, transactionId); } async writeDescriptorForDevice(deviceIdentifier, serviceUUID, characteristicUUID, descriptorUUID, valueBase64, transactionId) { return await this.bleManager.writeDescriptorForDevice(deviceIdentifier, serviceUUID, characteristicUUID, descriptorUUID, valueBase64, transactionId); } /** * Helper method to create a Device wrapper from NativeDevice data * This is a temporary method until we have proper Device Nitro objects */ createDeviceFromNative(nativeDevice) { // This is a placeholder - in the actual implementation, we'd need to create // proper Nitro Device objects, but for now we'll work with the native data return { id: nativeDevice.id, deviceName: nativeDevice.name, rssi: nativeDevice.rssi, mtu: nativeDevice.mtu, manufacturerData: nativeDevice.manufacturerData, rawScanRecord: nativeDevice.rawScanRecord, serviceData: nativeDevice.serviceData, serviceUUIDs: nativeDevice.serviceUUIDs, localName: nativeDevice.localName, txPowerLevel: nativeDevice.txPowerLevel, solicitedServiceUUIDs: nativeDevice.solicitedServiceUUIDs, isConnectable: nativeDevice.isConnectable, overflowServiceUUIDs: nativeDevice.overflowServiceUUIDs, // Add placeholder methods - these would be implemented in the actual Device class requestConnectionPriority: async () => this.createDeviceFromNative(nativeDevice), readRSSI: async () => this.createDeviceFromNative(nativeDevice), requestMTU: async () => this.createDeviceFromNative(nativeDevice), connect: async () => this.createDeviceFromNative(nativeDevice), cancelConnection: async () => this.createDeviceFromNative(nativeDevice), isConnected: async () => false, onDisconnected: () => ({ remove: () => { } }), discoverAllServicesAndCharacteristics: async () => this.createDeviceFromNative(nativeDevice), services: async () => [], characteristicsForService: async () => [], readCharacteristicForService: async () => ({}), writeCharacteristicWithResponseForService: async () => ({}), writeCharacteristicWithoutResponseForService: async () => ({}), monitorCharacteristicForService: () => ({ remove: () => { } }), descriptorsForService: async () => [], readDescriptorForService: async () => ({}), writeDescriptorForService: async () => ({}), }; } } /** * Factory function to create a compatibility BleManager */ export function createBleManagerCompat(options) { return new BleManagerCompat(options); }