Skip to content

fix(app): decode Bee voltage-format battery replies - #21752

Open
jms830 wants to merge 3 commits into
BasedHardware:mainfrom
jms830:fix/bee-voltage-battery
Open

jms830 wants to merge 3 commits into
BasedHardware:mainfrom
jms830:fix/bee-voltage-battery

Conversation

@jms830

@jms830 jms830 commented Oct 15, 2026 •

Copy link
Copy Markdown

What changed and why

Fixes #21749.

Decode three-byte Bee C00F battery replies as little-endian millivolts plus charging state, instead of treating the low voltage byte as a percentage. Preserve the older two-byte percentage/charging format. Use the Bee Frontier voltage curve with linear interpolation/rounding; reject unsupported lengths and voltages outside 2500–5000 mV. Keep parsing in the existing connector: no new dependencies, generic battery clamps, UI changes, audio hooks or protocol provisioning.

The captured db 0f 00 payload currently yields 219% and charging=true; the fix yields a 76% estimate and charging=false. The low-byte-zero case (4096 mV → 80%) is covered too, so this is not a heuristic based on values exceeding 100.

Product invariants affected

  • INV-CAP-1: connector path is covered, but capture admission, subscriptions, mute, audio delivery and storage behavior are unchanged.

How it was verified

  • git diff --check passed.
  • scripts/pr-preflight --suggest and scripts/pr-preflight --metadata-only --pr-body-file ... passed after materializing the referenced guard files. INV-CAP-1 is cited. The history-dependent failure-class ratchet explicitly skips on the shallow checkout.
  • Full make preflight was attempted, not passed: the manifest harness's platform test rejects native Android ('android' not found in {'macos', 'linux', 'windows'}), and the sparse checkout initially lacked unrelated inputs. Upstream full CI still needs maintainer approval for this fork contribution.
  • Focused Flutter CI passed: fork verification run 38784049460, Flutter 3.44.5 on Ubuntu. It exercised the actual production parser, not a translated test model:
    • Red: original upstream source returned {'level': 219, 'is_charging': true} against expected {'level': 76, 'is_charging': false}.
    • Green: all 9 regression tests passed with the fix.
    • dart analyze on the parser and test: No issues found.
    • PR metadata validation passed on a full checkout (history-dependent ratchet explicitly skipped due to shallow history).
    • Formatter output from that run is applied verbatim; the final parser/test bytes are the same ones exercised in the passing run.
  • Flutter is unavailable on the native ARM64 Android/Termux authoring environment; the tests above ran on GitHub, not this phone. The single-job verification workflow lives only on a separate fork branch and is not included in this PR. Full upstream CI still awaits maintainer approval.
  • Supplementary prior device evidence: the exact payload was captured using a patched/rooted Android connection on Omi 1.0.558/1400 with cached Bee firmware 0.6.20. A separate local compatibility patch subsequently displayed 77%, preserved checked settings and resumed audio forwarding. This is not execution evidence for this Dart PR or a stock connection, nor the same instantaneous voltage sample.

Tests

app/test/unit/bee_battery_response_test.dart directly exercises the production BeeDeviceConnection parser with a transport that throws on unexpected I/O:

  • captured response envelope → 76%, not charging;
  • legacy percentage/charging compatibility;
  • low voltage byte within the percentage range;
  • voltage-format charging states;
  • calibration anchors and interpolated values;
  • endpoint saturation and invalid voltage/length rejection;
  • bounded, monotonic output across every accepted integer millivolt value.

Focused command: cd app && flutter test test/unit/bee_battery_response_test.dart.

Calibration provenance and limits

The calibration points and voltage-format charging semantics were identified by inspecting the Bee Android client's battery calculator during diagnosis; this patch independently implements the numerical mapping, without copying proprietary source or attaching disassembly/APKs. The observed packet is from one device. Generic cached model labels do not independently establish hardware generation; maintainer review of calibration applicability is requested. Voltage-to-percentage is an estimate, not measured remaining capacity. This PR does not reproduce the Bee client's history-dependent discharge filter.

The analogous macOS parser is noted in the issue but deliberately left for separate work; no Mac runtime claim. Encrypted-audio support and #20828 are also out of scope.

UI evidence

UI-Evidence: none -- protocol parser and hermetic test change only; no widget/layout changes. Sanitized packet reproduction is in #21749.

Failure class (fixes)

Failure-Class: none

The advisory classes selected by scope concern async errors, timezone assumptions, analyzer gates, teardown and synthetic telemetry; none describes this real battery wire-format mismatch. No registry or new CI guard is added.

@jms830
jms830 marked this pull request as ready for review October 15, 2026 18:21
@jms830
jms830 requested a review from mdmohsin7 as a code owner October 15, 2026 18:21
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks for this well-scoped fix — the reproduction in #21749 (payload db 0f 00 decoding to 219% / charging) is clear, and the patch stays inside the connector with no new dependencies.

Review notes:

  • app/lib/services/devices/connectors/bee_connection.dart — the length-based dispatch preserves the legacy two-byte [percentage, charging] path exactly (payload[1] != 0), and the three-byte branch reads little-endian millivolts and interpolates the calibration curve correctly. I verified the math against your test vectors: 4059 mV → 76%, 4096 mV → 80%, 3400 mV → 2%, with correct endpoint clamping at 3300/4350 mV, and returning null for out-of-range voltages and unsupported lengths is the right defensive behavior.
  • One behavioral question: the legacy format treats any nonzero second byte as charging, but the voltage format treats only state 1 as charging (payload[2] == 1). If the Frontier firmware ever reports another nonzero charging state, this would show "not charging". Is state 1 the only charging value per the protocol docs, or should this be != 0 for symmetry with the legacy path?
  • app/test/unit/bee_battery_response_test.dart — good coverage: the captured packet end-to-end through parseResponse, legacy semantics, format selection by length rather than value heuristics (the 0x00 0x10 low-byte case), all curve points, interpolation/rounding, endpoint clamping, rejection cases, and a bounded monotonic sweep across 2500–5000 mV. The _NoIoTransport noSuchMethod guard failing on unexpected transport access is a nice touch.
  • app/changelog/unreleased/20261015-bee-battery-voltage.json — matches the existing unreleased-fragment format.

The one thing code review can't verify is the provenance of the 11-point voltage-to-percentage calibration curve — whether it comes from official Bee Frontier specs and which hardware/firmware generations it applies to. Your issue notes hardware-generation applicability beyond the tested device needs confirmation; since this curve determines what users see as their battery level, that source should be confirmed by a maintainer familiar with the Bee hardware before merge.

Leaving for human maintainer review to confirm the Frontier calibration curve's source and hardware-generation applicability.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added needs-maintainer-review Needs a human maintainer to sign off before merge flutter flutter work labels Oct 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work needs-maintainer-review Needs a human maintainer to sign off before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(bee): voltage-format battery replies are interpreted as percentages

2 participants