Skip to content

Handle BLE hardware errors and improve recovery process (fixes #245)#246

Open
Abasz wants to merge 1 commit into0.9.8-In-Developmentfrom
fix-ble-crash
Open

Handle BLE hardware errors and improve recovery process (fixes #245)#246
Abasz wants to merge 1 commit into0.9.8-In-Developmentfrom
fix-ble-crash

Conversation

@Abasz
Copy link
Copy Markdown
Collaborator

@Abasz Abasz commented May 3, 2026

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:

  • 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

Bluetooth error handling and recovery:

  • Added a hardwareError event to the BleManager class, 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.
  • Implemented a new restartBlePeripheral function 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:

  • Introduced a runExclusivePeripheralChange helper to serialize peripheral mode changes and prevent race conditions, replacing manual in-progress flags in switchBlePeripheralMode, switchAntPeripheralMode, and switchHrmMode.
  • Added the isBleRestartInProgress flag to prevent overlapping BLE restarts.

Centralized BLE manager instantiation:

  • Added an ensureBleManager function to centralize BLE manager creation and event hookup, reducing code duplication and ensuring consistent event handling.

Other improvements and refactoring:

  • Refactored error handling in BLE peripheral creation to return booleans on failure, improving clarity and control flow.

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
@JaapvanEkris JaapvanEkris added this to the 0.9.8 milestone May 4, 2026
@JaapvanEkris JaapvanEkris linked an issue May 4, 2026 that may be closed by this pull request
@JaapvanEkris JaapvanEkris self-assigned this May 4, 2026
@JaapvanEkris JaapvanEkris added bug Something isn't working Development branch Concerns a development branch labels May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Development branch Concerns a development branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sporadic Bluetooth crash, crashes ORM completely

2 participants