File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed
Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -409,9 +409,10 @@ export class BluetoothDeviceWrapper implements Logging {
409409 options : {
410410 actionName ?: string ;
411411 timeout ?: number ;
412+ initiallyDisconnected ?: boolean ;
412413 } = { } ,
413414 ) : Promise < T > {
414- if ( ! this . connected ) {
415+ if ( ! this . connected && ! options . initiallyDisconnected ) {
415416 throw new DisconnectError ( ) ;
416417 }
417418 const actionName = options . actionName ?? "action" ;
@@ -503,8 +504,25 @@ export class BluetoothDeviceWrapper implements Logging {
503504 await BleClient . createBond ( deviceId , { timeout : bondingTimeoutInMs } ) ;
504505 justBonded = true ;
505506 }
506- await this . connectInternal ( ) ;
507507
508+ // Connection after flashing fails. Subsequent attempt succeeds.
509+ const maxAttempts = 2 ;
510+ for ( let i = 0 ; i < maxAttempts ; i ++ ) {
511+ try {
512+ // Fail immediately if disconnect occurs whilst connecting.
513+ await this . raceDisconnectAndTimeout ( this . connectInternal ( ) , {
514+ actionName : "bond connect device internal" ,
515+ timeout : connectTimeoutInMs ,
516+ initiallyDisconnected : true ,
517+ } ) ;
518+ } catch ( error ) {
519+ const numAttempts = i + 1 ;
520+ if ( numAttempts === maxAttempts ) {
521+ throw error ;
522+ }
523+ console . log ( `Attempt ${ numAttempts } failed, retry...` ) ;
524+ }
525+ }
508526 return justBonded ;
509527 } else {
510528 // Long timeout as this is the point that the pairing dialog will show.
You can’t perform that action at this time.
0 commit comments