6 个版本
0.3.0-alpha.1 | 2021年12月9日 |
---|---|
0.2.1 | 2021年7月30日 |
0.1.2 | 2021年7月21日 |
0.1.0 | 2020年11月29日 |
#657 在 硬件支持
170KB
4K SLoC
gatt
蓝牙低功耗通用属性配置文件
参考蓝牙核心规范版本 5.1 | 第 3 卷,第 G 部分 通用属性配置文件 (GATT)
示例
use gatt::{CharacteristicProperties, Registration, Server};
use gatt::services as srv;
use gatt::characteristics as ch;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Token {
DeviceName,
BatteryLevelNotify,
}
fn new_registration() -> Registration<Token> {
let mut registration = Registration::new();
registration.add_primary_service(srv::GENERIC_ACCESS);
registration.add_characteristic_with_token(
Token::DeviceName,
ch::DEVICE_NAME,
"abc",
CharacteristicProperties::WRITE,
);
registration.add_characteristic(
ch::APPEARANCE,
0x03c0u16.to_le_bytes().to_vec(),
CharacteristicProperties::READ,
);
registration.add_primary_service(srv::GENERIC_ATTRIBUTE);
registration.add_characteristic(
ch::SERVICE_CHANGED,
"",
CharacteristicProperties::INDICATE,
);
registration.add_primary_service(srv::DEVICE_INFORMATION);
registration.add_characteristic(
ch::MANUFACTURER_NAME_STRING,
"機械",
CharacteristicProperties::READ,
);
registration.add_characteristic(
ch::MODEL_NUMBER_STRING,
"A123",
CharacteristicProperties::READ,
);
registration.add_characteristic(
ch::SERIAL_NUMBER_STRING,
"333-444",
CharacteristicProperties::READ,
);
registration.add_primary_service(srv::BATTERY);
registration.add_characteristic_with_token(
Token::BatteryLevelNotify,
ch::BATTERY_LEVEL,
"",
CharacteristicProperties::NOTIFY,
);
registration
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
use std::io::stdin;
use tokio::task::spawn_blocking;
use tokio::io::AsyncWriteExt;
let mut server = Server::bind()?;
let mut connection = server.accept(new_registration()).await?.unwrap();
let mut events = connection.events();
let mut notification = connection.notification(&Token::BatteryLevelNotify)?;
let task = connection.run();
tokio::pin!(task);
let mut n = 0;
loop {
tokio::select! {
r = Pin::new(&mut task) => {
r?;
break;
}
maybe_line = spawn_blocking(|| stdin().read_line(&mut String::new())) => {
maybe_line??;
notification.write_all(&[n]).await?;
n += 1;
}
event = events.next() => {
if let Some(event) = event {
println!("{:?}", event);
}
}
}
}
// ...
# Ok(())
}
支持的目标
- x86_64-unknown-linux-gnu
在 Linux 5.13 (Arch Linux) 上测试
许可证
许可协议为以下之一
- Apache License, Version 2.0 (LICENSE-APACHE 或 http://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT 或 http://opensource.org/licenses/MIT)任选。
贡献
除非你明确说明,否则根据Apache-2.0许可证定义,你有意提交的任何贡献都应双许可,如上所述,不附加任何额外条款或条件!
许可证:MIT 或 Apache-2.0
依赖
~4–13MB
~141K SLoC