Skip to content

Conversation

@jamesarich
Copy link
Collaborator

@jamesarich jamesarich commented Nov 1, 2025

Work In Progress - This is not yet functional.

This pull request migrates the Bluetooth radio interface implementation to use the new Nordic BLE client library, replacing the previous custom Bluetooth logic. The main changes include introducing the new NordicBleInterface class and updating related factories, specs, dependency injection bindings, and service logic to use this new implementation. Additionally, the legacy BluetoothWorkQueue and related classes have been removed, as they are no longer needed with the new BLE library.

Migration to Nordic BLE client library:

  • Added NordicBleInterface class, which uses the Nordic BLE client library for scanning, connecting, service/characteristic discovery, and data transfer, replacing the previous custom Bluetooth logic. (app/src/main/java/com/geeksville/mesh/repository/radio/NordicBleInterface.kt)
  • Updated dependency management to include the Nordic BLE client library in the project dependencies. (app/build.gradle.kts)

Refactoring factories, specs, and dependency injection:

  • Renamed and refactored the interface factory and spec classes to use NordicBleInterface instead of BluetoothInterface, updating their names and references throughout the codebase. (app/src/main/java/com/geeksville/mesh/repository/radio/NordicBleInterfaceFactory.kt, app/src/main/java/com/geeksville/mesh/repository/radio/NordicBleInterfaceSpec.kt) [1] [2]
  • Updated DI bindings to bind NordicBleInterfaceSpec for the Bluetooth interface ID. (app/src/main/java/com/geeksville/mesh/repository/radio/RadioRepositoryModule.kt)

Service logic updates:

  • Changed service logic to reference NordicBleInterface instead of BluetoothInterface for starting and stopping the radio interface. (app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt)

Removal of legacy Bluetooth work queue:

  • Deleted the legacy BluetoothWorkQueue and related work item classes, which are now obsolete due to the migration to the Nordic BLE library. (app/src/main/java/com/geeksville/mesh/service/BluetoothWorkQueue.kt)

… BLE library

This commit replaces the entire custom Bluetooth Low Energy (BLE) implementation with the modern, coroutine-based Nordic Semiconductor Kotlin BLE library. The legacy implementation, spread across `SafeBluetooth`, `BluetoothWorkQueue`, and `BluetoothInterface`, was complex, prone to race conditions, and difficult to maintain.

Specific changes include:
- Deleting the legacy BLE classes: `BluetoothInterface`, `SafeBluetooth`, `SafeBluetoothGattCallback`, and `BluetoothWorkQueue`.
- Introducing `NordicBleInterface` which leverages the Kotlin BLE library to manage BLE connections, service discovery, and communication.
- Adding the `no.nordicsemi.kotlin.ble:client-android` library dependency.
- Refactoring `RadioInterfaceService` and related factory/module classes to use the new `NordicBleInterface` instead of the old `BluetoothInterface`.
- Simplifying the connection logic by removing manual workarounds like forced service refreshes and complex reconnect scheduling.

Signed-off-by: James Rich <[email protected]>
@github-actions github-actions bot added the enhancement New feature or request label Nov 1, 2025
This commit refactors the Nordic BLE interface to improve reliability and performance. The previous implementation is replaced with a more robust event-driven model based on the `FROMNUM` notification characteristic.

Key changes:
- Switched from subscribing to `FROMRADIO` to subscribing to `FROMNUM` notifications.
- Implemented a packet queue draining mechanism that reads from the `FROMRADIO` characteristic after a `FROMNUM` notification is received.
- Introduced a `Mutex` to serialize access to the packet queue, preventing race conditions.
- Drains the packet queue upon initial connection and after writes to ensure pending data is processed promptly.
- Centralized coroutine management under the `RadioInterfaceService`'s scope for better lifecycle control.
- Added extensive `Timber` logging and error handling throughout the BLE and radio service code to improve debuggability and prevent crashes.
- Moved the Nordic BLE library dependency to `libs.versions.toml`.

Signed-off-by: James Rich <[email protected]>
This commit refactors the device connection and configuration logic to improve reliability, particularly over BLE.

- **Two-Stage Configuration:** The configuration process is now split into two distinct stages: `config-only` and `node-info`. This mirrors the implementation in Meshtastic-Apple and helps prevent issues with stale BLE packets during initial connection. Each stage uses a unique nonce (`configOnlyNonce`, `nodeInfoNonce`) for verification.

- **BLE Stability:**
    - A heartbeat is now sent between the two configuration stages to keep the BLE link active.
    - The `onDisconnect` call in `NordicBleInterface` now passes `false`, preventing automatic reconnection attempts on explicit disconnects or connection failures.
    - The delay between packet-queue reads (`INTER_READ_DELAY_MS`) has been reduced from 20ms to 5ms for faster processing.

- **Logging and Debugging:**
    - Added more detailed logging for unhandled `FromRadio` protocol buffer variants.
    - Cleaned up comments and removed unnecessary code related to the old single-stage config flow.
    - Minor exception handling and code style cleanups.

Signed-off-by: James Rich <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant