import * as BluetoothManager from './bluetooth_manager.uts'; import { ServiceManager } from './service_manager.uts'; import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback, BleProtocolType, BluetoothService as BluetoothServiceContract } from '../interface.uts'; const serviceManager = ServiceManager.getInstance(); class MpWeixinBluetoothService implements BluetoothServiceContract { scanDevices(options?: ScanDevicesOptions | null): Promise { return BluetoothManager.scanDevices(options ?? null); } async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise { const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard'; return BluetoothManager.connectDevice(deviceId, proto, options ?? null); } async disconnectDevice(deviceId: string, protocol?: string): Promise { const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard'; return BluetoothManager.disconnectDevice(deviceId, proto); } getConnectedDevices(): MultiProtocolDevice[] { return BluetoothManager.getConnectedDevices(); } on(event: BleEvent | string, callback: BleEventCallback): void { BluetoothManager.on(event as BleEvent, callback); } off(event: BleEvent | string, callback?: BleEventCallback | null): void { BluetoothManager.off(event as BleEvent, callback ?? null); } getServices(deviceId: string): Promise { return serviceManager.getServices(deviceId); } getCharacteristics(deviceId: string, serviceId: string): Promise { return serviceManager.getCharacteristics(deviceId, serviceId); } async getAutoBleInterfaces(deviceId: string): Promise { const services = await this.getServices(deviceId); if (services.length == 0) throw new Error('未发现服务'); let serviceId = services[0].uuid; for (let i = 0; i < services.length; i++) { const uuid = services[i].uuid ?? ''; if (/^bae/i.test(uuid)) { serviceId = uuid; break; } } const characteristics = await this.getCharacteristics(deviceId, serviceId); if (characteristics.length == 0) throw new Error('未发现特征值'); let writeCharId = ''; let notifyCharId = ''; for (let i = 0; i < characteristics.length; i++) { const c = characteristics[i]; if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse == true)) { writeCharId = c.uuid; } if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) { notifyCharId = c.uuid; } } if (writeCharId == '' || notifyCharId == '') throw new Error('未找到合适的写入或通知特征'); return { serviceId, writeCharId, notifyCharId }; } subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise { return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData); } readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise { return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId); } writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise { return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, value, options); } unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise { return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId); } autoDiscoverAll(deviceId: string): Promise { return serviceManager.autoDiscoverAll(deviceId); } subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise { return serviceManager.subscribeAllNotifications(deviceId, onData); } } export class BluetoothServiceShape extends MpWeixinBluetoothService {} const bluetoothServiceInstance = new BluetoothServiceShape(); BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance); export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance; export function getBluetoothService(): BluetoothServiceShape { return bluetoothServiceInstance; } export { dfuManager } from './dfu_manager.uts';