GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SimpleSOAP/src/ble/adapters.ts at main · jfweleber/SimpleSOAP Expand file tree/ Copy pathadapters.tsMore file actions 412 lines (366 loc) · 14.2 KB / Copy pathadapters.tsFile metadata and controls412 lines (366 loc) · 14.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 /** * Adapter registry for BLE vitals monitors. * * Two adapters implement published Bluetooth SIG profiles. The other five * implement vendor framing carried over a serial-style characteristic, derived * from protocol analysis; each is marked `verified: false` until it has been * confirmed against real hardware. * * Adding a device means adding one entry here. Nothing else needs to change. */ import type { Adapter, Measurement, ScannedDevice } from './types' import { emptyMeasurement } from './types' import { CHOICEMMED_ADVERT, FFE0_NOTIFY, FFE0_SERVICE, FFF0_NOTIFY, FFF0_SERVICE, HEART_RATE_MEASUREMENT, HEART_RATE_SERVICE, ISSC_NOTIFY, ISSC_SERVICE, JUMPER_NOTIFY, JUMPER_SERVICE, NUS_NOTIFY, NUS_SERVICE, PLX_CONTINUOUS_MEASUREMENT, PLX_SERVICE, VIATOM_NOTIFY, VIATOM_SERVICE, } from './uuid' const advertises = (d: ScannedDevice, uuid: string) => d.serviceUUIDs?.includes(uuid) ?? false const namedLike = (d: ScannedDevice, patterns: RegExp[]) => !!d.name && patterns.some((p) => p.test(d.name as string)) /** A reading where the probe is on but sees nothing — distinct from a bad frame. */ function noContact(): Measurement { return { ...emptyMeasurement(), sensorContact: false } } // --------------------------------------------------------------------------- // Standard GATT — Heart Rate Service 0x180D // --------------------------------------------------------------------------- /** * Heart Rate Measurement, characteristic 0x2A37. * * Flags byte drives a variable layout: * bit 0 heart rate is uint16 (else uint8) * bit 1 sensor contact detected * bit 2 sensor contact supported — bit 1 is meaningless without this * bit 3 energy expended field present (uint16) * bit 4 RR interval fields present (uint16 each, units of 1/1024 s) */ export const heartRateAdapter: Adapter = { id: 'ble_hr', name: 'Heart Rate Monitor', kind: 'heartRate', provides: ['heartRate', 'rrIntervals'], scanServiceUUIDs: [HEART_RATE_SERVICE], verified: false, matches: (d) => advertises(d, HEART_RATE_SERVICE), target: () => ({ service: HEART_RATE_SERVICE, characteristic: HEART_RATE_MEASUREMENT }), parse(view) { if (view.byteLength < 2) return null const flags = view.getUint8(0) const wide = (flags & 0x01) !== 0 const contactSupported = (flags & 0x04) !== 0 const contactDetected = (flags & 0x02) !== 0 const hasEnergy = (flags & 0x08) !== 0 const hasRR = (flags & 0x10) !== 0 let offset = 1 let heartRate: number if (wide) { if (view.byteLength < offset + 2) return null heartRate = view.getUint16(offset, true) offset += 2 } else { heartRate = view.getUint8(offset) offset += 1 } if (hasEnergy) offset += 2 const rrIntervals: number[] = [] if (hasRR) { while (offset + 1 < view.byteLength) { // transmitted in 1/1024 s units rrIntervals.push((view.getUint16(offset, true) / 1024) * 1000) offset += 2 } } return { ...emptyMeasurement(), heartRate, rrIntervals, sensorContact: contactSupported ? contactDetected : null, } }, } // --------------------------------------------------------------------------- // Standard GATT — Pulse Oximeter Service 0x1822 // --------------------------------------------------------------------------- /** * Decode an IEEE-11073 16-bit SFLOAT. * * Layout is a signed 4-bit exponent in the high nibble over a signed 12-bit * mantissa. Five mantissa values are reserved for non-numeric states * (+INFINITY, NaN, NRes, reserved, -INFINITY) and mean "no usable reading". */ function decodeSFloat(raw: number): number | null { const mantissa = raw & 0x0fff if (mantissa >= 0x07fe && mantissa <= 0x0802) return null const rawExponent = (raw >> 12) & 0x0f const exponent = rawExponent >= 0x08 ? rawExponent - 0x10 : rawExponent const value = mantissa >= 0x0800 ? mantissa - 0x1000 : mantissa return value * Math.pow(10, exponent) } /** PLX Continuous Measurement, characteristic 0x2A5F. */ export const plxAdapter: Adapter = { id: 'ble_plx', name: 'Pulse Oximeter (standard)', kind: 'pulseOximeter', provides: ['spo2', 'heartRate'], scanServiceUUIDs: [PLX_SERVICE], verified: false, matches: (d) => advertises(d, PLX_SERVICE), target: () => ({ service: PLX_SERVICE, characteristic: PLX_CONTINUOUS_MEASUREMENT }), parse(view) { // flags @0, then SpO2 and pulse rate as consecutive SFLOATs if (view.byteLength < 5) return null const spo2 = decodeSFloat(view.getUint16(1, true)) const pulse = decodeSFloat(view.getUint16(3, true)) if (spo2 === null || pulse === null) return noContact() return { ...emptyMeasurement(), spo2, heartRate: pulse, sensorContact: true } }, } // --------------------------------------------------------------------------- // Vendor — BCI protocol over Microchip/ISSC transparent UART (BerryMed) // --------------------------------------------------------------------------- const BERRYMED_NAMES = [/^BerryMed/i, /^BM\d/i] /** * A BCI frame is five bytes, and only the first has bit 7 set. That bit is * the frame boundary: the transparent UART carries a byte stream, so one * notification holds several frames and starts wherever the last one ended. * * 0 1sss ssss sync; bits 0–3 signal strength * 1 0ppp pppp pleth (0–100) * 2 0Rfs bbbb R = pulse rate bit 7, f = no finger, s = searching, bar graph * 3 0rrr rrrr pulse rate bits 0–6 * 4 0ooo oooo SpO₂, 127 when there is no reading * * The first version of this read offsets 3 and 4 of the notification, which * is right only when a frame happens to start at byte 0. Against a BM1000C * it mostly did not, and the screen showed an SpO₂ of 133 — a sync byte, * read as a number. Perfusion index is not in this frame; the manufacturer's * app shows one, so the device carries it somewhere else, still to be found. */ const BCI_FRAME = 5 function bciFrameAt(view: DataView, i: number): boolean { if (i + BCI_FRAME > view.byteLength) return false if ((view.getUint8(i) & 0x80) === 0) return false for (let k = 1; k < BCI_FRAME; k++) if (view.getUint8(i + k) & 0x80) return false return true } export const berryMedAdapter: Adapter = { id: 'ble_bci', name: 'BerryMed / BCI', kind: 'pulseOximeter', provides: ['spo2', 'heartRate'], scanServiceUUIDs: [ISSC_SERVICE], // BM1000C-O, 2026-09-06: pulse and SpO₂ tracked the unit's own display // once the parser found frame boundaries. Perfusion index is not carried // in this frame and stays unprovided until its source is known. verified: true, matches: (d) => advertises(d, ISSC_SERVICE) || namedLike(d, BERRYMED_NAMES), target: () => ({ service: ISSC_SERVICE, characteristic: ISSC_NOTIFY }), parse(view) { // the newest complete frame in the notification is the reading let start = -1 for (let i = view.byteLength - BCI_FRAME; i >= 0; i--) { if (bciFrameAt(view, i)) { start = i break } } if (start < 0) return null const status = view.getUint8(start + 2) const noFinger = (status & 0x10) !== 0 const pulse = ((status & 0x40) << 1) | (view.getUint8(start + 3) & 0x7f) const spo2 = view.getUint8(start + 4) & 0x7f if (noFinger || spo2 === 127 || spo2 === 0 || pulse === 0 || pulse === 255) return noContact() if (spo2 > 100) return null return { ...emptyMeasurement(), heartRate: pulse, spo2, sensorContact: true } }, } // --------------------------------------------------------------------------- // Vendor — ChoiceMMed, over Nordic UART // --------------------------------------------------------------------------- const CHOICEMMED_NAMES = [/^iP\d+/i] export const choiceMMedAdapter: Adapter = { id: 'ble_choicemmed', name: 'ChoiceMMed', kind: 'pulseOximeter', provides: ['spo2', 'heartRate', 'respiratoryRate', 'perfusionIndex'], scanServiceUUIDs: [NUS_SERVICE], verified: false, matches: (d) => advertises(d, CHOICEMMED_ADVERT) || namedLike(d, CHOICEMMED_NAMES), target: () => ({ service: NUS_SERVICE, characteristic: FFF0_NOTIFY }), // will not start streaming until this second characteristic is also subscribed extraSubscriptions: () => [{ service: NUS_SERVICE, characteristic: FFF0_SERVICE }], parse(view) { // 13-byte frame, 0x3E leader and 0xF0 terminator if (view.byteLength !== 13) return null if (view.getUint8(0) !== 0x3e) return null if (view.getUint8(12) !== 0xf0) { // idle frame the device sends with the probe empty const idle = view.getUint8(8) === 0x20 && view.getUint8(1) === 0 && view.getUint8(3) === 0 return idle ? noContact() : null } const spo2 = view.getUint8(1) const pulse = view.getUint8(3) if (spo2 === 0 || pulse === 0) return noContact() const pi = view.getUint8(11) return { ...emptyMeasurement(), spo2, heartRate: pulse, respiratoryRate: view.getUint8(5), perfusionIndex: pi > 0 ? pi / 10 : null, sensorContact: true, } }, } // --------------------------------------------------------------------------- // Vendor — generic FFE0 serial bridge (HM-10 class modules) // --------------------------------------------------------------------------- const FFE0_NAMES = [/^HealthTree/i, /^OXIMETER/i] export const ffe0Adapter: Adapter = { id: 'ble_ffe0', name: 'Generic Oximeter (FFE0)', kind: 'pulseOximeter', provides: ['spo2', 'heartRate', 'perfusionIndex'], scanServiceUUIDs: [FFE0_SERVICE], verified: false, matches: (d) => advertises(d, FFE0_SERVICE) || namedLike(d, FFE0_NAMES), target: () => ({ service: FFE0_SERVICE, characteristic: FFE0_NOTIFY }), parse(view) { // 20-byte frame behind an 0xFF 0x44 signature if (view.byteLength !== 20) return null if (view.getUint8(0) !== 0xff || view.getUint8(1) !== 0x44) return null const spo2 = view.getUint8(4) const pulse = view.getUint8(5) // 0xFF is the no-data sentinel; anything over 100% is a bad frame if (spo2 === 0 || spo2 === 0xff || pulse === 0 || pulse === 0xff || spo2 > 100) { return noContact() } // perfusion index spans two bytes, 7 significant bits each const pi = (view.getUint8(6) & 0x7f) | ((view.getUint8(7) & 0x7f) << 7) return { ...emptyMeasurement(), spo2, heartRate: pulse, perfusionIndex: pi > 0 ? pi / 100 : null, sensorContact: true, } }, } // --------------------------------------------------------------------------- // Vendor — Jumper // --------------------------------------------------------------------------- export const jumperAdapter: Adapter = { id: 'ble_jumper', name: 'Jumper', kind: 'pulseOximeter', provides: ['spo2', 'heartRate', 'perfusionIndex'], scanServiceUUIDs: [JUMPER_SERVICE], verified: false, matches: (d) => advertises(d, JUMPER_SERVICE), target: () => ({ service: JUMPER_SERVICE, characteristic: JUMPER_NOTIFY }), parse(view) { // 4-byte frame behind an 0x81 leader if (view.byteLength < 4) return null if (view.getUint8(0) !== 0x81) return null const pulse = view.getUint8(1) const spo2 = view.getUint8(2) if (pulse === 0 || spo2 === 0) return noContact() const pi = view.getUint8(3) return { ...emptyMeasurement(), heartRate: pulse, spo2, perfusionIndex: pi > 0 ? pi / 10 : null, sensorContact: true, } }, } // --------------------------------------------------------------------------- // Vendor — Viatom / Wellue // --------------------------------------------------------------------------- /** * CRC-8, polynomial 0x8C (reflected 0x31), zero seed. * Covers every byte of the frame except the checksum itself. */ function crc8(bytes: Uint8Array, length: number): number { let crc = 0 for (let i = 0; i < length; i++) { crc ^= bytes[i] for (let bit = 0; bit < 8; bit++) { crc = crc & 0x01 ? (crc >> 1) ^ 0x8c : crc >> 1 } } return crc } export const viatomAdapter: Adapter = { id: 'ble_viatom', name: 'Viatom / Wellue', kind: 'pulseOximeter', provides: ['spo2', 'heartRate'], scanServiceUUIDs: [VIATOM_SERVICE, NUS_SERVICE], verified: false, matches: (d) => advertises(d, VIATOM_SERVICE) || advertises(d, NUS_SERVICE), // newer units use the Viatom service; older ones fall back to Nordic UART target: (d) => advertises(d, VIATOM_SERVICE) ? { service: VIATOM_SERVICE, characteristic: VIATOM_NOTIFY } : { service: NUS_SERVICE, characteristic: NUS_NOTIFY }, parse(view) { // 0xAA 0x55 leader, uint16 length @3, CRC-8 in the final byte if (view.byteLength < 8) return null if (view.getUint8(0) !== 0xaa || view.getUint8(1) !== 0x55) return null const frameLength = 5 + view.getUint16(3, true) if (view.byteLength < frameLength) return null const bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength) if (crc8(bytes, frameLength - 1) !== view.getUint8(frameLength - 1)) return null // packet type 1 is the vitals frame; others are battery, config, etc. if (view.getUint8(5) !== 0x01) return null const spo2 = view.getUint8(6) const pulse = view.getUint8(7) if (spo2 === 0 || pulse === 0) return noContact() return { ...emptyMeasurement(), spo2, heartRate: pulse, sensorContact: true } }, } // --------------------------------------------------------------------------- // Registry // --------------------------------------------------------------------------- /** * Order matters. Standard profiles are tried first so a spec-compliant device * is never claimed by a vendor adapter with a looser match — Viatom in * particular matches on bare Nordic UART, which many unrelated devices expose. */ export const adapters: Adapter[] = [ heartRateAdapter, plxAdapter, berryMedAdapter, jumperAdapter, ffe0Adapter, choiceMMedAdapter, viatomAdapter, ] /** Every service UUID worth scanning for, deduplicated. */ export function scanServiceUUIDs(): string[] { return [...new Set(adapters.flatMap((a) => a.scanServiceUUIDs))] } /** First adapter that claims the device, or null if nothing recognizes it. */ export function resolveAdapter(device: ScannedDevice): Adapter | null { return adapters.find((a) => a.matches(device)) ?? null } export function adapterById(id: string): Adapter | null { return adapters.find((a) => a.id === id) ?? null } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |