From 52ead474e26538d121a1921691c43e56f8f61e97 Mon Sep 17 00:00:00 2001 From: MasterControl Date: Sat, 14 Nov 2026 21:46:32 +0000 Subject: [PATCH] fix(ble): fall back to direct connect when autoConnect can't reach an arm autoConnect only connects on a fresh advertisement, so an arm stuck in a half-open state (stopped advertising after a dropped link) is never reached. Retry with a direct connect: its aggressive scan catches a briefly-advertising arm and the cached bond can recover the half-open link. Adds a g2.connect-fallback-direct counter for diagnostics. --- .../com/faceclaw/app/g2protocol/ConnectionOptions.kt | 6 ++++++ .../app/g2protocol/session/GlassesSessionCore.kt | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt index 2ab2911d..53baca95 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt @@ -12,6 +12,12 @@ class ConnectionOptions { // giving up. Both are dialed at once, so a slow or missing arm gets the whole // window (it stays under Android's own ~30 s direct-connect timeout). const val ARM_CONNECT_WINDOW_MS = 20_000 + // When autoConnect (background reconnect) fails to reach an arm, a direct connect + // is retried: its aggressive scan can catch an arm that advertises only briefly, + // and the cached bond can sometimes recover a half-open link that stopped + // advertising after a dropped link. This is the direct retry's own window, + // independent of ARM_CONNECT_WINDOW_MS (already spent on the autoConnect attempt). + const val DIRECT_CONNECT_FALLBACK_TIMEOUT_MS = 15_000 const val SERVICES_TIMEOUT_MS = 5_000 const val DESCRIPTOR_TIMEOUT_MS = 5_000 const val RING_DESIRED_MTU = 247 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt index 646a6d7e..1de741b7 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt @@ -2454,7 +2454,15 @@ class GlassesSessionCore( internal fun connectArm(address: String, enableRenderNotify: Boolean, deadlineMs: Long) { val timeoutMs = maxOf(deadlineMs - now(), ConnectionOptions.CONNECT_TIMEOUT_MS.toLong()) if (!link.connect(address, timeoutMs.toInt(), true)) { - throw ArmUnreachableException(if (address.equals(leftAddress, ignoreCase = true)) "left" else "right", address) + // autoConnect only connects on a fresh advertisement; an arm stuck in a + // half-open state (it stopped advertising after a dropped link) never + // produces one. Retry direct: the aggressive scan plus the cached bond + // can recover it. Still unreachable after that is a real failure. + logLine("autoConnect timed out for " + address + "; retrying direct connect") + ConnectionCounters.increment("g2.connect-fallback-direct") + if (!link.connect(address, ConnectionOptions.DIRECT_CONNECT_FALLBACK_TIMEOUT_MS, false)) { + throw ArmUnreachableException(if (address.equals(leftAddress, ignoreCase = true)) "left" else "right", address) + } } // requestConnectionPriority has no callback in this Android compile target, so there is // no reliable completion point to keep it in the global GATT operation pipeline. But it's