From 025e39f000f735c2fa7da9a9e6cb87ba81e89c7f Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Wed, 30 Apr 2025 13:56:21 -0400 Subject: [PATCH] added get to some properties Signed-off-by: thatscringebro --- Cargo.toml | 3 +++ src/lib.rs | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f0b873..3320ae1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..8640dd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { + 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; }