1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 01:55:50 +01:00

Colmi R0x: Read charging state from battery notification

This commit is contained in:
Unpublished 2024-12-21 16:16:30 +01:00
parent 416631e028
commit 31c85be5a0

View File

@ -23,6 +23,8 @@ import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import androidx.annotation.NonNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -180,9 +182,7 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport {
int levelResponse = value[1]; int levelResponse = value[1];
boolean charging = value[2] == 1; boolean charging = value[2] == 1;
LOG.info("Received battery level response: {}% (charging: {})", levelResponse, charging); LOG.info("Received battery level response: {}% (charging: {})", levelResponse, charging);
GBDeviceEventBatteryInfo batteryEvent = new GBDeviceEventBatteryInfo(); GBDeviceEventBatteryInfo batteryEvent = createDeviceBatteryInfoEvent(levelResponse, charging);
batteryEvent.level = levelResponse;
batteryEvent.state = charging ? BatteryState.BATTERY_CHARGING : BatteryState.BATTERY_NORMAL;
evaluateGBDeviceEvent(batteryEvent); evaluateGBDeviceEvent(batteryEvent);
break; break;
case ColmiR0xConstants.CMD_PHONE_NAME: case ColmiR0xConstants.CMD_PHONE_NAME:
@ -319,10 +319,9 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport {
break; break;
case ColmiR0xConstants.NOTIFICATION_BATTERY_LEVEL: case ColmiR0xConstants.NOTIFICATION_BATTERY_LEVEL:
int levelNotif = value[2]; int levelNotif = value[2];
LOG.info("Received battery level notification: {}%", levelNotif); charging = value[3] == 1;
GBDeviceEventBatteryInfo batteryNotifEvent = new GBDeviceEventBatteryInfo(); LOG.info("Received battery level notification: {}% (charging: {})", levelNotif, charging);
batteryNotifEvent.state = BatteryState.BATTERY_NORMAL; GBDeviceEventBatteryInfo batteryNotifEvent = createDeviceBatteryInfoEvent(levelNotif, charging);
batteryNotifEvent.level = levelNotif;
evaluateGBDeviceEvent(batteryNotifEvent); evaluateGBDeviceEvent(batteryNotifEvent);
break; break;
case ColmiR0xConstants.NOTIFICATION_LIVE_ACTIVITY: case ColmiR0xConstants.NOTIFICATION_LIVE_ACTIVITY:
@ -400,6 +399,14 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport {
return false; 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) { private byte[] buildPacket(byte[] contents) {
ByteBuffer buffer = ByteBuffer.allocate(16); ByteBuffer buffer = ByteBuffer.allocate(16);
if (contents.length <= 15) { if (contents.length <= 15) {