:beacon · the sensor hub
The service, and the sensors behind it
One foreground service, one 250 ms tick loop, a watchdog that is not the tick loop, and a foreground-service-type matrix computed by a file with zero Android imports so the whole thing can be unit-tested.
Shape
TelemetryService (foreground, START_STICKY, partial wake lock)
│
TelemetryBroadcaster (object; SensorEventListener)
├── LocationManager NMEA listener ──► forwarded VERBATIM
├── LocationManager fix updates ──► TripOdometer, speed for $ZTLM
├── TYPE_ROTATION_VECTOR ──► $GPHDT, $ZTLM pitch/roll
├── TYPE_LINEAR_ACCELERATION ──► ShockDetector ──► $ZSHK
├── TYPE_LIGHT ──► $ZENV
├── AudioRecord 16 kHz ──► AudioLevels ──► $ZAUD
└── ACTION_BATTERY_CHANGED ──► $ZBCN
│
TickLoop (250 ms) ──► MulticastSocket ──► 239.7.7.10:10110 + subnet broadcast
Cadence
One 250 ms tick loop drives everything periodic, with integer
divisors so nothing needs its own timer: $GPHDT and $ZTLM every tick
(~4 Hz), $ZENV and $ZODO every 8 ticks
(~2 s), $ZBCN and the odometer persist every 20 ticks
(~5 s), the on-screen status every 4 ticks. $ZAUD is
driven by audio frames instead, and $ZSHK is event-driven.
There is no rate limiting or change detection beyond that. A value that has not changed is re-broadcast identically, which is what makes the stream self-healing for a tablet that just joined.
The watchdog is not the tick loop
One exception once killed every synthesized channel, silently, while GNSS passthrough kept flowing and the phone still said “Broadcasting”. The loop must not be the only thing able to report the loop's death.
Now: an extracted TickLoop catches per-tick exceptions and continues (rethrowing
CancellationException, letting Error propagate to a process restart),
plus a second coroutine that checks the tick clock every
2.5 s and, if the loop has been dead for more than
5 s, prepends a banner to the status readout naming exactly which
channels stopped.
Honest limit: a Doze stall freezes the watchdog too, so the banner appears on wake.
Surviving a week unattended
| Mechanism | Covers |
|---|---|
Foreground service, START_STICKY | Ordinary process death |
PARTIAL_WAKE_LOCK for the service lifetime, released after the odometer persist | CPU sleep between ticks |
| One-time battery-optimization exemption prompt | Deep Doze — stationary, screen off, unplugged (the flaky-charger case), where the platform ignores app wake locks entirely. Both halves are required. |
Boot receiver for BOOT_COMPLETED + MY_PACKAGE_REPLACED | Reboot, and a sideloaded update (which also kills the service) |
| Auto-start flag written on both toggle branches | Boot restores the operator's last expressed intent rather than starting unconditionally, which would make the STOP button a lie |
Foreground-service types
Computed by ForegroundTypes, a file with zero Android imports —
it mirrors the framework constants as literals so the whole matrix is unit-testable:
- Location from API 34, gated on permission
- Microphone only with the permission and not from a background start — a
START_STICKYrestart is a background start by definition, and the mic type on Android 14+ throws when denied - Floored to
specialUse, so the result is never zero
The API 34/35 branches are argued from documentation, not proven on hardware. Lint cannot validate them, and the only beacon device on hand is API 29. One boot test on an API 34+ phone is owed before the event.
Sensor detail worth knowing
| Component | Behaviour |
|---|---|
AudioLevels | RMS and peak normalised to 0..1; the beat flag compares frame energy against a decaying running average. The first frame never beats (no baseline) and sustained loudness stops beating as the average catches up. |
ShockDetector | Opens a window when acceleration crosses 1.5 g, tracks the peak for 120 ms, and reports the peak of the impulse rather than its onset. The 500 ms refractory period starts when the window closes. |
TripOdometer | Haversine accumulation with three fail-closed gates. A rejected step holds the anchor, so recovery is automatic. Gates → |
| Absent sensors | hdtSentenceOrNull / zenvSentenceOrNull return null rather than a fabricated 0.0, and the status readout shows --. |
Sentence building
Sentence builders and the XOR checksum are in Nmea.kt. All numeric formatting is
pinned to Locale.US, because a comma-decimal locale would emit 12,3 and
split the field — taking the whole fleet's telemetry down.
Nmea.checksum was validated against itself: mutating xor to
or passed all 35 beacon tests. It is now validated against published NMEA
0183 vectors. This is a recurring shape in this project and worth watching for.
Tests
beacon/src/test/…/{Nmea,AudioLevels,ShockDetector,TripOdometer}Test.kt
and others — 26 classes, 180 tests. Ten
suites use Robolectric; because TelemetryBroadcaster is a process singleton, the suites
that start it stop it and clear the injected GPS handle in @After.
Every step in android-ci.yml was :app:-scoped, so the beacon's unit
tests (109 at the time), ktlint, detekt, lint and assemble had never run on a push — on
the one module that must survive a reboot unattended for a week. All five gates are now unscoped.
Run them unscoped locally too: an :app:-prefixed task silently skips the beacon,
which is exactly how the gap survived.
Continuing the BeaconNet extraction out of the ~970-line
TelemetryBroadcaster object is recorded as open work, alongside a
SystemLocationSource clock, NetworkLocationSource.ingest() and a
BLE/USB SppHandle on the app side.
Converted fromARCHITECTURE.md §5 · docs/BUILD.md §3, §5