Skip to content

Commit 861e548

Browse files
Handle Android connection fail after flashing
- Attempt to connect twice before throwing error. - Throw immediately if disconnect occurs whilst connecting instead of waiting for timeout.
1 parent 55e26b8 commit 861e548

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/bluetooth-device-wrapper.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,10 @@ export class BluetoothDeviceWrapper implements Logging {
409409
options: {
410410
actionName?: string;
411411
timeout?: number;
412+
initiallyDisconnected?: boolean;
412413
} = {},
413414
): Promise<T> {
414-
if (!this.connected) {
415+
if (!this.connected && !options.initiallyDisconnected) {
415416
throw new DisconnectError();
416417
}
417418
const actionName = options.actionName ?? "action";
@@ -503,8 +504,25 @@ export class BluetoothDeviceWrapper implements Logging {
503504
await BleClient.createBond(deviceId, { timeout: bondingTimeoutInMs });
504505
justBonded = true;
505506
}
506-
await this.connectInternal();
507507

508+
// Connection after flashing fails. Subsequent attempt succeeds.
509+
const maxAttempts = 2;
510+
for (let i = 0; i < maxAttempts; i++) {
511+
try {
512+
// Fail immediately if disconnect occurs whilst connecting.
513+
await this.raceDisconnectAndTimeout(this.connectInternal(), {
514+
actionName: "bond connect device internal",
515+
timeout: connectTimeoutInMs,
516+
initiallyDisconnected: true,
517+
});
518+
} catch (error) {
519+
const numAttempts = i + 1;
520+
if (numAttempts === maxAttempts) {
521+
throw error;
522+
}
523+
console.log(`Attempt ${numAttempts} failed, retry...`);
524+
}
525+
}
508526
return justBonded;
509527
} else {
510528
// Long timeout as this is the point that the pairing dialog will show.

0 commit comments

Comments
 (0)