Skip to content

fix(bee): support 3-byte millivolts battery format and clamp legacy percentage (#21749) - #21768

Merged
kodjima33 merged 3 commits into
BasedHardware:mainfrom
hardik4love-lang:fix/bee-voltage-battery-parser-21749
Oct 16, 2026
Merged

kodjima33 merged 3 commits into
BasedHardware:mainfrom
hardik4love-lang:fix/bee-voltage-battery-parser-21749

Conversation

@hardik4love-lang

@hardik4love-lang hardik4love-lang commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Fixes #21749 by adding support for newer Bee firmware (e.g. 0.6.20+) 3-byte little-endian millivolts battery responses in BeeDeviceConnection.parseBatteryResponse, while preserving backwards compatibility with legacy 2-byte percentage responses and clamping the legacy level to 0..100.

Product invariants affected

  • INV-CAP-1

How it was verified

Verified via hermetic Dart unit tests covering both 2-byte percentage and 3-byte millivolts formats, boundary conditions (3400 mV and 4200 mV), charging/discharging curves, and out-of-range rejection (<3000 mV or >4500 mV).

Tests

�pp/test/unit/bee_connection_battery_test.dart

UI evidence

UI-Evidence: none -- Non-UI BLE protocol parser logic verified via hermetic unit tests.

Failure class (fixes)

Failure-Class: none

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the excellent root-cause analysis on #21749 — the format detection, clamping, and plausibility gate are the right shape for this fix, and the hermetic test suite is genuinely useful. I verified the interpolation independently: 4059 mV maps to 77% (discharging) / 62% (charging) exactly as your tests expect, and both calibration tables are strictly monotonic. However, a few things need fixing before this can merge.

app/lib/services/devices/connectors/bee_connection.dart

  • The payload-length branching in parseBatteryResponse is clean: 2-byte legacy path now clamps level to 0..100 (previously unclamped), and the 3-byte path decodes little-endian millivolts (payload[0] | (payload[1] << 8)), rejects readings outside 3000–4500 mV, and reads the charging state from payload[2]. This matches the payload evidence in the issue.
  • One maintainer note (non-blocking): the _dischargeStates/_chargingStates tables are a generic LiPo curve rather than officially sourced Bee firmware calibration — the issue author measured ~76% at 4059 mV vs 77% here. Close, but hardware-side confirmation of the curve would strengthen confidence.

app/test/unit/bee_connection_battery_test.dart

  • Good coverage: null on short payloads, both formats, clamping, boundary voltages (4200/3400 mV), and out-of-range rejection.
  • Blocker: the import of package:omi/services/devices/models.dart is unused (DeviceType and BtDevice come from the already-imported bt_device.dart). This trips the analyzer ratchet — unused_import: 1 found, baseline 0 — and fails Dart Analyze & Tests. Please remove it.

CI failures to address (all contributor-fixable):

  1. Dart Analyze & Tests — the unused import above. Two other lint counts actually improved (curly_braces_in_flow_control_structures 6→5, dangling_library_doc_comments 1→0); the ratchet output suggests running app/scripts/analyze_ratchet.sh --update-baseline in this PR to lock those in.
  2. Hygiene (PR Metadata Preflight) — the PR body is missing the required declarations the preflight names: an explicit UI-Evidence: none, the Product invariants affected section, and a validated failure-class declaration per the failure-class protocol. The preflight log lists exactly what to add and how.
  3. Mobile Release Eligibility — fails downstream of the above; it should clear once 1 and 2 are fixed.

No security, privacy, dependency, or workflow surfaces touched — this is a well-contained parser fix. Once the mechanical items above are resolved I'd expect this to move quickly.


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 the flutter flutter work label Oct 15, 2026
@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Thanks @Git-on-my-level! Both items addressed:\n1. Removed the unused models.dart import from �pp/test/unit/bee_connection_battery_test.dart to unblock the Dart analyzer ratchet.\n2. Updated the PR body metadata with all required preflight declarations (UI-Evidence: none, Product invariants affected: none, and Failure-Class: none).

@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Hi @Git-on-my-level,

All CI checks and preflight contracts are now 100% green:

  • Dart Analyze & Tests: PASS (19m42s, hermetic unit tests passing, zero analyzer warnings/unused imports)
  • PR Metadata Preflight: PASS (declared - INV-CAP-1 for �ee_connection.dart)
  • Mobile Release Eligibility & Merge Gate: PASS

Ready for review and merge! Thank you.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #21749 (Bee 3-byte millivolt battery format); confirmed main has no millivolt handling. Scoped diff, tests added, CI green on rerun - 5/5, merges.

@kodjima33
kodjima33 merged commit dd78fed into BasedHardware:main Oct 16, 2026
41 of 43 checks passed
@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Thank you @kodjima33, @Git-on-my-level, and @mdmohsin7 for reviewing and merging! Glad to have the Bee battery parser accurate and stable.

Bounty Claim Details

@kodjima33 @josancamon19

@hardik4love-lang

Copy link
Copy Markdown
Contributor Author

Hi @kodjima33, @josancamon19,

Following up on bounty settlement for merged PR #21768 (Issue #21749):

Whenever the batch disbursement runs, please confirm. Thank you!

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

Labels

flutter flutter work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants