BLE Streaming (Nordic UART Service)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
ComChan supports wireless data streaming from Bluetooth Low Energy (BLE) enabled embedded devices, such as the nRF52 series, using the Nordic UART Service (NUS) protocol. This feature allows users to monitor serial logs and visualize telemetry data without physical wired connections. The implementation leverages the btleplug library to interface with system Bluetooth adapters and provides an interactive CLI flow for device discovery and connection.
Sources: README.md:144-150, src/ble.rs:1-10
The BLE streaming module is integrated into both the raw terminal Monitor and the 2D/3D Plotter modes. Users can initiate BLE mode via specific CLI flags, which triggers a scanning process to identify available peripherals broadcasting the NUS service.
Sources: README.md:152-157, src/config.rs:222
Architecture and Discovery
The BLE implementation is encapsulated within a dedicated module that manages the asynchronous lifecycle of the Bluetooth connection. It utilizes a multi-threaded Tokio runtime to handle scanning, service discovery, and notification subscriptions while communicating with the main TUI thread via message passing.
Sources: src/ble.rs:22-30, src/monitor.rs:364-370
Connection Lifecycle
The following sequence diagram illustrates the process from user initiation to data streaming:
Sources: src/ble.rs:32-75, README.md:155-157
Service Identifiers
ComChan specifically targets the Nordic UART Service (NUS) by filtering for its unique UUIDs. The system primarily interacts with the TX characteristic to receive data notifications.
| Type | UUID | Purpose |
|---|---|---|
| NUS TX Characteristic | 6e400003-b5a3-f393-e0a9-e50e24dcca9e | Peripheral to Host data stream |
| NUS RX Characteristic | 6e400002-b5a3-f393-e0a9-e50e24dcca9e | Host to Peripheral commands |
| Sources: src/ble.rs:15-16, code_tests/ble_server/src/main.c:13-20 |
Implementation Details
Data Flow Logic
When a connection is established, ComChan spawns a background task that listens for BLE notifications. These notifications are converted from raw bytes to strings using lossy UTF-8 conversion and sent through a cross-beam channel to the active UI mode.
Sources: src/ble.rs:77-92
Sources: src/ble.rs:80-90, src/plotter.rs:408-435
Event Handling
The system uses a typed BleEvent enum to signal status changes and incoming data to the rest of the application.
Sources: src/ble.rs:18-22
// src/ble.rs:18-21
#[derive(Debug, Clone)]
pub enum BleEvent {
Payload(String),
Disconnected,
}
Integration with UI Modes
Monitor Integration
In run_normal_mode, the application polls the BLE receiver channel. It handles automatic line reconstruction (searching for \n) and provides verbose logging if configured. The monitor also supports a graceful exit if the BleEvent::Disconnected signal is received.
Sources: src/monitor.rs:364-406
Plotter Integration
The plotter mode includes specific logic to prevent UI lockups during high-frequency BLE streaming. It limits the number of BLE messages processed per frame iteration (maximum 32 messages) to maintain a responsive 60 FPS rendering loop.
Sources: src/plotter.rs:407-436
Embedded Server Configuration (Zephyr)
A reference implementation for the peripheral side is provided using the Zephyr RTOS. It defines the NUS service and utilizes the bt_gatt_notify function to push telemetry strings (e.g., "Pitch: X, Roll: Y, Yaw: Z") at a 10Hz update rate.
Sources: code_tests/ble_server/src/main.c:38-55, code_tests/ble_server/src/main.c:116-136
Summary
BLE Streaming in ComChan provides a robust, wireless alternative to traditional UART monitoring. By focusing on the Nordic UART Service, it ensures compatibility with a wide range of development boards. The system handles the complexities of asynchronous Bluetooth operations while providing a seamless TUI experience that supports both raw text logging and real-time sensor plotting.
Sources: README.md:144-160, src/ble.rs:77-80