From 31c85be5a020c8e0371ab648064b35306df72a54 Mon Sep 17 00:00:00 2001 From: Unpublished Date: Sat, 21 Dec 2024 16:16:30 +0100 Subject: [PATCH] Colmi R0x: Read charging state from battery notification --- .../devices/colmi/ColmiR0xDeviceSupport.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java index 1ef773e99..984a1c649 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java @@ -23,6 +23,8 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; +import androidx.annotation.NonNull; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -180,9 +182,7 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { int levelResponse = value[1]; boolean charging = value[2] == 1; LOG.info("Received battery level response: {}% (charging: {})", levelResponse, charging); - GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); - batteryEvent.level = levelResponse; - batteryEvent.state = charging ? BatteryState.BATTERY_CHARGING : BatteryState.BATTERY_NORMAL; + GBDeviceEventBatteryInfo batteryEvent = createDeviceBatteryInfoEvent(levelResponse, charging); evaluateGBDeviceEvent(batteryEvent); break; case ColmiR0xConstants.CMD_PHONE_NAME: @@ -319,10 +319,9 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { break; case ColmiR0xConstants.NOTIFICATION_BATTERY_LEVEL: int levelNotif = value[2]; - LOG.info("Received battery level notification: {}%", levelNotif); - GBDeviceEventBatteryInfo batteryNotifEvent = new GBDeviceEventBatteryInfo(); - batteryNotifEvent.state = BatteryState.BATTERY_NORMAL; - batteryNotifEvent.level = levelNotif; + charging = value[3] == 1; + LOG.info("Received battery level notification: {}% (charging: {})", levelNotif, charging); + GBDeviceEventBatteryInfo batteryNotifEvent = createDeviceBatteryInfoEvent(levelNotif, charging); evaluateGBDeviceEvent(batteryNotifEvent); break; case ColmiR0xConstants.NOTIFICATION_LIVE_ACTIVITY: @@ -400,6 +399,14 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { return false; } + @NonNull + private static GBDeviceEventBatteryInfo createDeviceBatteryInfoEvent(int levelResponse, boolean charging) { + GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); + batteryEvent.level = levelResponse; + batteryEvent.state = charging ? BatteryState.BATTERY_CHARGING : BatteryState.BATTERY_NORMAL; + return batteryEvent; + } + private byte[] buildPacket(byte[] contents) { ByteBuffer buffer = ByteBuffer.allocate(16); if (contents.length <= 15) {