@@ -106,7 +106,7 @@ export class BluetoothDeviceWrapper {
106
106
private disconnectPromise : Promise < unknown > | undefined ;
107
107
private connecting = false ;
108
108
private isReconnect = false ;
109
- private reconnectReadyPromise : Promise < void > | undefined ;
109
+ private connectReadyPromise : Promise < void > | undefined ;
110
110
111
111
private accelerometer = new ServiceInfo ( AccelerometerService . createService , [
112
112
"accelerometerdatachanged" ,
@@ -176,6 +176,15 @@ export class BluetoothDeviceWrapper {
176
176
"BluetoothRemoteGATTServer for micro:bit device is undefined" ,
177
177
) ;
178
178
}
179
+
180
+ if ( isWindowsOS ) {
181
+ // On Windows, the micro:bit can take around 3 seconds to respond to gatt.disconnect().
182
+ // Attempting to connect/reconnect before the micro:bit has responded results in another
183
+ // gattserverdisconnected event being fired. We then fail to get primaryService on a
184
+ // disconnected GATT server.
185
+ await this . connectReadyPromise ;
186
+ }
187
+
179
188
try {
180
189
// A previous connect might have completed in the background as a device was replugged etc.
181
190
await this . disconnectPromise ;
@@ -282,21 +291,14 @@ export class BluetoothDeviceWrapper {
282
291
this . disposeServices ( ) ;
283
292
this . duringExplicitConnectDisconnect -- ;
284
293
}
285
- this . reconnectReadyPromise = new Promise ( ( resolve ) =>
294
+ this . connectReadyPromise = new Promise ( ( resolve ) =>
286
295
setTimeout ( resolve , 3_500 ) ,
287
296
) ;
288
297
}
289
298
290
299
async reconnect ( ) : Promise < void > {
291
300
this . logging . log ( "Bluetooth reconnect" ) ;
292
301
this . isReconnect = true ;
293
- if ( isWindowsOS ) {
294
- // On Windows, the micro:bit can take around 3 seconds to respond to gatt.disconnect().
295
- // Attempting to reconnect before the micro:bit has responded results in another
296
- // gattserverdisconnected event being fired. We then fail to get primaryService on a
297
- // disconnected GATT server.
298
- await this . reconnectReadyPromise ;
299
- }
300
302
await this . connect ( ) ;
301
303
}
302
304
0 commit comments