@@ -162,29 +162,29 @@ async fn power_up_and_wait_for_connection(
162162
163163 // try to connect to saved devices or provided one via command line
164164 let mut connect_task: Option < JoinHandle < Result < ( ) > > > = None ;
165- if !dongle_mode {
166- if let Some ( address ) = connect {
167- let adapter_cloned = adapter . clone ( ) ;
168-
169- connect_task = Some ( tokio :: spawn ( async move {
170- let addresses = if address == Address :: any ( ) {
171- info ! ( "{} 🥏 Enumerating known bluetooth devices..." , NAME ) ;
172- adapter_cloned . device_addresses ( ) . await ?
173- } else {
174- vec ! [ address ]
175- } ;
176- // exit if we don't have anything to connect to
177- if addresses . is_empty ( ) {
178- return Ok ( ( ) ) ;
179- }
180- loop {
181- for addr in & addresses {
182- let device = adapter_cloned . device ( * addr ) ? ;
183- let dev_name = match device . name ( ) . await {
184- Ok ( Some ( name ) ) => format ! ( " (<b><blue>{}</>)" , name ) ,
185- _ => String :: default ( ) ,
186- } ;
187- info ! ( "{} 🧲 Trying to connect to: {}{}" , NAME , addr , dev_name ) ;
165+ if let Some ( address ) = connect {
166+ let adapter_cloned = adapter . clone ( ) ;
167+
168+ connect_task = Some ( tokio :: spawn ( async move {
169+ let addresses = if address == Address :: any ( ) {
170+ info ! ( "{} 🥏 Enumerating known bluetooth devices..." , NAME ) ;
171+ adapter_cloned . device_addresses ( ) . await ?
172+ } else {
173+ vec ! [ address ]
174+ } ;
175+ // exit if we don't have anything to connect to
176+ if addresses . is_empty ( ) {
177+ return Ok ( ( ) ) ;
178+ }
179+ loop {
180+ for addr in & addresses {
181+ let device = adapter_cloned . device ( * addr ) ? ;
182+ let dev_name = match device. name ( ) . await {
183+ Ok ( Some ( name ) ) => format ! ( " (<b><blue>{}</>)" , name ) ,
184+ _ => String :: default ( ) ,
185+ } ;
186+ info ! ( "{} 🧲 Trying to connect to: {}{}" , NAME , addr , dev_name ) ;
187+ if !dongle_mode {
188188 match device. connect_profile ( & HSP_AG_UUID ) . await {
189189 Ok ( _) => {
190190 info ! (
@@ -197,11 +197,49 @@ async fn power_up_and_wait_for_connection(
197197 warn ! ( "{} 🔇 {}{}: Error connecting: {}" , NAME , addr, dev_name, e)
198198 }
199199 }
200+ } else {
201+ match device. connect ( ) . await {
202+ Ok ( _) => {
203+ info ! (
204+ "{} 🔗 Successfully connected to device: {}{}" ,
205+ NAME , addr, dev_name
206+ ) ;
207+ return Ok ( ( ) ) ;
208+ }
209+ Err ( e) => {
210+ // should be handled with the following code:
211+ // match e.kind {bluer::ErrorKind::ConnectionAttemptFailed} ...
212+ // but the problem is that not all errors are defined in bluer,
213+ // so just fallback for text-searching in error :(
214+ let error_text = e. to_string ( ) ;
215+
216+ if let Some ( code) =
217+ error_text. splitn ( 2 , ':' ) . nth ( 1 ) . map ( |s| s. trim ( ) )
218+ {
219+ if code == "br-connection-page-timeout"
220+ || code == "br-connection-canceled"
221+ {
222+ warn ! (
223+ "{} 🔇 {}{}: Error connecting: {}" ,
224+ NAME , addr, dev_name, e
225+ )
226+ } else {
227+ info ! (
228+ "{} 🔗 Connection success, waiting for AA profile connection: {}{}, ignored error: {}" ,
229+ NAME , addr, dev_name, e
230+ ) ;
231+ return Ok ( ( ) ) ;
232+ }
233+ } else {
234+ warn ! ( "{} Unknown bluetooth error: {}" , NAME , e) ;
235+ }
236+ }
237+ }
200238 }
201- sleep ( Duration :: from_secs ( 1 ) ) . await ;
202239 }
203- } ) ) ;
204- }
240+ sleep ( Duration :: from_secs ( 1 ) ) . await ;
241+ }
242+ } ) ) ;
205243 }
206244
207245 // handling connection to headset profile in own task
0 commit comments