corebluetooth-async 0.1.0

An asynchronous wrapper for the `corebluetooth` crate
docs.rs failed to build corebluetooth-async-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

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(())
}