From 16d3b7c25f8134a1bfe3cb8c310fa2337caf0d7a Mon Sep 17 00:00:00 2001 From: James Babcock Date: Mon, 24 Aug 2026 12:56:09 -0700 Subject: [PATCH] Fix reconnect-loop issue when device MAC address is configured but not paired --- .../faceclaw/app/FaceclawBleCommunicator.java | 68 ++++++++++++++++++- .../com/faceclaw/app/FaceclawBleManager.java | 17 +++++ app/g2/dashboard-controller.ts | 43 ++++++++++++ app/native/faceclaw-communicator.ts | 1 + 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index f1e2002c..9f6a806d 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -63,6 +63,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private volatile Thread workerThread; private volatile boolean running; private volatile boolean userDisconnectRequested; + // Set when a connect attempt failed while an arm's Android bond is gone: + // retrying is pointless until the user re-pairs, so the worker loop parks + // instead of redialing. Cleared by start() (a fresh explicit connect). + private volatile boolean reconnectHalted; private String phase = "disconnected"; private String status = "Disconnected."; @@ -239,6 +243,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } running = true; userDisconnectRequested = false; + reconnectHalted = false; shutdownRequested = false; activeInstance = this; workerThread = new Thread(this, "FaceclawBleCommunicator"); @@ -1118,6 +1123,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } emitPhoneLockStateIfChanged(false); if (!sessionReady) { + if (reconnectHalted) { + interruptibleSleep.sleep(ConnectionOptions.IDLE_SLEEP_MS); + continue; + } long now = SystemClock.elapsedRealtime(); if (now < reconnectAfterMs) { interruptibleSleep.sleep(Math.min(ConnectionOptions.IDLE_SLEEP_MS, reconnectAfterMs - now)); @@ -1365,13 +1374,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { audioPacketListener = null; clearAllMessagesLocked("connection lost"); displayedFingerprint = ""; - reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; + if (!reconnectHalted) { + reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; + } } } interruptibleSleep.interrupt(); if (connected) { setStateDisplay("connected", "Connected."); - } else { + } else if (!reconnectHalted) { + // While parked on a missing bond, keep the "unpaired" display: this + // callback is just the teardown of the arm that did connect. setStateDisplay("connecting", "Connecting to the glasses..."); } } @@ -1440,7 +1453,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { tryConnectRing("initial"); } catch (Throwable t) { logLine("connect failed: " + safeMessage(t)); - handleTransportFailure("connect failed"); + String unpairedArm = firstUnpairedArm(); + if (unpairedArm != null) { + handleUnpairedFailure(unpairedArm); + } else { + handleTransportFailure("connect failed"); + } } } @@ -2928,6 +2946,49 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { + " release=" + previous.reason); } + /** The address of a configured arm Android no longer holds a bond for, or null. */ + private String firstUnpairedArm() { + if (!bleManager.isBonded(rightAddress)) return rightAddress; + if (!bleManager.isBonded(leftAddress)) return leftAddress; + return null; + } + + /** + * A connect failure while an arm's bond is missing (the pairing was + * forgotten in Android settings, or the address was typed in by hand and + * never paired) will repeat forever, so instead of scheduling a retry, + * park the worker loop and tell the user to re-pair. Only an explicit + * connect (which builds a fresh communicator) starts a new attempt. + */ + private void handleUnpairedFailure(String address) { + Log.e(TAG, "Connect failed and " + address + " is not paired; suspending reconnect"); + synchronized (lock) { + reconnectHalted = true; + sessionReady = false; + fixedLayoutCreated = false; + startupProbePending = false; + shutdownRequested = false; + chargingMode = false; + imageRetryAfterMs = 0; + displayedFingerprint = ""; + faceclawWakePendingNonce = -1; + lastFaceclawWakeLeaseQueuedAtMs = 0; + faceclawWakeControlSentCount = 0; + clearAllMessagesLocked("arm not paired: " + address); + reconnectAfterMs = Long.MAX_VALUE; + bleManager.disconnect(rightAddress); + bleManager.disconnect(leftAddress); + } + if (!userDisconnectRequested) { + setStateDisplay( + "unpaired", + "The glasses (" + address + ") are not paired with this phone." + + " Use \"Pair glasses\" to pair them again, then connect." + ); + } + interruptibleSleep.interrupt(); + } + private void handleTransportFailure(String reason) { Log.e(TAG, "Transport failure: "+reason); synchronized (lock) { @@ -2963,6 +3024,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { ringConnected = false; ringNotificationsReady = false; reconnectAfterMs = 0; + reconnectHalted = false; ringReconnectAfterMs = 0; lastAckAtMs = 0; lastIncomingAtMs = 0; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java index 69e54a61..179f6e04 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleManager.java @@ -64,6 +64,23 @@ public class FaceclawBleManager { this.listener = listener; } + /** + * Whether Android still holds a bond (pairing) for this address. Returns + * true when the state cannot be determined, so callers only act on a + * definite BOND_NONE. + */ + public boolean isBonded(String address) { + if (address == null || address.trim().isEmpty()) { + return true; + } + try { + BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); + return device == null || device.getBondState() != BluetoothDevice.BOND_NONE; + } catch (Throwable t) { + return true; + } + } + public boolean connect(String address, int timeoutMs) { if (address == null || address.trim().isEmpty()) { throw new IllegalArgumentException("address is required"); diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index c1bee7a3..687b2232 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -220,6 +220,7 @@ class DashboardController { // underneath it, so it waits for this to clear. private connectRunning = false; private incompatibleDisconnectPending = false; + private unpairedDisconnectPending = false; constructor() { const sharedActions = { @@ -871,6 +872,14 @@ class DashboardController { this.appendLog(line); }); this.offState = communicator.onStateChange((state) => { + if (state.phase === "unpaired") { + // Java parked its retry loop: an arm's Android bond is gone, so + // every redial would fail the same way until the user re-pairs. + // Tear down into the manual-disconnected state and keep the + // re-pair instruction as the visible status. + this.scheduleUnpairedDisconnect(state.status); + return; + } const mappedPhase = state.phase === "connected" ? "connected" @@ -1132,6 +1141,40 @@ class DashboardController { setTimeout(attempt, 0); } + /** + * A connect attempt found an arm whose Android bond is missing, so the Java + * worker stopped retrying. Drop into the manual-disconnected state (no + * auto-reconnect: it would just fail again) and leave the re-pair + * instruction from Java as the status the user sees. + */ + private scheduleUnpairedDisconnect(message: string): void { + if (this.unpairedDisconnectPending) return; + this.unpairedDisconnectPending = true; + const attempt = () => { + // The unpaired report can arrive while connect() is still mid-flight; + // let it finish so the teardown doesn't race its surface setup. + if (this.connectRunning) { + setTimeout(attempt, 200); + return; + } + this.unpairedDisconnectPending = false; + if (this.phase === "disconnected" || this.phase === "disconnecting") { + // The session ended some other way; still stop auto-reconnect from + // re-dialing glasses that are no longer paired. + suppressAutoReconnect(); + this.setStatus(message); + return; + } + this.appendLog(`Disconnecting: ${message} Auto-reconnect is disabled until you connect manually.`); + void this.disconnect({ skipFirmwareCleanup: true }) + .then(() => this.setStatus(message)) + .catch((error) => { + this.appendLog(`unpaired disconnect failed: ${this.formatError(error)}`); + }); + }; + setTimeout(attempt, 0); + } + /** * Tear down the connection and enter the manual-disconnected state: every * caller is deliberate (the phone/glasses Disconnect actions, entering the diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 4cfef948..3bfc2351 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -11,6 +11,7 @@ export type CommunicatorPhase = | "connected" | "charging" | "retrying" + | "unpaired" | "disconnecting"; export type CommunicatorState = {