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
12,857 每月下载量
用于 56 个crate(9直接)
175KB
4K SLoC
BlueZ异步客户端
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