feat(mp): BLE 血氧仪支持 + 服务发现增强
- 新增 Pulse Oximeter Service (0x1822) 支持,含 SFLOAT 解析 - 连接后自动扫描全部服务,发现并订阅已知健康 UUID - 设备同步页展示已发现的服务和可用数据类型标签 - 新增 BLEDiscoveredService / BLEDiscoveredCharacteristic 类型
This commit is contained in:
@@ -23,8 +23,44 @@ const SERVICES: Record<string, { uuid: string; chars: { notify: string; read: st
|
||||
read: '00002A35-0000-1000-8000-00805F9B34FB',
|
||||
},
|
||||
},
|
||||
pulse_oximeter: {
|
||||
uuid: '00001822-0000-1000-8000-00805F9B34FB',
|
||||
chars: {
|
||||
// PLX Continuous Measurement — 实时血氧+脉率
|
||||
notify: '00002A5F-0000-1000-8000-00805F9B34FB',
|
||||
// PLX Spot-Check Measurement — 单次测量
|
||||
read: '00002A5E-0000-1000-8000-00805F9B34FB',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// ── IEEE 11073 SFLOAT 解析(Bluetooth SIG 医疗 Profile 通用格式) ──
|
||||
|
||||
/** 特殊 SFLOAT 值 */
|
||||
const SFLOAT_NAN = 0x07FF;
|
||||
const SFLOAT_NRES = 0x0800;
|
||||
const SFLOAT_POS_INF = 0x07FE;
|
||||
const SFLOAT_NEG_INF = 0x0802;
|
||||
|
||||
function parseSFLOAT(view: DataView, offset: number): number | null {
|
||||
if (offset + 2 > view.byteLength) return null;
|
||||
const raw = view.getUint16(offset, true);
|
||||
|
||||
if (raw === SFLOAT_NAN || raw === SFLOAT_NRES) return null;
|
||||
if (raw === SFLOAT_POS_INF) return Infinity;
|
||||
if (raw === SFLOAT_NEG_INF) return -Infinity;
|
||||
|
||||
const signM = (raw >> 15) & 0x01;
|
||||
const exp = (raw >> 12) & 0x07;
|
||||
const mantissa = raw & 0x0FFF;
|
||||
|
||||
// 指数用 3 位补码表示(0-3 正,4-7 负)
|
||||
const exponent = exp >= 4 ? exp - 8 : exp;
|
||||
const signedMantissa = signM ? -(mantissa ^ 0x0FFF) - 1 : mantissa;
|
||||
|
||||
return signedMantissa * Math.pow(10, exponent);
|
||||
}
|
||||
|
||||
// ── 解析器 ──
|
||||
|
||||
function parseHeartRate(data: ArrayBuffer): NormalizedReading | null {
|
||||
@@ -66,6 +102,39 @@ function parseTemperature(data: ArrayBuffer): NormalizedReading | null {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 Pulse Oximeter Service 数据
|
||||
* PLX Continuous Measurement (0x2A5F) 和 Spot-Check (0x2A5E) 共用
|
||||
* 格式: Flags(1B) + SpO2(SFLOAT 2B) + PulseRate(SFLOAT 2B) + optional...
|
||||
*/
|
||||
function parsePulseOximeter(data: ArrayBuffer): NormalizedReading[] {
|
||||
const view = new DataView(data);
|
||||
if (view.byteLength < 5) return [];
|
||||
|
||||
const spO2 = parseSFLOAT(view, 1);
|
||||
const pulseRate = parseSFLOAT(view, 3);
|
||||
const now = new Date().toISOString();
|
||||
const results: NormalizedReading[] = [];
|
||||
|
||||
if (spO2 !== null && spO2 >= 0 && spO2 <= 100) {
|
||||
results.push({
|
||||
device_type: 'blood_oxygen',
|
||||
values: { blood_oxygen: Math.round(spO2), unit: '%' },
|
||||
measured_at: now,
|
||||
});
|
||||
}
|
||||
|
||||
if (pulseRate !== null && pulseRate > 0 && pulseRate <= 300) {
|
||||
results.push({
|
||||
device_type: 'heart_rate',
|
||||
values: { heart_rate: Math.round(pulseRate) },
|
||||
measured_at: now,
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// ── 工厂函数 ──
|
||||
|
||||
export interface GenericAdapterConfig {
|
||||
@@ -100,20 +169,23 @@ export function createGenericBleAdapter(config: GenericAdapterConfig): DeviceAda
|
||||
): NormalizedReading[] {
|
||||
const upper = charUUID.toUpperCase();
|
||||
|
||||
// Heart Rate Measurement
|
||||
const hrsChar = SERVICES.heart_rate.chars.notify.toUpperCase();
|
||||
if (upper === hrsChar || upper.includes('2A37')) {
|
||||
// Heart Rate Measurement (0x2A37)
|
||||
if (upper.includes('2A37')) {
|
||||
const result = parseHeartRate(data);
|
||||
return result ? [result] : [];
|
||||
}
|
||||
|
||||
// Temperature Measurement
|
||||
const htChar = SERVICES.health_thermometer.chars.notify.toUpperCase();
|
||||
if (upper === htChar || upper.includes('2A1C')) {
|
||||
// Temperature Measurement (0x2A1C)
|
||||
if (upper.includes('2A1C')) {
|
||||
const result = parseTemperature(data);
|
||||
return result ? [result] : [];
|
||||
}
|
||||
|
||||
// Pulse Oximeter Continuous (0x2A5F) / Spot-Check (0x2A5E)
|
||||
if (upper.includes('2A5F') || upper.includes('2A5E')) {
|
||||
return parsePulseOximeter(data);
|
||||
}
|
||||
|
||||
return [];
|
||||
},
|
||||
|
||||
@@ -155,7 +227,7 @@ export const HuaweiBandAdapter = createGenericBleAdapter({
|
||||
'华为手环',
|
||||
'华为手表',
|
||||
],
|
||||
profiles: ['heart_rate', 'health_thermometer'],
|
||||
profiles: ['heart_rate', 'health_thermometer', 'pulse_oximeter'],
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user