expo-ble模块测试demo
This commit is contained in:
76
ble/services/DeviceInfoService.ts
Normal file
76
ble/services/DeviceInfoService.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import {BleProtocolService} from './BleProtocolService';
|
||||
import {COMMAND_TYPES, RESPONSE_TYPES} from '../protocol/Constants';
|
||||
import {DeviceInfo, ActivationStatus} from '../protocol/types';
|
||||
import {Buffer} from 'buffer';
|
||||
|
||||
export class DeviceInfoService {
|
||||
private protocol = BleProtocolService.getInstance();
|
||||
private static instance: DeviceInfoService;
|
||||
private listeners: Map<string, Set<Function>> = new Map();
|
||||
|
||||
private constructor() {
|
||||
this.protocol.addListener(RESPONSE_TYPES.DEVICE_INFO_REPORT, this.onDeviceInfo);
|
||||
this.protocol.addListener(RESPONSE_TYPES.ACTIVATION_STATUS, this.onActivationStatus);
|
||||
}
|
||||
|
||||
public static getInstance(): DeviceInfoService {
|
||||
if (!DeviceInfoService.instance) {
|
||||
DeviceInfoService.instance = new DeviceInfoService();
|
||||
}
|
||||
return DeviceInfoService.instance;
|
||||
}
|
||||
|
||||
private onDeviceInfo = (data: ArrayBuffer, deviceId: string) => {
|
||||
try {
|
||||
const str = Buffer.from(data).toString('utf-8');
|
||||
// Remove null characters if any
|
||||
const cleanStr = str.replace(/\0/g, '');
|
||||
const json = JSON.parse(cleanStr) as DeviceInfo;
|
||||
this.emit('deviceInfo', json);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse device info", e);
|
||||
}
|
||||
}
|
||||
|
||||
private onActivationStatus = (data: ArrayBuffer, deviceId: string) => {
|
||||
try {
|
||||
const str = Buffer.from(data).toString('utf-8');
|
||||
const cleanStr = str.replace(/\0/g, '');
|
||||
const json = JSON.parse(cleanStr) as ActivationStatus;
|
||||
this.emit('activationStatus', json);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse activation status", e);
|
||||
}
|
||||
}
|
||||
|
||||
public addListener(event: string, cb: Function) {
|
||||
if (!this.listeners.has(event)) this.listeners.set(event, new Set());
|
||||
this.listeners.get(event)!.add(cb);
|
||||
}
|
||||
|
||||
public removeListener(event: string, cb: Function) {
|
||||
if (this.listeners.has(event)) {
|
||||
this.listeners.get(event)!.delete(cb);
|
||||
}
|
||||
}
|
||||
|
||||
private emit(event: string, data: any) {
|
||||
this.listeners.get(event)?.forEach(cb => cb(data));
|
||||
}
|
||||
|
||||
public async queryDeviceInfo(deviceId: string) {
|
||||
await this.protocol.send(deviceId, COMMAND_TYPES.DEVICE_INFO_SETTINGS, {
|
||||
type: COMMAND_TYPES.DEVICE_INFO_SETTINGS
|
||||
});
|
||||
}
|
||||
|
||||
public async queryActivationStatus(deviceId: string) {
|
||||
await this.protocol.send(deviceId, COMMAND_TYPES.ACTIVATION_QUERY, {
|
||||
type: COMMAND_TYPES.ACTIVATION_QUERY
|
||||
});
|
||||
}
|
||||
|
||||
public async queryDeviceVersion(deviceId: string) {
|
||||
await this.protocol.send(deviceId, COMMAND_TYPES.VERSION_QUERY, {type: COMMAND_TYPES.VERSION_QUERY})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user