#linux-bluetooth #bluetooth #ble #bluez #linux #async-client #api-bindings

bluez-async

An async wrapper around the D-Bus interface of BlueZ (the Linux Bluetooth daemon), supporting GATT client (central) functionality

17 releases

0.7.2 Apr 18, 2023
0.7.1 Feb 2, 2023
0.7.0 Jan 18, 2023
0.6.0 Jun 14, 2022
0.3.1 Mar 11, 2021

#92 in Unix APIs

Download history 3240/week @ 2024-07-21 2921/week @ 2024-07-28 2902/week @ 2024-08-04 2937/week @ 2024-08-11 2490/week @ 2024-08-18 2500/week @ 2024-08-25 2975/week @ 2024-09-01 3097/week @ 2024-09-08 3041/week @ 2024-09-15 2948/week @ 2024-09-22 2777/week @ 2024-09-29 2960/week @ 2024-10-06 2970/week @ 2024-10-13 3469/week @ 2024-10-20 2733/week @ 2024-10-27 2524/week @ 2024-11-03

11,970 downloads per month
Used in 61 crates (9 directly)

MIT/Apache

175KB
4K SLoC

BlueZ async client

crates.io page docs.rs page

bluez-async is an async wrapper around the D-Bus interface of BlueZ, the Linux Bluetooth daemon. It provides type-safe interfaces to a subset of the Bluetooth client (i.e. central, in Bluetooth terminology) interfaces exposed by BlueZ, focussing on the Generic Attribute Profile (GATT) of Bluetooth Low Energy (BLE).

Usage

// Create a new session. This establishes the D-Bus connection to talk to BlueZ. In this case we
// ignore the join handle, as we don't intend to run indefinitely.
let (_, session) = BluetoothSession::new().await?;

// Start scanning for Bluetooth devices, and wait a few seconds for some to be discovered.
session.start_discovery().await?;
time::sleep(Duration::from_secs(5)).await;
session.stop_discovery().await?;

// Get a list of devices which are currently known.
let devices = session.get_devices().await?;

// Find the device we care about.
let device = devices
    .into_iter()
    .find(|device| device.name.as_deref() == Some("My device"))
    .unwrap();

// Connect to it.
session.connect(&device.id).await?;

// Look up a GATT service and characteristic by short UUIDs.
let service = session
    .get_service_by_uuid(&device.id, uuid_from_u16(0x1234))
    .await?;
let characteristic = session
    .get_characteristic_by_uuid(&service.id, uuid_from_u32(0x1235))
    .await?;

// Read the value of the characteristic and write a new value.
println!(
    "Value: {:?}",
    session
        .read_characteristic_value(&characteristic.id)
        .await?
);
session
    .write_characteristic_value(&characteristic.id, vec![1, 2, 3])
    .await?;

// Subscribe to notifications on the characteristic and print them out.
let mut events = session
    .characteristic_event_stream(&characteristic.id)
    .await?;
session.start_notify(&characteristic.id).await?;
while let Some(event) = events.next().await {
    if let BluetoothEvent::Characteristic {
        id,
        event: CharacteristicEvent::Value { value },
    } = event
    {
        println!("Update from {}: {:?}", id, value);
    }
}

For some more complete examples, see the examples directory.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~9–18MB
~262K SLoC