Skip to content

Commit 0bbd3b1

Browse files
Fix model number error for Windows OS (#44)
1 parent 82d712b commit 0bbd3b1

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/bluetooth-device-wrapper.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class BluetoothDeviceWrapper {
106106
private disconnectPromise: Promise<unknown> | undefined;
107107
private connecting = false;
108108
private isReconnect = false;
109-
private reconnectReadyPromise: Promise<void> | undefined;
109+
private connectReadyPromise: Promise<void> | undefined;
110110

111111
private accelerometer = new ServiceInfo(AccelerometerService.createService, [
112112
"accelerometerdatachanged",
@@ -176,6 +176,15 @@ export class BluetoothDeviceWrapper {
176176
"BluetoothRemoteGATTServer for micro:bit device is undefined",
177177
);
178178
}
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+
179188
try {
180189
// A previous connect might have completed in the background as a device was replugged etc.
181190
await this.disconnectPromise;
@@ -282,21 +291,14 @@ export class BluetoothDeviceWrapper {
282291
this.disposeServices();
283292
this.duringExplicitConnectDisconnect--;
284293
}
285-
this.reconnectReadyPromise = new Promise((resolve) =>
294+
this.connectReadyPromise = new Promise((resolve) =>
286295
setTimeout(resolve, 3_500),
287296
);
288297
}
289298

290299
async reconnect(): Promise<void> {
291300
this.logging.log("Bluetooth reconnect");
292301
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-
}
300302
await this.connect();
301303
}
302304

lib/bluetooth.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ export class MicrobitWebBluetoothConnection
170170
return;
171171
}
172172
// TODO: timeout unification?
173-
// Connection happens inside createBluetoothDeviceWrapper.
174-
// await this.connection?.connect();
175-
this.setStatus(ConnectionStatus.CONNECTED);
176173
}
177174

178175
setNameFilter(name: string) {

0 commit comments

Comments
 (0)