Skip to content
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
31 changes: 24 additions & 7 deletions packages/microbit-connection/src/bluetooth-device-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ import {
DeviceBondState,
} from "./device-bond-state.js";

/**
* The @capacitor-community/bluetooth-le plugin throws this message when
* a characteristic UUID is not found in the device's GATT table.
*/
export const isCharacteristicNotFoundError = (e: unknown): boolean =>
e instanceof Error && e.message === "Characteristic not found.";

export const bondingTimeoutInMs = 40_000;
export const connectTimeoutInMs = 10_000;
export const scanningTimeoutInMs = 10_000;
Expand Down Expand Up @@ -479,14 +486,24 @@ export class BluetoothDeviceWrapper implements Logging {

await this.connectInternal();

progress(ProgressStage.ResettingDevice);
this.log("Resetting to pairing mode");
const pf = new PartialFlashingService(this);
await pf.resetToMode(MicroBitMode.Pairing);
await this.waitForDisconnect(10_000);
try {
progress(ProgressStage.ResettingDevice);
this.log("Resetting to pairing mode");
const pf = new PartialFlashingService(this);
await pf.resetToMode(MicroBitMode.Pairing);
await this.waitForDisconnect(10_000);

progress(ProgressStage.Connecting);
await this.connectInternal();
progress(ProgressStage.Connecting);
await this.connectInternal();
} catch (e) {
if (isCharacteristicNotFoundError(e)) {
this.log(
"Partial flashing characteristic not found, skipping reset.",
);
} else {
throw e;
}
}
}
this.log(`Connection ready; took ${Date.now() - startTime}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import MemoryMap from "nrf-intel-hex";
import { BluetoothDeviceWrapper } from "../bluetooth-device-wrapper.js";
import {
BluetoothDeviceWrapper,
isCharacteristicNotFoundError,
} from "../bluetooth-device-wrapper.js";
import {
MicroBitMode,
PacketState,
Expand Down Expand Up @@ -47,6 +50,9 @@ const partialFlash = async (
);
} catch (e) {
connection.error("Partial flash failed", e);
if (isCharacteristicNotFoundError(e)) {
return PartialFlashResult.AttemptFullFlash;
}
if (
// Error thrown in iOS only for when user cancels the pairing dialog.
e instanceof Error &&
Expand Down