Handle BLE hardware errors and improve recovery process (fixes #245)#246
Open
Abasz wants to merge 1 commit into0.9.8-In-Developmentfrom
Open
Handle BLE hardware errors and improve recovery process (fixes #245)#246Abasz wants to merge 1 commit into0.9.8-In-Developmentfrom
Abasz wants to merge 1 commit into0.9.8-In-Developmentfrom
Conversation
This commit fixes issue #245 by registering a manager 'error' listener immediately after BleManager creation so hardware errors are caught at the ORM wrapper level instead of crashing the process as an uncaught exception. On hardware error the wrapper: - closes the transport so the next open() creates a fresh HCI socket - clears all cached manager/transport/task state - emits an ORM-level 'hardwareError' event As part of the error handling it tries to recover the BLE peripheral: - a single restart attempt: best-effort destroy with a 5 s timeout, a 5 s wait, then recreate the active advertising peripheral - falls back to switchBlePeripheralMode('OFF') if recreation throws, keeping the app running and updating the UI to reflect OFF state
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.
This pull request introduces significant improvements to the management and robustness of Bluetooth Low Energy (BLE) peripherals. The main focus is on making BLE error handling more resilient, centralizing BLE manager instantiation, and ensuring that peripheral mode switching is performed safely and exclusively. The changes also refactor the BLE manager to emit hardware error events and handle recovery more gracefully. This is an attempt to fix #245
TL;DR
On hardware error the wrapper:
As part of the error handling it tries to recover the BLE peripheral:
Bluetooth error handling and recovery:
hardwareErrorevent to theBleManagerclass, which emits when a hardware error occurs. The manager now resets its state and notifies listeners, enabling the system to react to BLE errors more reliably.restartBlePeripheralfunction that attempts to recover from BLE hardware errors by destroying and recreating the BLE peripheral, with safeguards to prevent concurrent restarts or changes.Peripheral mode switching and exclusivity:
runExclusivePeripheralChangehelper to serialize peripheral mode changes and prevent race conditions, replacing manual in-progress flags inswitchBlePeripheralMode,switchAntPeripheralMode, andswitchHrmMode.isBleRestartInProgressflag to prevent overlapping BLE restarts.Centralized BLE manager instantiation:
ensureBleManagerfunction to centralize BLE manager creation and event hookup, reducing code duplication and ensuring consistent event handling.Other improvements and refactoring: