@@ -9,17 +9,15 @@ use bluest::{Adapter, DeviceId};
99
1010use tauri:: { command, AppHandle , State } ;
1111
12- const SVC_UUID : & str = "00000000-0196-6107-c967-c5cfb1c2482a" ;
13- const RPC_CHRC_UUID : & str = "00000001-0196-6107-c967-c5cfb1c2482a" ;
12+ const SVC_UUID : Uuid = Uuid :: from_u128 ( 0x00000000_0196_6107_c967_c5cfb1c2482a ) ;
13+ const RPC_CHRC_UUID : Uuid = Uuid :: from_u128 ( 0x00000001_0196_6107_c967_c5cfb1c2482a ) ;
1414
1515#[ command]
1616pub async fn gatt_connect (
1717 id : String ,
1818 app_handle : AppHandle ,
1919 state : State < ' _ , super :: commands:: ActiveConnection < ' _ > > ,
2020) -> Result < bool , ( ) > {
21- let uuid = Uuid :: parse_str ( SVC_UUID ) . expect ( "Valid UUID" ) ;
22-
2321 let adapter = Adapter :: default ( ) . await . ok_or ( ( ) ) ?;
2422
2523 adapter. wait_available ( ) . await . map_err ( |_| ( ) ) ?;
@@ -32,16 +30,15 @@ pub async fn gatt_connect(
3230 }
3331
3432 let service = d
35- . discover_services_with_uuid ( uuid )
33+ . discover_services_with_uuid ( SVC_UUID )
3634 . await
3735 . map_err ( |e| ( ) ) ?
3836 . get ( 0 )
3937 . cloned ( ) ;
4038
4139 if let Some ( s) = service {
42- let char_uuid = Uuid :: parse_str ( RPC_CHRC_UUID ) . expect ( "Valid UUID" ) ;
4340 let char = s
44- . discover_characteristics_with_uuid ( char_uuid )
41+ . discover_characteristics_with_uuid ( RPC_CHRC_UUID )
4542 . await
4643 . map_err ( |_| ( ) ) ?
4744 . get ( 0 )
@@ -81,32 +78,24 @@ pub async fn gatt_connect(
8178
8279#[ command]
8380pub async fn gatt_list_devices ( ) -> Result < Vec < super :: commands:: AvailableDevice > , ( ) > {
84- let uuid = Uuid :: parse_str ( SVC_UUID ) . expect ( "Valid UUID" ) ;
85-
8681 let adapter = Adapter :: default ( ) . await . ok_or ( ( ) ) ?;
8782
8883 adapter. wait_available ( ) . await . map_err ( |_| ( ) ) ?;
8984
9085 let devices = adapter
91- . discover_devices ( & [ uuid ] )
86+ . discover_devices ( & [ SVC_UUID ] )
9287 . await
9388 . expect ( "GET DEVICES!" )
9489 . take_until ( async_std:: task:: sleep ( Duration :: from_secs ( 2 ) ) )
95- . collect :: < Vec < _ > > ( )
96- . await ;
97-
98- let candidates: Vec < super :: commands:: AvailableDevice > = devices
99- . into_iter ( )
100- . filter_map ( |d| {
101- d. map ( |device| {
102- let label = device. name ( ) . unwrap_or ( "Unknown" . to_string ( ) ) ;
90+ . filter_map ( |d| ready ( d. ok ( ) ) )
91+ . then ( move |device| async move {
92+ let label = device. name_async ( ) . await . unwrap_or ( "Unknown" . to_string ( ) ) ;
10393 let id = serde_json:: to_string ( & device. id ( ) ) . unwrap ( ) ;
10494
10595 super :: commands:: AvailableDevice { label, id }
106- } )
107- . ok ( )
10896 } )
109- . collect ( ) ;
97+ . collect :: < Vec < _ > > ( )
98+ . await ;
11099
111- Ok ( candidates )
100+ Ok ( devices )
112101}
0 commit comments