Fix reconnect-loop issue when device MAC address is configured but not paired

This commit is contained in:
James Babcock committed 2026-08-24 12:56:09 -07:00
1 parent 1a1416c8f0
commit 16d3b7c25f
4 files changed
+126 -3

No files matched your search

@@ -63,6 +63,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private volatile Thread workerThread; private volatile Thread workerThread;
private volatile boolean running; private volatile boolean running;
private volatile boolean userDisconnectRequested; 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 phase = "disconnected";
private String status = "Disconnected."; private String status = "Disconnected.";
@@ -239,6 +243,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
} }
running = true; running = true;
userDisconnectRequested = false; userDisconnectRequested = false;
reconnectHalted = false;
shutdownRequested = false; shutdownRequested = false;
activeInstance = this; activeInstance = this;
workerThread = new Thread(this, "FaceclawBleCommunicator"); workerThread = new Thread(this, "FaceclawBleCommunicator");
@@ -1118,6 +1123,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
} }
emitPhoneLockStateIfChanged(false); emitPhoneLockStateIfChanged(false);
if (!sessionReady) { if (!sessionReady) {
if (reconnectHalted) {
interruptibleSleep.sleep(ConnectionOptions.IDLE_SLEEP_MS);
continue;
}
long now = SystemClock.elapsedRealtime(); long now = SystemClock.elapsedRealtime();
if (now < reconnectAfterMs) { if (now < reconnectAfterMs) {
interruptibleSleep.sleep(Math.min(ConnectionOptions.IDLE_SLEEP_MS, reconnectAfterMs - now)); interruptibleSleep.sleep(Math.min(ConnectionOptions.IDLE_SLEEP_MS, reconnectAfterMs - now));
@@ -1365,13 +1374,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
audioPacketListener = null; audioPacketListener = null;
clearAllMessagesLocked("connection lost"); clearAllMessagesLocked("connection lost");
displayedFingerprint = ""; displayedFingerprint = "";
reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; if (!reconnectHalted) {
reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS;
}
} }
} }
interruptibleSleep.interrupt(); interruptibleSleep.interrupt();
if (connected) { if (connected) {
setStateDisplay("connected", "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..."); setStateDisplay("connecting", "Connecting to the glasses...");
} }
} }
@@ -1440,7 +1453,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
tryConnectRing("initial"); tryConnectRing("initial");
} catch (Throwable t) { } catch (Throwable t) {
logLine("connect failed: " + safeMessage(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); + " 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) { private void handleTransportFailure(String reason) {
Log.e(TAG, "Transport failure: "+reason); Log.e(TAG, "Transport failure: "+reason);
synchronized (lock) { synchronized (lock) {
@@ -2963,6 +3024,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
ringConnected = false; ringConnected = false;
ringNotificationsReady = false; ringNotificationsReady = false;
reconnectAfterMs = 0; reconnectAfterMs = 0;
reconnectHalted = false;
ringReconnectAfterMs = 0; ringReconnectAfterMs = 0;
lastAckAtMs = 0; lastAckAtMs = 0;
lastIncomingAtMs = 0; lastIncomingAtMs = 0;
@@ -64,6 +64,23 @@ public class FaceclawBleManager {
this.listener = listener; 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) { public boolean connect(String address, int timeoutMs) {
if (address == null || address.trim().isEmpty()) { if (address == null || address.trim().isEmpty()) {
throw new IllegalArgumentException("address is required"); throw new IllegalArgumentException("address is required");
+43
View File
@@ -220,6 +220,7 @@ class DashboardController {
// underneath it, so it waits for this to clear. // underneath it, so it waits for this to clear.
private connectRunning = false; private connectRunning = false;
private incompatibleDisconnectPending = false; private incompatibleDisconnectPending = false;
private unpairedDisconnectPending = false;
constructor() { constructor() {
const sharedActions = { const sharedActions = {
@@ -871,6 +872,14 @@ class DashboardController {
this.appendLog(line); this.appendLog(line);
}); });
this.offState = communicator.onStateChange((state) => { 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 = const mappedPhase =
state.phase === "connected" state.phase === "connected"
? "connected" ? "connected"
@@ -1132,6 +1141,40 @@ class DashboardController {
setTimeout(attempt, 0); 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 * Tear down the connection and enter the manual-disconnected state: every
* caller is deliberate (the phone/glasses Disconnect actions, entering the * caller is deliberate (the phone/glasses Disconnect actions, entering the
+1
View File
@@ -11,6 +11,7 @@ export type CommunicatorPhase =
| "connected" | "connected"
| "charging" | "charging"
| "retrying" | "retrying"
| "unpaired"
| "disconnecting"; | "disconnecting";
export type CommunicatorState = { export type CommunicatorState = {