Skip to main content

BluetoothProvider

Trait BluetoothProvider 

Source
pub trait BluetoothProvider {
    type Error: Debug;

    // Required methods
    fn start_advertising(
        &mut self,
        device_name: &str,
    ) -> Result<(), Self::Error>;
    fn stop_advertising(&mut self) -> Result<(), Self::Error>;
    fn is_connected(&self) -> bool;
    fn connection_status(&self) -> ConnectionStatus;
    fn send(&mut self, data: &[u8]) -> Result<(), Self::Error>;
    fn receive(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>;

    // Provided method
    fn flush(&mut self) -> Result<(), Self::Error> { ... }
}
Expand description

Bluetooth Low Energy provider trait

This trait must be implemented by each platform to provide BLE capabilities. The trait is designed to be simple and compatible with both async and sync implementations.

§Design Principles

  1. Minimal API: Only essential operations
  2. Error transparency: Platform errors are exposed
  3. No callbacks: Use polling or async/await at the platform level
  4. Buffer-based I/O: Caller manages buffers

§Thread Safety

Implementations do NOT need to be Send or Sync - embedded BLE typically runs in a single executor/thread.

Required Associated Types§

Source

type Error: Debug

Platform-specific error type

Required Methods§

Source

fn start_advertising(&mut self, device_name: &str) -> Result<(), Self::Error>

Start BLE advertising with the given device name

This should set up the BLE stack (if not already initialized) and begin advertising. The device becomes discoverable with the given name.

§Errors

Returns an error if:

  • BLE stack initialization fails
  • Device name is invalid (e.g., too long)
  • Advertising setup fails
§Blocking Behavior

This method MAY block until advertising is successfully started. On some platforms (e.g., TrouBLE), this might block until a connection is made. Check platform documentation for blocking behavior.

Source

fn stop_advertising(&mut self) -> Result<(), Self::Error>

Stop BLE advertising

If already connected, this may also disconnect.

Source

fn is_connected(&self) -> bool

Check if BLE is currently connected to a client

Returns true if a client is connected and data can be exchanged.

Source

fn connection_status(&self) -> ConnectionStatus

Get current connection status

Provides more detail than is_connected().

Source

fn send(&mut self, data: &[u8]) -> Result<(), Self::Error>

Send data over BLE to the connected client

On most platforms, this uses the TX characteristic (Notify).

§Errors

Returns an error if:

  • Not connected (is_connected() == false)
  • Data is too large for MTU
  • BLE stack error
§Blocking Behavior

This method MAY block until the data is queued for transmission. It does NOT wait for acknowledgment from the client.

Source

fn receive(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>

Receive data from the connected client

On most platforms, this reads from the RX characteristic (Write).

§Returns
  • Ok(n) where n is the number of bytes written to buffer
  • Err(e) if no data available or BLE error
§Non-blocking

This method SHOULD NOT block. If no data is available, return an error or Ok(0).

Provided Methods§

Source

fn flush(&mut self) -> Result<(), Self::Error>

Flush any pending transmit data

This is optional and may be a no-op on some platforms.

Implementors§