Try to connect directly to the ring (requires ring to be unpaired)

This commit is contained in:
James Babcock committed 2026-06-09 19:05:34 -07:00
1 parent ec924f7909
commit 7857e6337a
6 files changed
+295 -1

No files matched your search

@@ -54,6 +54,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private boolean rightConnected;
private boolean leftConnected;
private boolean ringConnected;
private boolean ringNotificationsReady;
private boolean sessionReady;
private boolean fixedLayoutCreated;
private boolean warmedUp;
@@ -61,6 +63,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private boolean startupProbePending;
private long reconnectAfterMs;
private long ringReconnectAfterMs;
private long lastAckAtMs;
private long lastIncomingAtMs;
private long lastHeartbeatSentAtMs;
@@ -155,6 +158,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
bleManager.disconnect(rightAddress);
bleManager.disconnect(leftAddress);
if (hasRingAddress()) {
bleManager.disconnect(ringAddress);
}
bleManager.close();
setStateDisplay("disconnected", "Disconnected.");
}
@@ -354,6 +360,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
continue;
}
if (shouldAttemptRingConnect()) {
tryConnectRing("retry");
continue;
}
long sleepMs = driveSession();
if (sleepMs > 0) {
interruptibleSleep.sleep(sleepMs);
@@ -371,6 +382,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return;
}
String uuid = characteristicUuid.toLowerCase(Locale.US);
if (isDirectRingNotification(address, uuid)) {
handleDirectRingNotification(uuid, data);
return;
}
if (BleProtocol.RENDER_NOTIFY_UUID.equals(uuid)) {
handleRenderNotification(address, data);
return;
@@ -391,7 +406,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
event = G2Event.decode(frame);
if (event != null) {
lastConnectionOrInputAtMs = lastIncomingAtMs;
if (event.kind == "sys-event") {
if ("sys-event".equals(event.kind)) {
if (event.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || event.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || event.eventType == BleProtocol.EVENT_SYSTEM_EXIT) {
if (shutdownRequested) {
lastShutdownExitAtMs = SystemClock.elapsedRealtime();
@@ -412,6 +427,24 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
private void handleDirectRingNotification(String characteristicUuid, byte[] data) {
FaceclawRingEventDecoder.DirectRingEvent decoded = FaceclawRingEventDecoder.decode(data);
if (decoded == null) {
Log.d(TAG, "direct ring notify ignored: characteristicUuid=" + characteristicUuid + " raw=" + hex(data));
return;
}
G2Event event = decoded.event;
long arrivalMs = SystemClock.elapsedRealtime();
synchronized (lock) {
lastIncomingAtMs = arrivalMs;
lastConnectionOrInputAtMs = arrivalMs;
}
logLine("direct ring " + decoded.label + " " + decoded.detail + " raw=" + hex(data));
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode);
interruptibleSleep.interrupt();
}
private void handleRenderNotification(String address, byte[] data) {
FaceclawAudioPacketListener listenerToCall;
long arrivalMs = SystemClock.elapsedRealtime();
@@ -435,10 +468,21 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (address == null) {
return;
}
if (isConfiguredRingAddress(address)) {
ringConnected = connected;
ringNotificationsReady = false;
if (!connected) {
ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS;
}
logLine(connected ? "direct ring BLE connected" : "direct ring BLE disconnected");
return;
}
if (address.equalsIgnoreCase(rightAddress)) {
rightConnected = connected;
} else if (address.equalsIgnoreCase(leftAddress)) {
leftConnected = connected;
} else {
return;
}
if (!connected) {
sessionReady = false;
@@ -492,6 +536,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
setStateDisplay("connected", "Connected.");
logLine("session ready");
tryConnectRing("initial");
} catch (Throwable t) {
logLine("connect failed: " + safeMessage(t));
handleTransportFailure("connect failed");
@@ -541,6 +586,78 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
}
private boolean shouldAttemptRingConnect() {
if (!hasRingAddress()) {
return false;
}
long now = SystemClock.elapsedRealtime();
synchronized (lock) {
return running
&& sessionReady
&& !ringNotificationsReady
&& now >= ringReconnectAfterMs
&& pendingMessages.isEmpty()
&& inFlightMessages.isEmpty();
}
}
private void tryConnectRing(String reason) {
if (!hasRingAddress()) {
return;
}
try {
connectRing();
} catch (Throwable t) {
synchronized (lock) {
ringConnected = false;
ringNotificationsReady = false;
ringReconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RING_RECONNECT_DELAY_MS;
}
logLine("direct ring connect failed (" + reason + "): " + safeMessage(t));
}
}
private void connectRing() {
logLine("connecting direct ring " + ringAddress);
if (!bleManager.connect(ringAddress, ConnectionOptions.CONNECT_TIMEOUT_MS)) {
throw new IllegalStateException("connect failed: " + ringAddress);
}
bleManager.requestConnectionPriority(ringAddress, BluetoothGatt.CONNECTION_PRIORITY_HIGH);
bleManager.requestMtu(ringAddress, ConnectionOptions.RING_DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS);
if (!bleManager.discoverServices(ringAddress, ConnectionOptions.SERVICES_TIMEOUT_MS)) {
throw new IllegalStateException("discoverServices failed: " + ringAddress);
}
boolean phoneNotify = enableRingNotification(BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID);
boolean dataNotify = enableRingNotification(BleProtocol.R1_NOTIFY_CHAR_UUID);
if (!phoneNotify && !dataNotify) {
throw new IllegalStateException("no R1 notify characteristic subscribed");
}
synchronized (lock) {
ringConnected = true;
ringNotificationsReady = true;
ringReconnectAfterMs = 0;
}
logLine("direct ring ready phoneNotify=" + phoneNotify + " dataNotify=" + dataNotify);
}
private boolean enableRingNotification(String characteristicUuid) {
try {
return bleManager.enableNotifications(
ringAddress,
characteristicUuid,
true,
ConnectionOptions.DESCRIPTOR_TIMEOUT_MS
);
} catch (Throwable t) {
Log.d(TAG, "direct ring notify subscribe skipped: " + characteristicUuid + " " + safeMessage(t));
return false;
}
}
private void sendPrelude() throws InterruptedException {
synchronized (lock) {
clearAllMessagesLocked("prelude");
@@ -1323,7 +1440,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
warmedUp = false;
rightConnected = false;
leftConnected = false;
ringConnected = false;
ringNotificationsReady = false;
reconnectAfterMs = 0;
ringReconnectAfterMs = 0;
lastAckAtMs = 0;
lastIncomingAtMs = 0;
lastHeartbeatSentAtMs = 0;
@@ -1488,6 +1608,36 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return address.trim();
}
private boolean hasRingAddress() {
return ringAddress != null && !ringAddress.trim().isEmpty();
}
private boolean isConfiguredRingAddress(String address) {
return hasRingAddress() && address != null && address.equalsIgnoreCase(ringAddress);
}
private boolean isDirectRingNotification(String address, String characteristicUuid) {
if (!isConfiguredRingAddress(address) || characteristicUuid == null) {
return false;
}
return BleProtocol.R1_PHONE_NOTIFY_CHAR_UUID.equals(characteristicUuid)
|| BleProtocol.R1_NOTIFY_CHAR_UUID.equals(characteristicUuid);
}
private static String hex(byte[] data) {
if (data == null || data.length == 0) {
return "";
}
char[] out = new char[data.length * 2];
char[] digits = "0123456789abcdef".toCharArray();
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xff;
out[i * 2] = digits[value >>> 4];
out[i * 2 + 1] = digits[value & 0x0f];
}
return new String(out);
}
private static byte[][] emptyTileSet() {
return new byte[][] {new byte[0], new byte[0], new byte[0], new byte[0]};
}
@@ -0,0 +1,126 @@
package com.faceclaw.app;
import java.util.Locale;
final class FaceclawRingEventDecoder {
private FaceclawRingEventDecoder() {
}
static DirectRingEvent decode(byte[] raw) {
if (raw == null || raw.length == 0) {
return null;
}
DirectRingEvent chargerGesture = decodeChargerGesture(raw);
if (chargerGesture != null) {
return chargerGesture;
}
return decodeDirectGesture(raw);
}
private static DirectRingEvent decodeChargerGesture(byte[] raw) {
if (raw.length != 11) {
return null;
}
if (u8(raw[0]) != 0x00 || u8(raw[1]) != 0x09 || u8(raw[2]) != 0x61 || u8(raw[3]) != 0x00) {
return null;
}
int code = u8(raw[4]);
int param = u8(raw[5]) | (u8(raw[6]) << 8);
long tick = (u8(raw[7]) | (u8(raw[8]) << 8) | (u8(raw[9]) << 16) | (u8(raw[10]) << 24)) & 0xffffffffL;
String detail = String.format(Locale.US, "code=0x%02x(%s) param=0x%04x tick=%d", code, chargerCodeName(code), param, tick);
switch (code) {
case 0x00:
return event(BleProtocol.EVENT_RING_LONG_PRESS, "LONG_PRESS", detail);
case 0x01:
return event(BleProtocol.EVENT_CLICK, "TAP", detail);
case 0x02:
return event(BleProtocol.EVENT_DOUBLE_CLICK, "DOUBLE_TAP", detail);
case 0x04:
return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_UP", detail);
case 0x05:
return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_DOWN", detail);
case 0x08:
return event(BleProtocol.EVENT_RING_LONG_PRESS_RELEASE, "LONG_PRESS_RELEASE", detail);
default:
return null;
}
}
private static DirectRingEvent decodeDirectGesture(byte[] raw) {
if (raw.length != 3 || u8(raw[0]) != 0xff) {
return null;
}
int type = u8(raw[1]);
int param = u8(raw[2]);
String detail = String.format(Locale.US, "type=0x%02x param=0x%02x", type, param);
if (type == 0x03 && param == 0x20) {
return event(BleProtocol.EVENT_RING_LONG_PRESS, "HOLD", detail);
}
if (type == 0x04 && param == 0x01) {
return event(BleProtocol.EVENT_CLICK, "TAP_SINGLE", detail);
}
if (type == 0x04 && param == 0x02) {
return event(BleProtocol.EVENT_DOUBLE_CLICK, "TAP_DOUBLE", detail);
}
if (type == 0x05) {
if (param <= 0x01) {
return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_FORWARD", detail);
}
return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_BACKWARD", detail);
}
return null;
}
private static DirectRingEvent event(int eventType, String label, String detail) {
G2Event event = new G2Event(
"sys-event",
"",
eventType,
BleProtocol.EVENT_SOURCE_RING,
0
);
return new DirectRingEvent(event, label, detail);
}
private static String chargerCodeName(int code) {
switch (code) {
case 0x00:
return "LONG_PRESS";
case 0x01:
return "TAP";
case 0x02:
return "DOUBLE_TAP";
case 0x04:
return "SWIPE_UP";
case 0x05:
return "SWIPE_DOWN";
case 0x08:
return "LONG_PRESS_RELEASE";
default:
return "unknown";
}
}
private static int u8(byte value) {
return value & 0xff;
}
static final class DirectRingEvent {
final G2Event event;
final String label;
final String detail;
DirectRingEvent(G2Event event, String label, String detail) {
this.event = event;
this.label = label;
this.detail = detail;
}
}
}
@@ -18,6 +18,8 @@ public class BleProtocol {
public static final String WRITE_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5401";
public static final String NOTIFY_CHAR_UUID = "00002760-08c2-11e1-9073-0e8ac72e5402";
public static final String RENDER_NOTIFY_UUID = "00002760-08c2-11e1-9073-0e8ac72e6402";
public static final String R1_PHONE_NOTIFY_CHAR_UUID = "bae80011-4f05-4503-8e65-3af1f7329d1f";
public static final String R1_NOTIFY_CHAR_UUID = "bae80013-4f05-4503-8e65-3af1f7329d1f";
public static final int PRELUDE_ACK_SID = 0x01;
public static final int PRELUDE_ACK_MAGIC = 156;
@@ -37,6 +39,12 @@ public class BleProtocol {
public static final int EVENT_FOREGROUND_EXIT = 5;
public static final int EVENT_ABNORMAL_EXIT = 6;
public static final int EVENT_SYSTEM_EXIT = 7;
public static final int EVENT_RING_LONG_PRESS = 9;
public static final int EVENT_RING_LONG_PRESS_RELEASE = 10;
public static final int EVENT_SOURCE_GLASSES_R = 1;
public static final int EVENT_SOURCE_RING = 2;
public static final int EVENT_SOURCE_GLASSES_L = 3;
public static final UUID CCCD_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
@@ -8,6 +8,8 @@ public class ConnectionOptions {
public static final int CONNECT_TIMEOUT_MS = 5_000;
public static final int SERVICES_TIMEOUT_MS = 5_000;
public static final int DESCRIPTOR_TIMEOUT_MS = 5_000;
public static final int RING_DESIRED_MTU = 247;
public static final int RING_RECONNECT_DELAY_MS = 2_000;
public static final int WRITE_TIMEOUT_MS = 2_000;
public static final int PRELUDE_TIMEOUT_MS = 2_000;
public static final int ACK_TIMEOUT_MS = 3_500;