Multiple fixes for BLE subscriptions#142
Open
DennisMoschina wants to merge 8 commits into
Open
Conversation
BREAKING CHANGE: BleGattManager.subscribe and SensorHandler.subscribeToSensorData now return Future<Stream<...>> so callers can await notification readiness before issuing dependent writes.
Document the breaking BLE subscription API changes and the notification setup race fix in the unreleased changelog.
… allow retries on failure
…with notification support and fallback
|
Visit the preview URL for this PR (updated for commit 41904bf): https://open-earable-lib-web-example--pr142-subscriptions-fix-c1glxhf9.web.app (expires Wed, 19 Aug 2026 13:45:04 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: 7f6db4d9d00b851ad49af109fa949061904b3151 |
There was a problem hiding this comment.
Pull request overview
This PR hardens BLE notification subscriptions and sensor-stream setup across the library to reduce subscription races, connection duplication, and improve OpenEarable V2 sensor-scheme reading reliability (including a notification→read fallback path).
Changes:
- Make
BleGattManager.subscribeandSensorHandler.subscribeToSensorDataasync (Future<Stream<...>>) so callers can await notification readiness before listening. - Rework several device streams to set up subscriptions in
onListen, add closed-controller guards, and improve cancellation behavior. - Improve OpenEarable V2 sensor-scheme loading by keeping a single notification subscription during scheme reads and adding synchronous-read/legacy fallbacks.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/src/utils/sensor_scheme_parser/v2_sensor_scheme_reader.dart | Adds buffered notification matching + read fallback for V2 sensor schemes. |
| lib/src/models/devices/polar_factory.dart | Awaits BLE subscription setup and guards stream controller usage. |
| lib/src/models/devices/open_ring.dart | Awaits BLE subscription setup and adds error-handling guards for battery/time sync. |
| lib/src/models/devices/open_earable_v2.dart | Moves subscribe/read setup into async onListen with closed-controller checks. |
| lib/src/models/devices/open_earable_v1.dart | Adds closed-controller guards + error propagation for battery/sensor streams. |
| lib/src/models/devices/open_earable_factory.dart | Awaits sensor subscription setup and guards controller writes. |
| lib/src/models/devices/esense_factory.dart | Awaits sensor subscription setup and adds cancellation + error guards. |
| lib/src/models/devices/cosinuss_one.dart | Awaits BLE subscription setup and adds controller lifecycle guards. |
| lib/src/models/devices/battery_gatt_reader/battery_level_status_service_gatt_reader.dart | Refactors polling via helper + unawaited, adds closed-controller guard. |
| lib/src/models/devices/battery_gatt_reader/battery_level_status_gatt_reader.dart | Refactors polling via helper + unawaited, adds closed-controller guard. |
| lib/src/models/devices/battery_gatt_reader/battery_health_status_gatt_reader.dart | Refactors polling via helper + unawaited, adds closed-controller guard. |
| lib/src/models/devices/battery_gatt_reader/battery_energy_status_gatt_reader.dart | Refactors polling via helper + unawaited, adds closed-controller guard. |
| lib/src/models/capabilities/sensor_specializations/open_ring/open_ring_sensor.dart | Makes OpenRing sensor subscription setup async and idempotent. |
| lib/src/managers/v2_sensor_handler.dart | Makes sensor subscription async and awaits BLE notification readiness. |
| lib/src/managers/sensor_handler.dart | Updates interface to Future<Stream<...>> for subscription readiness. |
| lib/src/managers/open_ring_sensor_handler.dart | Makes shared OpenRing data stream creation async and awaits BLE subscription. |
| lib/src/managers/open_earable_sensor_manager.dart | Makes sensor subscription async and awaits BLE notification readiness. |
| lib/src/managers/esense_sensor_handler.dart | Makes sensor subscription async and awaits BLE notification readiness. |
| lib/src/managers/ble_manager.dart | Adds pending connection reuse + async subscribe with setup/teardown tracking. |
| lib/src/managers/ble_gatt_manager.dart | Updates subscribe API contract to Future<Stream<...>>. |
| lib/open_earable_flutter.dart | Deduplicates wearable connection attempts by caching in-flight futures. |
| CHANGELOG.md | Documents breaking API changes and summarizes the BLE subscription fixes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+570
to
+572
| bool _matchesSensorId(SensorScheme scheme, int requestedSensorId) { | ||
| return scheme.sensorId == requestedSensorId; | ||
| } |
Comment on lines
70
to
72
| if (!_bleManager.isConnected(deviceId)) { | ||
| Exception("Can't subscribe to sensor data. Earable not connected"); | ||
| } |
Comment on lines
+266
to
+272
| try { | ||
| UniversalBle.connect(device.id); | ||
| } catch (error, stack) { | ||
| if (!completer.isCompleted) { | ||
| completer.completeError(error, stack); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reduced data races, improved timeout for reading, reduced BLE overhead