Skip to content

Multiple fixes for BLE subscriptions#142

Open
DennisMoschina wants to merge 8 commits into
mainfrom
subscriptions-fix
Open

Multiple fixes for BLE subscriptions#142
DennisMoschina wants to merge 8 commits into
mainfrom
subscriptions-fix

Conversation

@DennisMoschina

Copy link
Copy Markdown
Collaborator

Reduced data races, improved timeout for reading, reduced BLE overhead

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.
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.subscribe and SensorHandler.subscribeToSensorData async (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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants