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 56f4f729..c4ef1fe5 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 @@ -99,6 +99,29 @@ public class FaceclawBleManager { } } + /** + * Android's bond state for this address (BluetoothDevice.BOND_NONE / + * BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined. + * BOND_BONDING covers the whole OS pairing flow, including a pairing + * dialog that is still waiting for the user. + */ + public int getBondState(String address) { + if (address == null || address.trim().isEmpty()) { + return -1; + } + try { + BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); + return device == null ? -1 : device.getBondState(); + } catch (Throwable t) { + return -1; + } + } + + /** Whether a GATT client for this address is open, i.e. no disconnect callback has arrived for it. */ + public boolean isConnected(String address) { + return address != null && gattClients.containsKey(address); + } + public boolean connect(String address, int timeoutMs) { if (address == null || address.trim().isEmpty()) { throw new IllegalArgumentException("address is required"); diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java index f39fcb20..c4ad3efe 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceInfoProbe.java @@ -1,12 +1,16 @@ package com.faceclaw.app; +import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.content.Context; import android.os.Handler; import android.os.Looper; +import android.os.SystemClock; import android.util.Log; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -20,6 +24,18 @@ import java.util.concurrent.TimeUnit; public class FaceclawDeviceInfoProbe implements FaceclawBleListener { private static final String TAG = "FaceclawDeviceInfo"; private static final int QUERY_TIMEOUT_MS = 4_000; + /** Connect + auth attempts per arm; a link that drops mid-pairing is retried once the bond settles. */ + private static final int ARM_ATTEMPTS = 3; + private static final int ARM_RETRY_DELAY_MS = 1_000; + /** + * Upper bound on waiting for the auth success while Android reports the arm + * as BOND_BONDING — i.e. an OS pairing dialog may be sitting there waiting + * for the user, which can take a lot longer than the normal 30 s window. + */ + private static final int PAIRING_WAIT_CAP_MS = 90_000; + /** After a fresh bond, how long the firmware gets to notify success on its own before the request is re-sent. */ + private static final int POST_BOND_GRACE_MS = 2_000; + private static final int POLL_MS = 250; private final Context context; private final String rightAddress; @@ -36,6 +52,17 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { private byte[] awaitPb = null; private CountDownLatch awaitLatch = null; + /** + * Security-auth wait, kept separate from the generic ack wait: only the + * firmware's SUCCESS result (empty result message) may satisfy it. The + * firmware answers the request as soon as it arrives, but with a + * non-success result until the link is encrypted; accepting that first + * reply as "the ack" is what let the probe move on to the other arm while + * Android's pairing dialog for this one was still up. + */ + private final Set authMagics = new HashSet<>(); + private CountDownLatch authLatch = null; + /** * A sid-0x09 frame that carried firmware versions without matching the * awaited magic — some firmware pushes the settings snapshot on a magic it @@ -92,32 +119,49 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { return; } - // Pair both arms before asking for versions, even when the right - // lens answers immediately and no left-lens fallback is needed. - boolean rightAuthenticated = connectAndAuthenticateArm(rightAddress, "right"); - boolean leftReady = false; + boolean haveLeft = !leftAddress.trim().isEmpty() && !leftAddress.equalsIgnoreCase(rightAddress); + + // Bring up and authenticate BOTH arms before asking for versions. + // Each arm is its own peripheral with its own bond, so on a fresh + // phone each raises its own pairing prompt; completing both here + // means the flash step (and the app proper) find both bonds in + // place. Strictly one arm at a time: Android pairs with one device + // at a time, and a second arm connected while the first was still + // pairing has been seen to drop within a second and never pair + // (2026-09-11 logcat). + boolean rightAuthenticated = bringUpArm(rightAddress, "right"); boolean leftAuthenticated = false; - if (!cancelled && !leftAddress.trim().isEmpty() - && !leftAddress.equalsIgnoreCase(rightAddress)) { - try { - leftAuthenticated = connectAndAuthenticateArm(leftAddress, "left"); - leftReady = !cancelled; - } catch (Exception e) { - emitLog("left-lens connection/auth failed: " - + (e.getMessage() == null ? e.toString() : e.getMessage())); - } + if (haveLeft && !cancelled) { + leftAuthenticated = bringUpArm(leftAddress, "left"); } if (cancelled) { emitError("Cancelled."); return; } + if (!bleManager.isConnected(rightAddress)) { + // The right lens may have dropped while the left one paired; + // it is bonded by now, so this comes back quickly. + emitLog("right lens disconnected while the left lens was set up; reconnecting"); + rightAuthenticated = bringUpArm(rightAddress, "right"); + if (cancelled) { + emitError("Cancelled."); + return; + } + } - byte[] ack = queryArm(rightAddress, "right", rightAuthenticated); + byte[] ack = null; + String rightFailure = null; + try { + ack = queryArm(rightAddress, "right", rightAuthenticated); + } catch (IllegalStateException e) { + rightFailure = e.getMessage() == null ? e.toString() : e.getMessage(); + emitLog("right-lens query failed: " + rightFailure); + } // The right lens is the documented control endpoint, but a silent // right lens has been observed on stock 2.2.9 even after a // successful security auth — run the same probe against the left // lens rather than giving up, and log which lens answered. - if (ack == null && !cancelled && leftReady) { + if (ack == null && !cancelled && haveLeft) { emitLog("right lens did not answer the settings query; probing the left lens"); try { ack = queryArm(leftAddress, "left", leftAuthenticated); @@ -130,8 +174,9 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { return; } if (ack == null) { - throw new IllegalStateException( - "no response to the device-info query on either lens — check `adb logcat -s " + throw new IllegalStateException(rightFailure != null + ? rightFailure + : "no response to the device-info query on either lens — check `adb logcat -s " + TAG + "` for the frame trace"); } @@ -152,21 +197,207 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { } } - private boolean connectAndAuthenticateArm(String address, String label) throws InterruptedException { - emitState("connecting", label); - connectArm(address); + /** + * Connect to one arm and complete its security-auth exchange, retrying + * when the link drops mid-pairing. Returns whether the auth success was + * seen (false = connected but unconfirmed, tolerated because the custom + * firmware's response to the exchange is not yet hardware-verified); + * throws when the arm cannot be brought up at all. + */ + private boolean bringUpArm(String address, String label) throws InterruptedException { + String lastFailure = null; + for (int attempt = 1; attempt <= ARM_ATTEMPTS && !cancelled; attempt++) { + if (attempt > 1) { + // A link Android dropped while it was still pairing comes back + // once the bond settles; reconnecting sooner just fails again. + waitForBondToSettle(address, label); + if (cancelled) { + break; + } + emitLog("retrying the " + label + " lens (attempt " + attempt + "/" + ARM_ATTEMPTS + "): " + lastFailure); + Thread.sleep(ARM_RETRY_DELAY_MS); + } + emitState("connecting", label); + try { + connectArm(address); + } catch (IllegalStateException e) { + lastFailure = e.getMessage() == null ? e.toString() : e.getMessage(); + emitLog(label + " lens: " + lastFailure); + continue; + } + if (cancelled) { + break; + } + + // Firmware 2.2.9 answers no queries until the security-auth + // exchange completes over an encrypted link; on a phone with no + // existing bond this is also what triggers SMP pairing (and its OS + // prompt), so it must come before the prelude and query. + emitState("authenticating", label); + AuthResult result = authenticate(address, label); + if (result == AuthResult.SUCCESS) { + return true; + } + if (result == AuthResult.UNCONFIRMED) { + return false; + } + lastFailure = "link dropped during authentication"; + } if (cancelled) { return false; } + throw new IllegalStateException("could not connect to the " + label + " lens (" + address + "): " + + lastFailure + " — if Android showed a Bluetooth pairing request, accept it and try again"); + } - // Firmware 2.2.9 answers no queries until the security-auth exchange - // completes over an encrypted link; on a phone with no existing bond - // this is also what triggers SMP pairing (and its OS prompt), so it - // must come before the prelude and query. Soft: this probe also runs - // against the custom firmware, whose response to the exchange is not - // yet hardware-verified, so an unconfirmed auth falls through. - emitState("authenticating", label); - return authenticate(address); + private enum AuthResult { SUCCESS, UNCONFIRMED, LINK_DROPPED } + + /** + * Send the sid-0x80 authentication request and wait for the firmware's + * SUCCESS notification, which only arrives once the link is encrypted. + * On an unbonded phone that means waiting through Android's pairing flow + * (BOND_BONDING, possibly with a dialog up), so the wait is extended while + * the OS reports pairing in progress and the request is re-sent once + * after a fresh bond in case the pre-pairing write was dropped. Bond + * state is polled rather than awaited via broadcast; 250 ms granularity + * is plenty here. + */ + private AuthResult authenticate(String address, String label) throws InterruptedException { + long start = SystemClock.elapsedRealtime(); + long deadline = start + ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS; + long hardDeadline = start + PAIRING_WAIT_CAP_MS; + CountDownLatch latch = new CountDownLatch(1); + synchronized (lock) { + authMagics.clear(); + authLatch = latch; + } + int initialBond = bleManager.getBondState(address); + emitLog("security auth: " + label + " lens, Android bond state " + bondStateName(initialBond)); + try { + int sends = 0; + boolean lastSendFailed = false; + long nextSendAt = start; + long bondedAt = -1; + boolean resentAfterBond = false; + boolean loggedBonding = false; + while (!cancelled) { + long now = SystemClock.elapsedRealtime(); + int bond = bleManager.getBondState(address); + + if (bond == BluetoothDevice.BOND_BONDING) { + // OS pairing in progress — the firmware will answer once the + // link is encrypted. Don't write, don't give up, don't move + // on to the other arm. + if (!loggedBonding) { + emitLog("Android is pairing with the " + label + " lens; waiting for the user to accept"); + loggedBonding = true; + } + if (now >= hardDeadline) { + break; + } + } else { + if (bond == BluetoothDevice.BOND_BONDED && initialBond != BluetoothDevice.BOND_BONDED + && bondedAt < 0) { + bondedAt = now; + emitLog("Android bond established with the " + label + " lens; waiting for the auth success"); + deadline = Math.max(deadline, now + ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS); + } + boolean wantSend = sends == 0 + || (lastSendFailed && sends < 3 && now >= nextSendAt) + || (bondedAt >= 0 && !resentAfterBond && now - bondedAt >= POST_BOND_GRACE_MS); + if (wantSend) { + if (bondedAt >= 0) { + resentAfterBond = true; + if (sends > 0) { + emitLog("re-sending the auth request after pairing (" + label + " lens)"); + } + } + lastSendFailed = !sendAuthRequest(address); + sends++; + nextSendAt = SystemClock.elapsedRealtime() + ARM_RETRY_DELAY_MS; + if (lastSendFailed && !bleManager.isConnected(address)) { + return AuthResult.LINK_DROPPED; + } + } + if (now >= deadline) { + break; + } + } + + if (latch.await(POLL_MS, TimeUnit.MILLISECONDS)) { + emitLog("security auth complete: " + label + " lens (" + address + ")"); + return AuthResult.SUCCESS; + } + if (!bleManager.isConnected(address)) { + emitLog("security auth: " + label + " lens disconnected while waiting" + + (bond == BluetoothDevice.BOND_BONDING ? " (Android was still pairing)" : "")); + return AuthResult.LINK_DROPPED; + } + } + if (cancelled) { + return AuthResult.UNCONFIRMED; + } + emitLog("security auth unconfirmed: " + label + " lens (" + address + "), Android bond state " + + bondStateName(bleManager.getBondState(address))); + return AuthResult.UNCONFIRMED; + } finally { + synchronized (lock) { + authLatch = null; + authMagics.clear(); + } + } + } + + private boolean sendAuthRequest(String address) { + int magic = allocMagic(); + int seq; + synchronized (lock) { + authMagics.add(magic); + seq = nextSeq++ & 0xff; + } + byte[] payload = BleProtocol.buildAuthenticationRequest(magic); + emitLog(String.format("tx %s sid=0x%02x flag=0x%02x magic=%d seq=0x%02x len=%d", + address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, magic, seq, payload.length)); + List frames = BleProtocol.framePb(payload, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, seq); + boolean written; + try { + written = bleManager.writeFrames( + address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS); + } catch (IllegalStateException e) { + // "Not connected" — the link went away underneath us. + written = false; + } + if (!written) { + emitLog("tx write FAILED sid=0x80 (" + address + ")"); + } + return written; + } + + /** Block while Android reports this arm as BOND_BONDING (pairing dialog up / SMP in flight). */ + private void waitForBondToSettle(String address, String label) throws InterruptedException { + long cap = SystemClock.elapsedRealtime() + PAIRING_WAIT_CAP_MS; + boolean logged = false; + while (!cancelled && bleManager.getBondState(address) == BluetoothDevice.BOND_BONDING + && SystemClock.elapsedRealtime() < cap) { + if (!logged) { + emitLog("waiting for Android to finish pairing with the " + label + " lens before reconnecting"); + logged = true; + } + Thread.sleep(POLL_MS); + } + } + + private static String bondStateName(int state) { + switch (state) { + case BluetoothDevice.BOND_NONE: + return "none"; + case BluetoothDevice.BOND_BONDING: + return "bonding"; + case BluetoothDevice.BOND_BONDED: + return "bonded"; + default: + return "unknown"; + } } /** @@ -213,15 +444,6 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { return null; } - private boolean authenticate(String address) throws InterruptedException { - int magic = allocMagic(); - byte[] ack = writeAndAwaitAck(address, BleProtocol.SID_SECURITY_AUTH, BleProtocol.FLAG_SECURITY_AUTH, - magic, BleProtocol.buildAuthenticationRequest(magic), ConnectionOptions.SECURITY_AUTH_TIMEOUT_MS); - boolean success = ack != null && BleProtocol.isAuthenticationSuccess(ack, magic); - emitLog(success ? "security auth complete: " + address : "security auth unconfirmed: " + address); - return success; - } - private void connectArm(String address) { if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { throw new IllegalStateException("connect failed"); @@ -306,7 +528,32 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener { unsolicitedSettingsPb = frame.pb; } if (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT) { - return; // async event, not an ack + // Async event, not an ack. This includes the lens's own periodic + // sid-0x80 notifies (device-chosen magic, non-empty result). + return; + } + if (frame.sid == BleProtocol.SID_SECURITY_AUTH) { + boolean success = false; + boolean ours = false; + synchronized (lock) { + if (authLatch != null) { + ours = authMagics.contains(frame.msgSeq); + for (int magic : authMagics) { + if (BleProtocol.isAuthenticationSuccess(frame.pb, magic)) { + success = true; + authLatch.countDown(); + break; + } + } + } + } + if (ours && !success) { + // Expected before the link is encrypted: the firmware answers + // the request straight away with a non-success result and + // sends the real success once pairing/encryption completes. + emitLog("auth reply for magic " + frame.msgSeq + " is not the success result; still waiting"); + } + return; } synchronized (lock) { if (awaitLatch != null && frame.sid == awaitSid && frame.msgSeq == awaitMagic) { diff --git a/app/phone-ui/onboarding-firmware-check-view-model.ts b/app/phone-ui/onboarding-firmware-check-view-model.ts index 6916d7e9..3314fab4 100644 --- a/app/phone-ui/onboarding-firmware-check-view-model.ts +++ b/app/phone-ui/onboarding-firmware-check-view-model.ts @@ -178,7 +178,7 @@ export class OnboardingFirmwareCheckViewModel extends Observable { this.disposeProbe(); const probe = new DeviceInfoProbe(stored.right, stored.left); this.probeInstance = probe; - probe.onStateChange((state) => this.reportProbeState(state)); + probe.onStateChange((state, detail) => this.reportProbeState(state, detail)); const info = await probe.run(); this.probeInstance = null; @@ -191,12 +191,15 @@ export class OnboardingFirmwareCheckViewModel extends Observable { } } - private reportProbeState(state: DeviceInfoState): void { + private reportProbeState(state: DeviceInfoState, detail: string): void { + // The probe brings up the right lens, then the left, and pairs with each + // in turn (they are separate Bluetooth devices with separate bonds). + const lens = detail === "left" || detail === "right" ? `the ${detail} lens` : "your glasses"; if (state === "connecting") { - this.status = "Connecting to your glasses..."; + this.status = `Connecting to ${lens}...`; } else if (state === "authenticating") { // First-time connections pair here; the OS may show a Bluetooth dialog. - this.status = "Authenticating with your glasses... If Android asks to pair, tap Pair."; + this.status = `Pairing with ${lens}... Each lens pairs separately; if Android asks to pair, tap Pair.`; } else if (state === "querying") { this.status = "Reading the firmware version..."; }