Skip to main content

Module ble

Module ble 

Source
Expand description

Bluetooth LE: Espressif’s controller (via esp-radio) under the pure-Rust trouble host stack. Requires the ble feature.

ⓘ
let controller = ble::controller(board.radio.bt.reborrow())?;
let mut resources = ble::ScanResources::new();
let stack = ble::stack(controller, &mut resources);
loop {
    ble::scan(&stack, Duration::from_secs(5), |adv| {
        info!("{} dBm {} {:?}", adv.rssi, adv.address, adv.name());
    })
    .await?;
}

GATT services are declared in the application with trouble’s #[gatt_server] / #[gatt_service] macros (see ble_peripheral).

Create the controller and stack once per boot: trouble keeps the host inside the resources without ever dropping it, so a second controller would find the first still registered with the radio. Scans, advertising sessions and connections on that stack can be started as often as needed.

BLE alone fits in the reclaimed-RAM heap. Running Wi-Fi at the same time (coexistence) needs more; see crate::radio.

Re-exports§

pub use bt_hci;
pub use trouble_host;

Structs§

Advertisement
One advertising report, as passed to the scan callback.

Constants§

COMMAND_SLOTS
HCI command slots the controller adapter can have in flight (the value trouble’s ESP32 examples use).

Functions§

address
This device’s BLE address: a static random address derived from the chip’s factory Bluetooth MAC, so it is stable across boots and unique per board. (Static random addresses must have the two top bits set.)
controller
Starts the Bluetooth controller.
scan
Scans for duration, calling on_advertisement for every report (duplicates included, so signal strength stays current). Active scanning, 100 ms interval and window, so names usually arrive too.
stack
Builds the host stack on controller with this device’s address.

Type Aliases§

Controller
The HCI controller type to hand to trouble_host::new.
HostError
Errors from the host stack.
InitError
Error starting the BLE controller.
ScanResources
Host resources sized for scanning (one connection slot, one channel). Peripherals with GATT services usually want more; see the trouble docs.