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
- Minimal API: Only essential operations
- Error transparency: Platform errors are exposed
- No callbacks: Use polling or async/await at the platform level
- 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§
Required Methods§
Sourcefn start_advertising(&mut self, device_name: &str) -> Result<(), Self::Error>
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.
Sourcefn stop_advertising(&mut self) -> Result<(), Self::Error>
fn stop_advertising(&mut self) -> Result<(), Self::Error>
Stop BLE advertising
If already connected, this may also disconnect.
Sourcefn is_connected(&self) -> bool
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.
Sourcefn connection_status(&self) -> ConnectionStatus
fn connection_status(&self) -> ConnectionStatus
Get current connection status
Provides more detail than is_connected().
Sourcefn send(&mut self, data: &[u8]) -> Result<(), Self::Error>
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.
Sourcefn receive(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>
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)wherenis the number of bytes written tobufferErr(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).