Add R1 ring battery indicator

This commit is contained in:
James Babcock committed 2026-09-09 11:31:44 -07:00
1 parent 92d5857c3e
commit adf9fa89c4
10 files changed
+111 -32

No files matched your search

@@ -162,6 +162,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private long lastShutdownExitAtMs = 0;
private int headsetBattery = -1;
private int headsetCharging = -1;
private int ringBattery = -1;
private int ringCharging = -1;
// Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked.
private int silentMode = -1;
private int wearState = -1;
@@ -1490,6 +1492,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
if (!faceclawWakeNotification
&& frame.ok
&& frame.sid == BleProtocol.SID_UI_SETTING) {
// Mode-17 query notifications; settings READs are handled by
// createBatteryQueryMessageLocked so headset/ring update together.
if (address.equalsIgnoreCase(rightAddress)
&& (frame.flag == BleProtocol.FLAG_NOTIFY || frame.flag == BleProtocol.FLAG_NOTIFY_ALT)) {
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(frame.pb);
if (ring != null) {
ringBattery = ring.battery;
ringCharging = ring.charging;
emitBatteryState(headsetBattery, headsetCharging);
}
}
// CFW mic status (field 104) rides both standalone pushes and
// settings read acks, from each temple on its own link.
byte[] micStatus = BleProtocol.parseFaceclawMicStatus(frame.pb);
@@ -2777,6 +2790,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private OutboundMessage createBatteryQueryMessageLocked() {
OutboundMessage message = messageBuilder.batteryQuery();
message.onAck = () -> {
BleProtocol.RingBatterySnapshot ring = BleProtocol.parseRingBattery(message.ackPayload);
// Old firmware and missing/malformed extensions must clear any prior reading.
ringBattery = ring == null ? -1 : ring.battery;
ringCharging = ring == null ? -1 : ring.charging;
BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(message.ackPayload);
if (snapshot != null) {
headsetBattery = snapshot.battery;
@@ -3361,6 +3378,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
private void resetSessionStateLocked() {
ringBattery = -1;
ringCharging = -1;
sessionReady = false;
shutdownRequested = false;
fixedLayoutCreated = false;
@@ -3528,13 +3547,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
private void emitBatteryState(int headsetBattery, int headsetCharging) {
final int reportedRingBattery = ringBattery;
final int reportedRingCharging = ringCharging;
final FaceclawBleCommunicatorListener current = listener;
if (current == null) {
return;
}
mainHandler.post(() -> {
try {
current.onBatteryState(headsetBattery, headsetCharging);
current.onBatteryState(headsetBattery, headsetCharging, reportedRingBattery, reportedRingCharging);
} catch (Throwable t) {
Log.w(TAG, "listener onBatteryState failed", t);
}
@@ -4,7 +4,7 @@ public interface FaceclawBleCommunicatorListener {
void onLog(String line);
void onStateChange(String phase, String status);
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
void onBatteryState(int headsetBattery, int headsetCharging);
void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging);
void onSilentMode(boolean silent);
void onWearState(boolean wearing);
void onPhoneLockState(boolean locked);
@@ -475,6 +475,32 @@ public class BleProtocol {
return body;
}
/** Field 106: RB/version 1/flags/percentage, from CFW ringbat17. */
public static RingBatterySnapshot parseRingBattery(byte[] pb) {
if (pb == null) return null;
byte[] body = readFieldBytes(stripTrailingCrc(pb), 106);
if (body == null || body.length != 5 || body[0] != 'R' || body[1] != 'B'
|| body[2] != 1) return null;
int flags = body[3] & 0xff;
int level = body[4] & 0xff;
if ((flags & ~7) != 0) return null;
if ((flags & 2) != 0) {
if ((flags & 1) == 0 || level > 100) return null;
return new RingBatterySnapshot(level, (flags & 4) != 0 ? 1 : 0);
}
if (level != 255 || (flags & 4) != 0) return null;
return new RingBatterySnapshot(-1, -1);
}
public static final class RingBatterySnapshot {
final int battery;
final int charging;
RingBatterySnapshot(int battery, int charging) {
this.battery = battery;
this.charging = charging;
}
}
/**
* Return the uint16 wake nonce from a CFW field-102 notification, or -1
* when this is an ordinary settings frame.