91 lines
4.5 KiB
Plaintext
91 lines
4.5 KiB
Plaintext
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 HarmonyBluetoothService implements BluetoothServiceContract {
|
|
scanDevices(options?: ScanDevicesOptions | null): Promise<void> {
|
|
return BluetoothManager.scanDevices(options ?? null);
|
|
}
|
|
async connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt | null): Promise<void> {
|
|
const proto = (protocol != null && protocol !== '') ? (protocol as BleProtocolType) : 'standard';
|
|
return BluetoothManager.connectDevice(deviceId, proto, options ?? null);
|
|
}
|
|
async disconnectDevice(deviceId: string, protocol?: string): Promise<void> {
|
|
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<BleService[]> {
|
|
return serviceManager.getServices(deviceId);
|
|
}
|
|
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
|
|
return serviceManager.getCharacteristics(deviceId, serviceId);
|
|
}
|
|
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
|
|
const services = await this.getServices(deviceId);
|
|
if (services.length == 0) throw new Error('未发现服务');
|
|
let targetService = services[0].uuid;
|
|
for (let i = 0; i < services.length; i++) {
|
|
const uuid = services[i].uuid ?? '';
|
|
if (/^bae/i.test(uuid)) {
|
|
targetService = uuid;
|
|
break;
|
|
}
|
|
}
|
|
const characteristics = await this.getCharacteristics(deviceId, targetService);
|
|
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: targetService, writeCharId, notifyCharId };
|
|
}
|
|
subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
|
|
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
|
|
}
|
|
readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
|
|
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
|
|
}
|
|
writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise<boolean> {
|
|
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, value, options);
|
|
}
|
|
unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
|
|
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
|
|
}
|
|
autoDiscoverAll(deviceId: string): Promise<any> {
|
|
return serviceManager.autoDiscoverAll(deviceId);
|
|
}
|
|
subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
|
|
return serviceManager.subscribeAllNotifications(deviceId, onData);
|
|
}
|
|
}
|
|
|
|
export class BluetoothServiceShape extends HarmonyBluetoothService {}
|
|
|
|
const bluetoothServiceInstance = new BluetoothServiceShape();
|
|
BluetoothManager.setDefaultBluetoothService(bluetoothServiceInstance);
|
|
export const bluetoothService: BluetoothServiceContract = bluetoothServiceInstance;
|
|
export function getBluetoothService(): BluetoothServiceShape {
|
|
return bluetoothServiceInstance;
|
|
}
|
|
|
|
export { dfuManager } from './dfu_manager.uts';
|