Refactor iOS glasses connection handling to shared code, fixing incompatible-version-detection and other error cases

This commit is contained in:
James Babcock committed 2026-10-03 15:08:37 -07:00
1 parent 9b0bf77d79
commit 66ca06f584
16 files changed
+341 -119

No files matched your search

+42 -16
View File
@@ -24,6 +24,8 @@ import { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events'
import { loadDeviceAddresses } from './device-addresses'
import { firmwareIncompatibilityMessage, hasCompatibleFirmware } from './firmware-compat'
import { isHaltedSessionPhase, suppressAutoReconnect } from './reconnect-policy'
import { deviceAddressError } from './ios-peripheral-identity'
import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app'
import { ALL_APPS } from '../apps/all-apps'
@@ -55,7 +57,9 @@ const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000
export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'
export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null;
ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string;
/** Why the connected firmware can't run Faceclaw (firmware-compat.ts), or '' when it can or is unknown. */
firmwareWarning: string }
function ancsCapable(capabilities: string): boolean {
return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION
@@ -72,7 +76,7 @@ export class IosPreviewController {
private communicator: FaceclawCommunicatorBridge | null = null
private readonly startedCommunicators = new WeakSet<FaceclawCommunicatorBridge>()
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
frames: 0, capabilities: '', leftVersion: '', rightVersion: '', firmwareWarning: '' }
private notifications: AncsClient | null = null
private readonly sessionOffs: (() => void)[] = []
private stopping: Promise<void> | null = null
@@ -81,6 +85,8 @@ export class IosPreviewController {
private connecting = false
private sessionCreated = false
private wearDetectionRequested = false
/** The last firmware report was Faceclaw's firmware at the required revision. */
private firmwareCompatible = false
private readonly glanceDisplay: GlanceDisplay = {
configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) },
setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() },
@@ -346,7 +352,7 @@ export class IosPreviewController {
if (!enabled) this.setGlassesLocked(false)
else {
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
if (this.communicator && this.state.phase === 'connected')
if (this.communicator && this.state.phase === 'connected' && this.firmwareCompatible)
void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error))
}
}
@@ -682,7 +688,8 @@ export class IosPreviewController {
this.connecting = true
this.sessionCreated = true
try {
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', firmwareWarning: '', battery: null, charging: null, ring: false }
this.firmwareCompatible = false
this.update('connecting', 'Finding configured devices…')
let identifiers
try { identifiers = await resolveIosPeripherals(addresses) }
@@ -735,10 +742,19 @@ export class IosPreviewController {
}),
communicator.onFirmwareInfo(info => {
if (this.communicator !== communicator) return
this.logBluetooth(`Firmware: L=${info.leftVersion || '?'} R=${info.rightVersion || '?'}` +
(info.extension ? ` ext="${info.extension}"` : ' (no firmware extension string)'))
// The shared session halts on incompatible firmware by itself ("incompatible-firmware"
// phase, handled in handleSessionState); this only records why, for the warning.
const firmwareWarning = firmwareIncompatibilityMessage(info) ?? ''
if (firmwareWarning && firmwareWarning !== this.state.firmwareWarning) this.logBluetooth(`Firmware compatibility warning: ${firmwareWarning}`)
this.firmwareCompatible = !firmwareWarning && hasCompatibleFirmware(info)
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities }
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities,
firmwareWarning }
if (this.active) this.onConnectionState({ ...this.state })
this.syncNotifications(communicator)
this.ensureWearDetection(communicator)
}),
communicator.onFrameMetrics(() => {
if (this.communicator !== communicator) return
@@ -766,21 +782,31 @@ export class IosPreviewController {
// Registering the listener makes the Kotlin session report its current state, which is
// "disconnected" before start(); that must not be mistaken for the session ending.
if (!this.startedCommunicators.has(communicator)) return
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase
if (phase === 'connected' && this.state.phase !== 'connected') {
this.state = { ...this.state, frames: this.state.frames }
if (!this.wearDetectionRequested) {
this.wearDetectionRequested = true
void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`))
}
}
// The shared session parked its retry loop (an arm unpaired, or firmware Faceclaw can't
// run): a redial would fail the same way, so stay down until the user connects explicitly
// (or re-pairs / installs the custom firmware, which lift the suppression).
if (isHaltedSessionPhase(state.phase)) suppressAutoReconnect()
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : isHaltedSessionPhase(state.phase) ? 'error' : state.phase
// Wear state is a per-transport snapshot: ask again on every new session.
if (phase !== 'connected') this.wearDetectionRequested = false
this.update(phase, state.status)
if (phase === 'connected') this.syncNotifications(communicator)
else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) {
// The session ended on its own (retries exhausted, an arm unpaired): release it and go headless.
if (phase === 'connected') {
this.syncNotifications(communicator)
this.ensureWearDetection(communicator)
} else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) {
// The session ended on its own (an arm unpaired, incompatible firmware): release it and go headless.
void this.disconnect(state.status)
}
}
/**
* Wear reports come from the custom firmware, so like Android only ask once the session
* is up and the firmware is confirmed compatible (stock firmware never hears the query).
*/
private ensureWearDetection(communicator: FaceclawCommunicatorBridge): void {
if (this.wearDetectionRequested || !this.firmwareCompatible || this.state.phase !== 'connected') return
this.wearDetectionRequested = true
void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`))
}
private handleRingEvent(input: RawInputEvent): void {
this.inputQueue = this.inputQueue.then(async () => {
if (this.state.phase !== 'connected') return