feat(mp): DeviceType 扩展支持 blood_pressure/blood_glucose + 适配器接口改数组返回

This commit is contained in:
iven
2026-04-28 19:27:14 +08:00
parent 83e243f03e
commit 8a5b14e087
3 changed files with 18 additions and 14 deletions

View File

@@ -172,14 +172,14 @@ export class BLEManager {
// 监听数据通知 // 监听数据通知
Taro.onBLECharacteristicValueChange((res: any) => { Taro.onBLECharacteristicValueChange((res: any) => {
if (res.deviceId !== device.deviceId) return; if (res.deviceId !== device.deviceId) return;
const reading = device.adapter!.parseNotification( const newReadings = device.adapter!.parseNotification(
res.serviceId, res.serviceId,
res.characteristicId, res.characteristicId,
res.value, res.value,
); );
if (reading) { if (newReadings.length > 0) {
this.readings = [...this.readings, reading]; this.readings = [...this.readings, ...newReadings];
this.onReadings?.([reading]); this.onReadings?.(newReadings);
} }
}); });

View File

@@ -56,27 +56,27 @@ export const XiaomiBandAdapter: DeviceAdapter = {
_serviceUUID: string, _serviceUUID: string,
charUUID: string, charUUID: string,
data: ArrayBuffer, data: ArrayBuffer,
): NormalizedReading | null { ): NormalizedReading[] {
if (charUUID.toUpperCase().includes('2A37')) { if (charUUID.toUpperCase().includes('2A37')) {
const hr = parseHeartRate(data); const hr = parseHeartRate(data);
if (hr !== null && hr > 0 && hr < 300) { if (hr !== null && hr > 0 && hr < 300) {
return { return [{
device_type: 'heart_rate', device_type: 'heart_rate',
values: { heart_rate: hr }, values: { heart_rate: hr },
measured_at: new Date().toISOString(), measured_at: new Date().toISOString(),
}; }];
} }
} }
return null; return [];
}, },
parseReadResponse( parseReadResponse(
_serviceUUID: string, _serviceUUID: string,
_charUUID: string, _charUUID: string,
_data: ArrayBuffer, _data: ArrayBuffer,
): NormalizedReading | null { ): NormalizedReading[] {
// 读取模式暂不支持,使用通知模式获取数据 // 读取模式暂不支持,使用通知模式获取数据
return null; return [];
}, },
}; };

View File

@@ -7,12 +7,16 @@ export type DeviceType =
| 'steps' | 'steps'
| 'sleep' | 'sleep'
| 'temperature' | 'temperature'
| 'stress'; | 'stress'
| 'blood_pressure'
| 'blood_glucose';
/** 标准化的设备读数 */ /** 标准化的设备读数 */
export interface NormalizedReading { export interface NormalizedReading {
device_type: DeviceType; device_type: DeviceType;
values: Record<string, number>; values: Record<string, number | string>;
/** 多指标设备的具体指标(如 systolic/diastolic */
metric?: string;
measured_at: string; measured_at: string;
} }
@@ -38,14 +42,14 @@ export interface DeviceAdapter {
serviceUUID: string, serviceUUID: string,
charUUID: string, charUUID: string,
data: ArrayBuffer, data: ArrayBuffer,
): NormalizedReading | null; ): NormalizedReading[];
/** 解析 BLE 读取数据为标准读数 */ /** 解析 BLE 读取数据为标准读数 */
parseReadResponse( parseReadResponse(
serviceUUID: string, serviceUUID: string,
charUUID: string, charUUID: string,
data: ArrayBuffer, data: ArrayBuffer,
): NormalizedReading | null; ): NormalizedReading[];
} }
/** 扫描发现的 BLE 设备 */ /** 扫描发现的 BLE 设备 */