Skip to content

Commit

Permalink
Fix model number error for Windows OS
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert committed Nov 29, 2024
1 parent 82d712b commit 5fcaa4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/bluetooth-device-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class BluetoothDeviceWrapper {
private disconnectPromise: Promise<unknown> | undefined;
private connecting = false;
private isReconnect = false;
private reconnectReadyPromise: Promise<void> | undefined;
private connectReadyPromise: Promise<void> | undefined;

private accelerometer = new ServiceInfo(AccelerometerService.createService, [
"accelerometerdatachanged",
Expand Down Expand Up @@ -176,6 +176,15 @@ export class BluetoothDeviceWrapper {
"BluetoothRemoteGATTServer for micro:bit device is undefined",
);
}

if (isWindowsOS) {
// On Windows, the micro:bit can take around 3 seconds to respond to gatt.disconnect().
// Attempting to connect/reconnect before the micro:bit has responded results in another
// gattserverdisconnected event being fired. We then fail to get primaryService on a
// disconnected GATT server.
await this.connectReadyPromise;
}

try {
// A previous connect might have completed in the background as a device was replugged etc.
await this.disconnectPromise;
Expand Down Expand Up @@ -282,21 +291,14 @@ export class BluetoothDeviceWrapper {
this.disposeServices();
this.duringExplicitConnectDisconnect--;
}
this.reconnectReadyPromise = new Promise((resolve) =>
this.connectReadyPromise = new Promise((resolve) =>
setTimeout(resolve, 3_500),
);
}

async reconnect(): Promise<void> {
this.logging.log("Bluetooth reconnect");
this.isReconnect = true;
if (isWindowsOS) {
// On Windows, the micro:bit can take around 3 seconds to respond to gatt.disconnect().
// Attempting to reconnect before the micro:bit has responded results in another
// gattserverdisconnected event being fired. We then fail to get primaryService on a
// disconnected GATT server.
await this.reconnectReadyPromise;
}
await this.connect();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class MicrobitWebBluetoothConnection
// TODO: timeout unification?
// Connection happens inside createBluetoothDeviceWrapper.
// await this.connection?.connect();
this.setStatus(ConnectionStatus.CONNECTED);
// this.setStatus(ConnectionStatus.CONNECTED);
}

setNameFilter(name: string) {
Expand Down

0 comments on commit 5fcaa4a

Please sign in to comment.