Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix model number error for Windows OS
  • Loading branch information
microbit-robert committed Nov 29, 2024
commit 5fcaa4afac64a22e54b652dd82960d73a61ca47f
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