@@ -275,8 +275,6 @@ async fn watch_bt_presence_async(
275275 start_bt_presence_watch ( & ble_watcher) ?;
276276
277277 scopeguard:: defer! {
278- info!( "Release the watching of presence in the devices" ) ;
279-
280278 btc_tokens. into_iter( ) . enumerate( ) . for_each( |( index, token) | match index {
281279 0 => { let _ = btc_watcher. RemoveAdded ( token) ; } ,
282280 1 => { let _ = btc_watcher. RemoveRemoved ( token) ; } ,
@@ -292,53 +290,54 @@ async fn watch_bt_presence_async(
292290 stop_bt_presence_watch( & ble_watcher) . unwrap( ) ;
293291 }
294292
295- while !exit_flag . load ( Ordering :: Relaxed ) {
293+ loop {
296294 tokio:: select! {
297295 maybe_update = rx. recv( ) => {
298- if let Some ( ( info, presence) ) = maybe_update {
299- let update_event = |presence: BluetoothPresence , name: String | {
300- // 设备添加/移除后,所有监听增加或移除设备
301- restart_flag. fetch_add( 1 , Ordering :: Relaxed ) ;
302- // 更新托盘信息
303- let _ = proxy. send_event( UserEvent :: UnpdatTray ) ;
304- // 因 Watcher 无 Config,需传递给有通知配置的 APP 结构体
305- match presence {
306- BluetoothPresence :: Added => {
307- info!( "[{name}]: New Bluetooth Device Connected" ) ;
308- let _ = proxy. send_event( UserEvent :: Notify ( NotifyEvent :: Added ( name) ) ) ;
309- }
310- BluetoothPresence :: Removed => {
311- info!( "[{name}]: Bluetooth Device Removed" ) ;
312- let _ = proxy. send_event( UserEvent :: Notify ( NotifyEvent :: Removed ( name) ) ) ;
313- }
296+ let Some ( ( info, presence) ) = maybe_update else {
297+ return Err ( anyhow!( "Channel closed while watching Bluetooth presence" ) ) ;
298+ } ;
299+
300+ let update_event = |presence: BluetoothPresence , name: String | {
301+ // 设备添加/移除后,所有监听增加或移除设备
302+ restart_flag. fetch_add( 1 , Ordering :: Relaxed ) ;
303+ // 更新托盘信息
304+ let _ = proxy. send_event( UserEvent :: UnpdatTray ) ;
305+ // 因 Watcher 无 Config,需传递给有通知配置的 APP 结构体
306+ match presence {
307+ BluetoothPresence :: Added => {
308+ info!( "[{name}]: New Bluetooth Device Connected" ) ;
309+ let _ = proxy. send_event( UserEvent :: Notify ( NotifyEvent :: Added ( name) ) ) ;
314310 }
315- } ;
316-
317- if let Entry :: Vacant ( e) = bluetooth_devices_info. lock( ) . unwrap( ) . entry( info. address) {
318- let name = info. name. clone( ) ;
319- e. insert( info) ;
320- update_event( presence, name) ;
321- } else {
322- match presence {
323- BluetoothPresence :: Added => ( ) , // 原设备未被移除
324- BluetoothPresence :: Removed => {
325- let removed_info = bluetooth_devices_info. lock( ) . unwrap( ) . remove( & info. address) ;
326- let name = removed_info. map_or( "Unknown name" . to_owned( ) , |i| i. name) ;
327- update_event( presence, name) ;
328- }
311+ BluetoothPresence :: Removed => {
312+ info!( "[{name}]: Bluetooth Device Removed" ) ;
313+ let _ = proxy. send_event( UserEvent :: Notify ( NotifyEvent :: Removed ( name) ) ) ;
329314 }
330315 }
316+ } ;
317+
318+ if let Entry :: Vacant ( e) = bluetooth_devices_info. lock( ) . unwrap( ) . entry( info. address) {
319+ let name = info. name. clone( ) ;
320+ e. insert( info) ;
321+ update_event( presence, name) ;
331322 } else {
332- return Err ( anyhow!( "Channel closed while watching Bluetooth presence" ) ) ;
323+ match presence {
324+ BluetoothPresence :: Added => ( ) , // 原设备未被移除
325+ BluetoothPresence :: Removed => {
326+ let removed_info = bluetooth_devices_info. lock( ) . unwrap( ) . remove( & info. address) ;
327+ let name = removed_info. map_or( "Unknown name" . to_owned( ) , |i| i. name) ;
328+ update_event( presence, name) ;
329+ }
330+ }
333331 }
334332 }
335333 _ = async {
336334 while !exit_flag. load( Ordering :: Relaxed ) {
337335 tokio:: time:: sleep( std:: time:: Duration :: from_secs( 1 ) ) . await ;
338336 }
339- } => info!( "Watch Bluetooth Presence was cancelled by exit flag." ) ,
337+ } => {
338+ info!( "Watch Bluetooth Presence was cancelled by exit flag." ) ;
339+ return Ok ( ( ) ) ;
340+ } ,
340341 }
341342 }
342-
343- Ok ( ( ) )
344343}
0 commit comments