@@ -35,6 +35,8 @@ export class MicrobitSerial implements MicrobitConnection {
3535 private connectionCheckIntervalId : ReturnType < typeof setInterval > | undefined ;
3636 private lastReceivedMessageTimestamp : number | undefined ;
3737 private isReconnect : boolean = false ;
38+ // Whether this is the final reconnection attempt.
39+ private finalAttempt = false ;
3840
3941 constructor (
4042 private usb : MicrobitUSB ,
@@ -174,6 +176,7 @@ export class MicrobitSerial implements MicrobitConnection {
174176 await this . disconnectInternal ( false , reconnectHelp ) ;
175177 throw e ;
176178 } finally {
179+ this . finalAttempt = false ;
177180 this . isConnecting = false ;
178181 }
179182 }
@@ -202,7 +205,11 @@ export class MicrobitSerial implements MicrobitConnection {
202205 await this . usb . stopSerial ( ) ;
203206 stateOnDisconnected (
204207 DeviceRequestStates . INPUT ,
205- userDisconnect ? false : this . isReconnect ? 'autoReconnect' : 'connect' ,
208+ userDisconnect || this . finalAttempt
209+ ? false
210+ : this . isReconnect
211+ ? 'autoReconnect'
212+ : 'connect' ,
206213 reconnectHelp ,
207214 ) ;
208215 }
@@ -226,7 +233,8 @@ export class MicrobitSerial implements MicrobitConnection {
226233 }
227234 }
228235
229- async reconnect ( ) : Promise < void > {
236+ async reconnect ( finalAttempt : boolean = false ) : Promise < void > {
237+ this . finalAttempt = finalAttempt ;
230238 logMessage ( 'Serial reconnect' ) ;
231239 this . isReconnect = true ;
232240 await this . connect ( DeviceRequestStates . INPUT ) ;
0 commit comments