G1 support: Flashing within the Android app

This commit is contained in:
James Babcock committed 2026-11-06 12:55:24 -08:00
1 parent 345ba36c33
commit 38c52ebc31
37 files changed
+2819 -32

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
import { Application } from '@nativescript/core'
import { parseG1FlashResult, type G1FlashResult } from '../g1/flash-plan'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { G1FlasherOptions } from './g1-flasher'
export type { G1FlashMode, G1FlasherOptions } from './g1-flasher'
declare const FaceclawKitIosG1Flasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
/**
* iOS twin of the Android G1Flasher: the shared Kotlin G1ArmFlashFlow over a CoreBluetooth central. As with the G2's
* flasher, the phone must stay in the foreground with the screen awake, and addresses are mapped to peripheral
* identifiers first.
*/
export class G1Flasher {
private flasher: any = null
private listenerProxy: object | null = null
private logListener: ((line: string) => void) | null = null
private progressListener: ((bytesSent: number, bytesTotal: number) => void) | null = null
private stateListener: ((state: string) => void) | null = null
private previousIdle = false
private cancelled = false
private foregroundError = ''
constructor(private readonly options: G1FlasherOptions) {}
onLog(listener: (line: string) => void): void { this.logListener = listener }
onProgress(listener: (bytesSent: number, bytesTotal: number) => void): void { this.progressListener = listener }
onState(listener: (state: string) => void): void { this.stateListener = listener }
private readonly backgrounded = () => {
this.foregroundError = 'The install stopped because Faceclaw left the foreground. Return to Faceclaw and try again.'
this.cancel()
}
run(): Promise<G1FlashResult> {
this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
UIApplication.sharedApplication.idleTimerDisabled = true
Application.on(Application.suspendEvent, this.backgrounded)
const restore = () => {
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
}
return new Promise<G1FlashResult>((resolve, reject) => {
const identifier = iosBluetooth().peripheralIdentifier(this.options.address, this.options.side)
if (!identifier) throw new Error(`Could not find the ${this.options.side} arm. Keep the glasses in the case with the lid open.`)
this.flasher = FaceclawKitIosG1Flasher.alloc().initWithAddressSideFirmwarePathModeConnectWindowMs(
identifier, this.options.side, this.options.firmwarePath, this.options.mode, this.options.connectWindowMs)
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
onLogLine: (line: string) => this.logListener?.(String(line ?? '')),
onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
_lens: string, _componentIndex: number, _componentCount: number, _blockIndex: number, _blockCount: number, sent: number, total: number,
) => this.progressListener?.(Number(sent), Number(total)),
onStateStateDetail: (state: string) => this.stateListener?.(String(state)),
onCompleteSuccessDetail: (success: boolean, detail: string) => {
if (success) resolve(parseG1FlashResult(String(detail ?? '')))
else reject(new Error(this.foregroundError || String(detail ?? '') || 'The install failed.'))
},
})
this.flasher.setListenerListener(this.listenerProxy)
if (this.cancelled) this.flasher.cancel()
this.flasher.start()
}).finally(restore)
}
cancel(): void {
this.cancelled = true
try { this.flasher?.cancel() } catch { /* ignore */ }
}
close(): void {
this.cancel()
try { this.flasher?.close() } catch { /* ignore */ }
}
}
+73
View File
@@ -0,0 +1,73 @@
import { Utils } from "@nativescript/core";
import { parseG1FlashResult, type G1FlashResult, type G1Side } from "../g1/flash-plan";
declare const com: any;
export type G1FlashMode = "stock" | "update" | "probe";
export type G1FlasherOptions = {
address: string;
side: G1Side;
firmwarePath: string;
mode: G1FlashMode;
/** How long to keep trying to reach the arm and pair with it. */
connectWindowMs: number;
};
/** TS wrapper around FaceclawG1Flasher: one arm's install, or a question about what it runs. */
export class G1Flasher {
private readonly flasher: any;
private readonly listenerProxy: any;
private logListener: ((line: string) => void) | null = null;
private progressListener: ((bytesSent: number, bytesTotal: number) => void) | null = null;
private stateListener: ((state: string) => void) | null = null;
private completion: ((success: boolean, detail: string) => void) | null = null;
constructor(options: G1FlasherOptions) {
const context = Utils.android.getApplicationContext();
if (!context) throw new Error("Android application context unavailable");
this.flasher = new com.faceclaw.app.FaceclawG1Flasher(
context, options.address, options.side, options.firmwarePath, options.mode, options.connectWindowMs,
);
this.listenerProxy = new com.faceclaw.app.FaceclawFirmwareFlasherListener({
onLog: (line: string) => this.logListener?.(String(line)),
onProgress: (_lens: string, _component: number, _components: number, _block: number, _blocks: number, sent: number, total: number) =>
this.progressListener?.(Number(sent), Number(total)),
onState: (state: string) => this.stateListener?.(String(state)),
onComplete: (success: boolean, detail: string) => this.completion?.(Boolean(success), String(detail ?? "")),
});
this.flasher.setListener(this.listenerProxy);
}
onLog(listener: (line: string) => void): void {
this.logListener = listener;
}
onProgress(listener: (bytesSent: number, bytesTotal: number) => void): void {
this.progressListener = listener;
}
onState(listener: (state: string) => void): void {
this.stateListener = listener;
}
run(): Promise<G1FlashResult> {
return new Promise((resolve, reject) => {
this.completion = (success, detail) => {
this.completion = null;
if (success) resolve(parseG1FlashResult(detail));
else reject(new Error(detail || "The install failed."));
};
this.flasher.start();
});
}
cancel(): void {
this.flasher.cancel();
}
close(): void {
this.flasher.close();
}
}
+9
View File
@@ -129,6 +129,15 @@ export class IosBluetooth {
})
return result
}
/** The peripheral last heard at this address, if it was on this side. */
peripheralIdentifier(address: string, role: string): string | null {
try {
const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}'))
return saved.role === role && typeof saved.identifier === 'string' ? saved.identifier : null
} catch {
return null
}
}
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
}
let instance: IosBluetooth | null = null