Skip to content

Fix model number error for Windows OS #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 0 additions & 3 deletions lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ export class MicrobitWebBluetoothConnection
return;
}
// TODO: timeout unification?
// Connection happens inside createBluetoothDeviceWrapper.
// await this.connection?.connect();
this.setStatus(ConnectionStatus.CONNECTED);
}

setNameFilter(name: string) {
Expand Down
Loading