Skip to content

Commit 8afe6df

Browse files
Use hasPrevBonded to infer isBonded for iOS
We have no way of knowing whether iOS isBonded or not so we need to remember it. For the scenario where the user forgets the micro:bit, ideally we would let flashing fail and set hasPrevBonded to false so that we can prepare to bond in the next try. However, currently, when we have let flashing fail (due to not waiting for disconnect before flashing when the micro:bit shows tick icon), the download progress hangs and due to the reconnecting logic, the micro:bit reconnects automatically.
1 parent 16f7605 commit 8afe6df

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/bluetooth-device-wrapper.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class BluetoothDeviceWrapper implements Logging {
110110
constructor(
111111
public readonly device: BleDevice,
112112
private logging: Logging = new ConsoleLogging(),
113+
private hasPrevBonded: boolean = false,
113114
dispatchTypedEvent: TypedServiceEventDispatcher,
114115
private currentEvents: () => Array<keyof ServiceConnectionEventMap>,
115116
private callbacks: ConnectCallbacks,
@@ -476,8 +477,8 @@ export class BluetoothDeviceWrapper implements Logging {
476477

477478
private async bondConnectDeviceInternal(): Promise<boolean> {
478479
const { deviceId } = this.device;
480+
let justBonded = false;
479481
if (isAndroid()) {
480-
let justBonded = false;
481482
// This gets us a nicer pairing dialog than just going straight for the characteristic.
482483
if (!(await BleClient.isBonded(deviceId))) {
483484
await BleClient.createBond(deviceId, { timeout: bondingTimeoutInMs });
@@ -493,11 +494,14 @@ export class BluetoothDeviceWrapper implements Logging {
493494
// need to call startNotifications again. We need to be connected to
494495
// startNotifications.
495496
await this.connectInternal();
496-
const pf = new PartialFlashingService(this);
497-
await pf.startNotifications({ timeout: bondingTimeoutInMs });
498-
// We just did it now to trigger pairing at a well defined point.
499-
await pf.stopNotifications();
500-
return true;
497+
if (!this.hasPrevBonded) {
498+
const pf = new PartialFlashingService(this);
499+
await pf.startNotifications({ timeout: bondingTimeoutInMs });
500+
// We just did it now to trigger pairing at a well defined point.
501+
await pf.stopNotifications();
502+
justBonded = true
503+
}
504+
return justBonded;
501505
}
502506
}
503507
}

lib/bluetooth.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ class MicrobitWebBluetoothConnectionImpl
292292
return this.connection?.boardVersion;
293293
}
294294

295+
private hasPrevBonded: boolean = false;
296+
295297
async connect(options?: ConnectOptions): Promise<void> {
296298
const progress = options?.progress ?? (() => {});
297299

@@ -306,11 +308,15 @@ class MicrobitWebBluetoothConnectionImpl
306308
this.connection = new BluetoothDeviceWrapper(
307309
device,
308310
this.logging,
311+
this.hasPrevBonded,
309312
this.dispatchTypedEvent.bind(this),
310313
() => this.getActiveEvents() as Array<keyof ServiceConnectionEventMap>,
311314
{
312315
onConnecting: () => this.setStatus(ConnectionStatus.CONNECTING),
313-
onSuccess: () => this.setStatus(ConnectionStatus.CONNECTED),
316+
onSuccess: () => {
317+
this.hasPrevBonded = true;
318+
this.setStatus(ConnectionStatus.CONNECTED);
319+
},
314320
onDisconnect: () => this.setStatus(ConnectionStatus.DISCONNECTED),
315321
},
316322
);

0 commit comments

Comments
 (0)