Crates.io | corebluetooth-async |
lib.rs | corebluetooth-async |
version | 0.1.0 |
created_at | 2025-07-07 19:11:19.371831+00 |
updated_at | 2025-07-07 19:11:19.371831+00 |
description | An asynchronous wrapper for the `corebluetooth` crate |
homepage | |
repository | https://github.com/alexmoon/corebluetooth-rs |
max_upload_size | |
id | 1741658 |
size | 67,619 |
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.
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(())
}