|
| 1 | +// A Coros sports watch (Pace/Apex/Vertix series) as a [BandAdapter]: a |
| 2 | +// one-shot status pull (battery, model, serial, firmware) at connect, then |
| 3 | +// the same generic 0x2A37 heart-rate parse [BleHrsAdapter] already has. |
| 4 | +// |
| 5 | +// SCOPE IS DELIBERATELY NARROW. Every standard SIG service on this watch |
| 6 | +// answers a plain connect with no pairing or bonding enforced — battery, |
| 7 | +// device information and heart rate are all documented, unencrypted GATT. |
| 8 | +// Recorded activity, sleep and step history ride a completely undocumented |
| 9 | +// proprietary channel with no public frame spec anywhere; decoding it would |
| 10 | +// mean inventing a physiological data format from nothing, which is exactly |
| 11 | +// what this project refuses to do. That channel is never touched here. |
| 12 | +// |
| 13 | +// NOTHING HERE HAS MET HARDWARE. Nobody on this project owns one, so ships |
| 14 | +// EXPERIMENTAL (ASSUMPTIONS R6): `signals` covers only the generic HR parse, |
| 15 | +// and `coros` stays absent from `kDerivableSources` like every other band — |
| 16 | +// see `_registry.dart`'s `kCoros` doc on the one real unknown (the exact |
| 17 | +// advertised service UUID) that still needs a real device to close. |
| 18 | + |
| 19 | +import 'dart:convert'; |
| 20 | + |
| 21 | +import 'package:openstrap_protocol/openstrap_protocol.dart'; |
| 22 | + |
| 23 | +import '_registry.dart'; |
| 24 | +import 'adapter.dart'; |
| 25 | +import 'signals.dart'; |
| 26 | + |
| 27 | +/// The adapter. Const, and it holds no session state — everything a session |
| 28 | +/// needs lives inside [run]. |
| 29 | +class CorosAdapter extends BandAdapter { |
| 30 | + const CorosAdapter(); |
| 31 | + |
| 32 | + @override |
| 33 | + BandEntry get entry => kCoros; |
| 34 | + |
| 35 | + /// The generic HR parse only. Nothing else on this watch is decoded — |
| 36 | + /// battery and device identity are [BandNote]s, not a physiological signal. |
| 37 | + @override |
| 38 | + Map<InputSignal, Duration> get signals => const { |
| 39 | + InputSignal.hrSparse: Duration(seconds: 1), |
| 40 | + InputSignal.rrIntervals: Duration(seconds: 1), |
| 41 | + }; |
| 42 | + |
| 43 | + @override |
| 44 | + Stream<BandEvent> run(BandLink link) async* { |
| 45 | + // One-shot, best-effort: a watch that answers battery and identity but |
| 46 | + // not heart rate (or the reverse) still gets whatever it does answer — |
| 47 | + // see `kCoros`'s own doc on why none of this is required to connect. |
| 48 | + final battery = await link.read(kBatteryLevelUuid); |
| 49 | + if (battery != null && battery.isNotEmpty) { |
| 50 | + yield BandNote('battery', battery[0]); |
| 51 | + } |
| 52 | + final model = await _readString(link, kModelNumberUuid); |
| 53 | + if (model != null) yield BandNote('model', model); |
| 54 | + final serial = await _readString(link, kSerialNumberUuid); |
| 55 | + if (serial != null) yield BandNote('serial', serial); |
| 56 | + final firmware = await _readString(link, kFirmwareRevisionUuid); |
| 57 | + if (firmware != null) yield BandNote('firmware', firmware); |
| 58 | + |
| 59 | + // No handshake for HR itself — same floor as `BleHrsAdapter`: one |
| 60 | + // subscription, no clock, no INIT. |
| 61 | + await for (final (atSec, value) in link.notify(kHeartRateMeasurementUuid)) { |
| 62 | + final s = parseHeartRateMeasurement(value); |
| 63 | + if (s == null) continue; |
| 64 | + // The sensor's own "no skin contact" is a REFUSAL, not a low reading — |
| 65 | + // see `ble_hrs.dart`'s identical guard. |
| 66 | + if (s.contact == false) continue; |
| 67 | + yield SampleBatch( |
| 68 | + [ |
| 69 | + NeutralSample( |
| 70 | + // ARRIVAL TIME, NOT BEAT TIME — see `ble_hrs.dart`'s doc on why. |
| 71 | + anchor: TimeAnchor.arrival, |
| 72 | + tsEpoch: atSec, |
| 73 | + hr: s.hr, |
| 74 | + rrMs: s.rrMs, |
| 75 | + vendor: s.contact == null ? const {} : {'contact': s.contact}, |
| 76 | + ), |
| 77 | + ], |
| 78 | + ephemeral: false, |
| 79 | + ); |
| 80 | + } |
| 81 | + // No OffloadCheckpoint, ever. This watch's recorded history is never |
| 82 | + // requested — see the header note — so there is nothing to tell it to |
| 83 | + // forget. |
| 84 | + } |
| 85 | + |
| 86 | + /// A read-only Device Information string, or null for a missing |
| 87 | + /// characteristic, an empty reply or one that decodes to nothing. |
| 88 | + Future<String?> _readString(BandLink link, String uuid) async { |
| 89 | + final bytes = await link.read(uuid); |
| 90 | + if (bytes == null || bytes.isEmpty) return null; |
| 91 | + final s = utf8.decode(bytes, allowMalformed: true).trim(); |
| 92 | + return s.isEmpty ? null : s; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// The single instance. Const, so it costs nothing to reference. |
| 97 | +const CorosAdapter kCorosAdapter = CorosAdapter(); |
0 commit comments