Skip to content

Commit

Permalink
feat: use FindAllAsyncAqsFilter instead of FindAllAsync for Bluetooth…
Browse files Browse the repository at this point in the history
… device discovery
  • Loading branch information
iKineticate committed Aug 29, 2024
1 parent 1719c8b commit 42f6b2d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# BlueGauge
A lightweight tray tool for easily checking the battery level of your Bluetooth devices.
A lightweight tray tool for easily checking the battery level of your Bluetooth LE devices.

一款轻便的托盘工具,可轻松检查蓝牙设备的电池电量
一款轻便的托盘工具,可轻松检查低功耗蓝牙设备的电池电量

![image](https://raw.githubusercontent.com/iKineticate/BlueGauge/main/screenshots/app.png)

Expand All @@ -10,4 +10,4 @@ A lightweight tray tool for easily checking the battery level of your Bluetooth
- [ ] 左键点击托盘显示通知
- [ ] 菜单:自定义更新时间
- [ ] 菜单:添加开机启动
- [ ] 托盘图标替换为指定蓝牙设备的电量
- [ ] 托盘图标替换为指定蓝牙设备的电量(数字或电池图标)
32 changes: 10 additions & 22 deletions src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@ use windows::{
};

pub fn find_bluetooth_devices() -> windows::core::Result<Vec<DeviceInformation>> {
let devices =
windows::Devices::Enumeration::DeviceInformation::FindAllAsync()?.get()?;
let bt_le_aqs_filter = BluetoothLEDevice::GetDeviceSelector().unwrap();

let mut discovered_devices = Vec::new();
let devices = DeviceInformation::FindAllAsyncAqsFilter(&bt_le_aqs_filter)?.get()?;

Ok(devices
.into_iter()
.filter_map(|device| {
device.Name().ok().and_then(|n| {
let name = n.to_string();
if discovered_devices.contains(&name) {
None
} else {
discovered_devices.push(name);
Some(device)
}
})
})
.collect()
)
Ok(devices.into_iter().collect())
}

pub fn get_battery_level(device: &BluetoothLEDevice) -> windows::core::Result<u8> {
Expand Down Expand Up @@ -55,6 +40,8 @@ pub fn get_bluetooth_info(devices: Vec<DeviceInformation>) -> windows::core::Res

for device in devices {
if let Ok(le_device) = BluetoothLEDevice::FromIdAsync(&device.Id()?)?.get() {
let device_name = device.Name()?.to_string();

let status = le_device.ConnectionStatus().expect("Failed to get link status");

let battery_level = match get_battery_level(&le_device) {
Expand All @@ -63,13 +50,14 @@ pub fn get_bluetooth_info(devices: Vec<DeviceInformation>) -> windows::core::Res
};

if status == BluetoothConnectionStatus::Connected {
let menu_text = format!(" {} - {}%", device.Name().unwrap(), battery_level);
let tooltip_text = format!("🟢 {} - {}%", device.Name().unwrap(), battery_level);
let menu_text = format!("🔗 {} - {}%", &device_name, battery_level);
let tooltip_text = format!("🟢 {} - {}%", &device_name, battery_level);
menu_items.insert(0, menu_text);
tooltip.insert(0, tooltip_text);
// println!("{:?}", device.Properties()?)
} else {
let menu_text = format!("× {} - {}%", device.Name().unwrap(), battery_level);
let tooltip_text = format!("🔴 {} - {}%", device.Name().unwrap(), battery_level);
let menu_text = format!(" {} - {}%", &device_name, battery_level);
let tooltip_text = format!("🔴 {} - {}%", &device_name, battery_level);
menu_items.push(menu_text);
tooltip.push(tooltip_text);
};
Expand Down
22 changes: 15 additions & 7 deletions src/systray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tao::event_loop::{ControlFlow, EventLoopBuilder};
use tao::platform::run_return::EventLoopExtRunReturn;
use tray_icon::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem};
use tray_icon::TrayIconBuilder;
use tray_icon::TrayIconBuilder; // TrayIconEvent
use image;

use crate::bluetooth::{find_bluetooth_devices, get_bluetooth_info};
Expand Down Expand Up @@ -50,6 +50,7 @@ fn loop_systray() -> windows::core::Result<()> {
.unwrap();

let menu_channel = MenuEvent::receiver();
// let tray_channel = TrayIconEvent::receiver();

{
let devices_clone = devices.clone();
Expand Down Expand Up @@ -85,16 +86,23 @@ fn loop_systray() -> windows::core::Result<()> {
if menu_event.id == menu_quit.id() {
println!("process exist");
*control_flow = ControlFlow::Exit;
}
}
};
};

// if let Ok(tray_event) = tray_channel.try_recv() {
// if tray_event.id() == tray_icon.id() {
// return
// };
// };

if event == tao::event::Event::UserEvent(()) {
println!("update tooltip and menu");
let tray_menu = Menu::new();

let tooltip_lock = tooltip.lock().unwrap();
let menu_items_2 = menu_items.lock().unwrap();
let menu_items_lock = menu_items.lock().unwrap();

menu_items_2.iter().for_each(|i| {
menu_items_lock.iter().for_each(|i| {
let item = MenuItem::new(i, true, None);
tray_menu.append(&item).unwrap();
});
Expand All @@ -103,12 +111,12 @@ fn loop_systray() -> windows::core::Result<()> {

tray_icon.set_tooltip(Some(tooltip_lock.join("\n"))).unwrap();
tray_icon.set_menu(Some(Box::new(tray_menu)));
}
};
});

if return_code != 0 {
std::process::exit(return_code);
}
};

Ok(())
}
Expand Down

0 comments on commit 42f6b2d

Please sign in to comment.