Skip to content

Commit 42f6b2d

Browse files
committed
feat: use FindAllAsyncAqsFilter instead of FindAllAsync for Bluetooth device discovery
1 parent 1719c8b commit 42f6b2d

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# BlueGauge
2-
A lightweight tray tool for easily checking the battery level of your Bluetooth devices.
2+
A lightweight tray tool for easily checking the battery level of your Bluetooth LE devices.
33

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

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

@@ -10,4 +10,4 @@ A lightweight tray tool for easily checking the battery level of your Bluetooth
1010
- [ ] 左键点击托盘显示通知
1111
- [ ] 菜单:自定义更新时间
1212
- [ ] 菜单:添加开机启动
13-
- [ ] 托盘图标替换为指定蓝牙设备的电量
13+
- [ ] 托盘图标替换为指定蓝牙设备的电量(数字或电池图标)

src/bluetooth.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,11 @@ use windows::{
77
};
88

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

13-
let mut discovered_devices = Vec::new();
12+
let devices = DeviceInformation::FindAllAsyncAqsFilter(&bt_le_aqs_filter)?.get()?;
1413

15-
Ok(devices
16-
.into_iter()
17-
.filter_map(|device| {
18-
device.Name().ok().and_then(|n| {
19-
let name = n.to_string();
20-
if discovered_devices.contains(&name) {
21-
None
22-
} else {
23-
discovered_devices.push(name);
24-
Some(device)
25-
}
26-
})
27-
})
28-
.collect()
29-
)
14+
Ok(devices.into_iter().collect())
3015
}
3116

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

5641
for device in devices {
5742
if let Ok(le_device) = BluetoothLEDevice::FromIdAsync(&device.Id()?)?.get() {
43+
let device_name = device.Name()?.to_string();
44+
5845
let status = le_device.ConnectionStatus().expect("Failed to get link status");
5946

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

6552
if status == BluetoothConnectionStatus::Connected {
66-
let menu_text = format!(" {} - {}%", device.Name().unwrap(), battery_level);
67-
let tooltip_text = format!("🟢 {} - {}%", device.Name().unwrap(), battery_level);
53+
let menu_text = format!("🔗 {} - {}%", &device_name, battery_level);
54+
let tooltip_text = format!("🟢 {} - {}%", &device_name, battery_level);
6855
menu_items.insert(0, menu_text);
6956
tooltip.insert(0, tooltip_text);
57+
// println!("{:?}", device.Properties()?)
7058
} else {
71-
let menu_text = format!("× {} - {}%", device.Name().unwrap(), battery_level);
72-
let tooltip_text = format!("🔴 {} - {}%", device.Name().unwrap(), battery_level);
59+
let menu_text = format!(" {} - {}%", &device_name, battery_level);
60+
let tooltip_text = format!("🔴 {} - {}%", &device_name, battery_level);
7361
menu_items.push(menu_text);
7462
tooltip.push(tooltip_text);
7563
};

src/systray.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tao::event_loop::{ControlFlow, EventLoopBuilder};
22
use tao::platform::run_return::EventLoopExtRunReturn;
33
use tray_icon::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem};
4-
use tray_icon::TrayIconBuilder;
4+
use tray_icon::TrayIconBuilder; // TrayIconEvent
55
use image;
66

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

5252
let menu_channel = MenuEvent::receiver();
53+
// let tray_channel = TrayIconEvent::receiver();
5354

5455
{
5556
let devices_clone = devices.clone();
@@ -85,16 +86,23 @@ fn loop_systray() -> windows::core::Result<()> {
8586
if menu_event.id == menu_quit.id() {
8687
println!("process exist");
8788
*control_flow = ControlFlow::Exit;
88-
}
89-
}
89+
};
90+
};
91+
92+
// if let Ok(tray_event) = tray_channel.try_recv() {
93+
// if tray_event.id() == tray_icon.id() {
94+
// return
95+
// };
96+
// };
9097

9198
if event == tao::event::Event::UserEvent(()) {
9299
println!("update tooltip and menu");
93100
let tray_menu = Menu::new();
101+
94102
let tooltip_lock = tooltip.lock().unwrap();
95-
let menu_items_2 = menu_items.lock().unwrap();
103+
let menu_items_lock = menu_items.lock().unwrap();
96104

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

104112
tray_icon.set_tooltip(Some(tooltip_lock.join("\n"))).unwrap();
105113
tray_icon.set_menu(Some(Box::new(tray_menu)));
106-
}
114+
};
107115
});
108116

109117
if return_code != 0 {
110118
std::process::exit(return_code);
111-
}
119+
};
112120

113121
Ok(())
114122
}

0 commit comments

Comments
 (0)