#linux-bluetooth #bluetooth #ble #bluez #async-client #linux #events

bluez-async

BlueZ (Linux蓝牙守护进程)的D-Bus接口的异步包装器,支持GATT客户端(中心)功能

17个版本

0.7.2 2023年4月18日
0.7.1 2023年2月2日
0.7.0 2023年1月18日
0.6.0 2022年6月14日
0.3.1 2021年3月11日

#2 in #bluez

Download history 4427/week @ 2024-04-23 5064/week @ 2024-04-30 4106/week @ 2024-05-07 3526/week @ 2024-05-14 3403/week @ 2024-05-21 3713/week @ 2024-05-28 3007/week @ 2024-06-04 2999/week @ 2024-06-11 3772/week @ 2024-06-18 4893/week @ 2024-06-25 4075/week @ 2024-07-02 4082/week @ 2024-07-09 3310/week @ 2024-07-16 3267/week @ 2024-07-23 2841/week @ 2024-07-30 2926/week @ 2024-08-06

12,857 每月下载量
用于 56 个crate(9直接)

MIT/Apache

175KB
4K SLoC

BlueZ异步客户端

crates.io page docs.rs page

bluez-async 是 BlueZ(Linux蓝牙守护进程)的D-Bus接口的异步包装器。它提供了类型安全的接口,用于BlueZ暴露的蓝牙客户端(即中心,在蓝牙术语中)接口的子集,专注于蓝牙低能耗(BLE)的通用属性配置文件(GATT)。

用法

// 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);
    }
}

有关更多完整示例,请参阅示例目录

许可证

根据您的选择,许可协议为

贡献

除非您明确声明,否则根据Apache-2.0许可证定义,您有意提交的任何贡献,包括但不限于以下内容,都应作为上述双重许可,而不附加任何额外的条款或条件。

依赖关系

~9–19MB
~265K SLoC