added get to some properties

Signed-off-by: thatscringebro <thatscringebro@tutanota.com>
This commit is contained in:
thatscringebro 2025-04-30 13:56:21 -04:00
parent f908c728df
commit 025e39f000
2 changed files with 38 additions and 10 deletions

View File

@ -4,3 +4,6 @@ version = "0.1.0"
edition = "2024"
[dependencies]
btleplug = "0.11.8"
tokio = { version = "1.44.2", features = ["full"] }
uuid = "1.16.0"

View File

@ -1,14 +1,39 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
use btleplug::Result;
use btleplug::api::{Central, Manager as _, Peripheral as _, ScanFilter};
use btleplug::platform::{Adapter, Manager, Peripheral};
use std::error::Error;
use uuid::{Uuid, uuid};
#[cfg(test)]
mod tests {
use super::*;
const FIRMWARE_VERSION_UUID: Uuid = uuid!("00002a26-0000-1000-8000-00805f9b34fb");
const BATTERY_LEVEL_UUID: Uuid = uuid!("00002a19-0000-1000-8000-00805f9b34fb");
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
pub async fn find_infinitime(adapter: &Adapter) -> Option<Peripheral> {
for p in adapter.peripherals().await.unwrap() {
if p.properties()
.await
.unwrap()
.unwrap()
.local_name
.iter()
.any(|name| name.to_lowercase().contains("infinitime"))
{
return Some(p);
}
}
return None;
}
async fn get_property_value(infinitime: &Peripheral, property: Uuid) -> String {
let chars = infinitime.characteristics();
let cmd_char = chars.iter().find(|c| c.uuid == property).unwrap();
let result = infinitime.read(cmd_char).await.unwrap();
return String::from_utf8(result).expect("Invalid UTF-8");
}
pub async fn get_firmware_version(infinitime: &Peripheral) -> String {
return get_property_value(infinitime, FIRMWARE_VERSION_UUID).await;
}
pub async fn get_battery_level(infinitime: &Peripheral) -> String {
return get_property_value(infinitime, BATTERY_LEVEL_UUID).await;
}