diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java index 9412868b5..0b8f5623f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java @@ -26,7 +26,7 @@ public class MiBand2Service { public static final UUID UUID_CHARACTERISTIC_3_CONFIGURATION = UUID.fromString("00000003-0000-3512-2118-0009af100700"); public static final UUID UUID_UNKNOWN_CHARACTERISTIC4 = UUID.fromString("00000004-0000-3512-2118-0009af100700"); public static final UUID UUID_CHARACTERISTIC_5_ACTIVITY_DATA = UUID.fromString("00000005-0000-3512-2118-0009af100700"); - public static final UUID UUID_UNKNOWN_CHARACTERISTIC6 = UUID.fromString("00000006-0000-3512-2118-0009af100700"); + public static final UUID UUID_CHARACTERISTIC_6_BATTERY_INFO = UUID.fromString("00000006-0000-3512-2118-0009af100700"); public static final UUID UUID_UNKNOWN_CHARACTERISTIC7 = UUID.fromString("00000007-0000-3512-2118-0009af100700"); public static final UUID UUID_UNKNOWN_CHARACTERISTIC8 = UUID.fromString("00000008-0000-3512-2118-0009af100700"); // service uuid fee1 diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/BatteryInfo.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/BatteryInfo.java new file mode 100644 index 000000000..8b44c8740 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/BatteryInfo.java @@ -0,0 +1,95 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.miband2; + +import java.util.GregorianCalendar; + +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter; +import nodomain.freeyourgadget.gadgetbridge.model.BatteryState; +import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; +import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.AbstractInfo; + +//00000006-0000-3512-2118-0009af100700 +// +// f = ? +// 30 = 48% +// 00 = 00 = STATUS_NORMAL, 01 = STATUS_CHARGING +// e0 07 = 2016 +// 0b = 11 +// 1a = 26 +// 12 = 18 +// 23 = 35 +// 2c = 44 +// 04 = 4 // num charges?? +// +// e0 07 = 2016 // last charge time +// 0b = 11 +// 1a = 26 +// 17 = 23 +// 2b = 43 +// 3b = 59 +// 04 = 4 // num charges?? +// 64 = 100 // how much was charged + +public class BatteryInfo extends AbstractInfo { + public static final byte DEVICE_BATTERY_NORMAL = 0; + public static final byte DEVICE_BATTERY_CHARGING = 1; +// public static final byte DEVICE_BATTERY_LOW = 1; +// public static final byte DEVICE_BATTERY_CHARGING_FULL = 3; +// public static final byte DEVICE_BATTERY_CHARGE_OFF = 4; + + public BatteryInfo(byte[] data) { + super(data); + } + + public int getLevelInPercent() { + if (mData.length >= 2) { + return mData[1]; + } + return 50; // actually unknown + } + + public BatteryState getState() { + if (mData.length >= 3) { + int value = mData[2]; + switch (value) { + case DEVICE_BATTERY_NORMAL: + return BatteryState.BATTERY_NORMAL; + case DEVICE_BATTERY_CHARGING: + return BatteryState.BATTERY_CHARGING; +// case DEVICE_BATTERY_CHARGING: +// return BatteryState.BATTERY_CHARGING; +// case DEVICE_BATTERY_CHARGING_FULL: +// return BatteryState.BATTERY_CHARGING_FULL; +// case DEVICE_BATTERY_CHARGE_OFF: +// return BatteryState.BATTERY_NOT_CHARGING_FULL; + } + } + return BatteryState.UNKNOWN; + } + + public int getLastChargeLevelInParcent() { + if (mData.length >= 20) { + return mData[19]; + } + return 50; // actually unknown + } + + public GregorianCalendar getLastChargeTime() { + GregorianCalendar lastCharge = MiBandDateConverter.createCalendar(); + + if (mData.length >= 18) { + lastCharge = BLETypeConversions.rawBytesToCalendar(new byte[]{ + mData[10], mData[11], mData[12], mData[13], mData[14], mData[15], mData[16], mData[17] + }, true); + } + + return lastCharge; + } + + public int getNumCharges() { +// if (mData.length >= 10) { +// return ((0xff & mData[7]) | ((0xff & mData[8]) << 8)); +// +// } + return -1; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java index 6f792d291..e1667f6f9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java @@ -68,9 +68,9 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WriteAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.heartrate.HeartRateProfile; -import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.BatteryInfo; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.CheckAuthenticationNeededAction; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.DeviceInfo; +import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.RealtimeSamplesSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.FetchActivityOperation; @@ -268,9 +268,9 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { public MiBand2Support enableFurtherNotifications(TransactionBuilder builder, boolean enable) { // builder.notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_REALTIME_STEPS), enable) // .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_ACTIVITY_DATA), enable) -// .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_BATTERY), enable) // .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_SENSOR_DATA), enable); builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), enable); + builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO), enable); BluetoothGattCharacteristic heartrateCharacteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT); if (heartrateCharacteristic != null) { builder.notify(heartrateCharacteristic, enable); @@ -367,6 +367,13 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { return this; } + private MiBand2Support requestBatteryInfo(TransactionBuilder builder) { + LOG.debug("Requesting Battery Info!"); + BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO); + builder.read(characteristic); + return this; + } + public MiBand2Support requestDeviceInfo(TransactionBuilder builder) { LOG.debug("Requesting Device Info!"); deviceInfoProfile.requestDeviceInfo(builder); @@ -817,7 +824,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { super.onCharacteristicChanged(gatt, characteristic); UUID characteristicUUID = characteristic.getUuid(); - if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) { + if (MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO.equals(characteristicUUID)) { handleBatteryInfo(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS); return true; } else if (MiBandService.UUID_CHARACTERISTIC_NOTIFICATION.equals(characteristicUUID)) { @@ -856,7 +863,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { if (GattCharacteristic.UUID_CHARACTERISTIC_GAP_DEVICE_NAME.equals(characteristicUUID)) { handleDeviceName(characteristic.getValue(), status); return true; - } else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) { + } else if (MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO.equals(characteristicUUID)) { handleBatteryInfo(characteristic.getValue(), status); return true; } else if (MiBandService.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT.equals(characteristicUUID)) { @@ -1272,11 +1279,11 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { public void phase2Initialize(TransactionBuilder builder) { LOG.info("phase2Initialize..."); enableFurtherNotifications(builder, true); + requestBatteryInfo(builder); setDateDisplay(builder); setWearLocation(builder); setFitnessGoal(builder); setActivateDisplayOnLiftWrist(builder); setHeartrateSleepSupport(builder); - } }