corebluetooth-async

Crates.iocorebluetooth-async
lib.rscorebluetooth-async
version0.1.0
created_at2025-07-07 19:11:19.371831+00
updated_at2025-07-07 19:11:19.371831+00
descriptionAn asynchronous wrapper for the `corebluetooth` crate
homepage
repositoryhttps://github.com/alexmoon/corebluetooth-rs
max_upload_size
id1741658
size67,619
Alex Moon (alexmoon)

documentation

https://docs.rs/corebluetooth

README

corebluetooth-async

An asynchronous wrapper for the corebluetooth crate.

This crate provides async functions and streams for interacting with the CoreBluetooth framework, making it easy to use within async Rust applications. It is built on top of the corebluetooth crate. This is likely the crate you will want to use for most applications.

Example

This example shows how to scan for peripherals and print their advertisement data using an asynchronous stream.

Add futures and tokio (or another async runtime) to your Cargo.toml dependencies. You will also need to depend on the corebluetooth crate to have access to some enums.

use corebluetooth::CBManagerState;
use corebluetooth_async::CentralManagerAsync;
use dispatch_executor::MainThreadMarker;
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mtm = MainThreadMarker::new().unwrap();
    let central = CentralManagerAsync::main_thread(false, mtm);

    // Wait for the manager to power on.
    central
        .state_updates()
        .filter(|&state| state == CBManagerState::PoweredOn)
        .next()
        .await;

    println!("Bluetooth is powered on, starting scan.");

    // Start scanning for peripherals.
    let mut discoveries = central.scan(None, false, None);

    // Print discoveries as they come in.
    while let Some(discovery) = discoveries.next().await {
        if let Some(name) = discovery.peripheral.name() {
            println!(
                "Discovered peripheral '{}' (RSSI: {}) with advertisement data: {:?}",
                name, discovery.rssi, discovery.advertisement_data
            );
        }
    }

    Ok(())
}
Commit count: 0

cargo fmt