From 4a1a1d59be17676b5e2c6e3acf713db2a957bdd7 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 19 Apr 2015 22:31:09 +0200 Subject: [PATCH] battery info hooked in, dummy for pebble --- .../gadgetbridge/AbstractBTDeviceSupport.java | 6 +++ .../gadgetbridge/EventHandler.java | 2 + .../gadgetbridge/miband/MiBandSupport.java | 43 ++++++++++++++----- .../protocol/GBDeviceProtocol.java | 4 ++ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java index ae1992b18..8a5650cea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AbstractBTDeviceSupport.java @@ -81,6 +81,12 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport { sendToDevice(bytes); } + @Override + public void onBatteryInfoReq() { + byte[] bytes = gbDeviceProtocol.encodeBatteryInfoReq(); + sendToDevice(bytes); + } + @Override public void onAppInfoReq() { byte[] bytes = gbDeviceProtocol.encodeAppInfoReq(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/EventHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/EventHandler.java index 3c307ce0c..390b92bdf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/EventHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/EventHandler.java @@ -13,6 +13,8 @@ public interface EventHandler { public void onFirmwareVersionReq(); + public void onBatteryInfoReq(); + public void onAppInfoReq(); public void onAppDelete(int id, int index); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java index 10e18d5da..6dfa8f7da 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java @@ -142,7 +142,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { @Override public void onFirmwareVersionReq() { try { - TransactionBuilder builder = performInitialized("Get MI Band Device Info"); + TransactionBuilder builder = performInitialized("Get MI Band device info"); BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_DEVICE_INFO); builder.read(characteristic).queue(getQueue()); } catch (IOException ex) { @@ -150,6 +150,17 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { } } + @Override + public void onBatteryInfoReq() { + try { + TransactionBuilder builder = performInitialized("Get MI Band battery info"); + BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_BATTERY); + builder.read(characteristic).queue(getQueue()); + } catch (IOException ex) { + Log.e(TAG, "Unable to read battery info from MI", ex); + } + } + @Override public void onAppInfoReq() { // TODO Auto-generated method stub @@ -173,16 +184,11 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); - if (MiBandService.UUID_CHARACTERISTIC_DEVICE_INFO.equals(characteristic.getUuid())) { + UUID characteristicUUID = characteristic.getUuid(); + if (MiBandService.UUID_CHARACTERISTIC_DEVICE_INFO.equals(characteristicUUID)) { handleDeviceInfo(characteristic.getValue(), status); - } - } - - private void handleDeviceInfo(byte[] value, int status) { - if (status == BluetoothGatt.GATT_SUCCESS) { - DeviceInfo info = new DeviceInfo(value); - getDevice().setFirmwareVersion(info.getFirmwareVersion()); - getDevice().sendDeviceUpdateIntent(getContext()); + } else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) { + handleBatteryInfo(characteristic.getValue(), status); } } @@ -197,6 +203,23 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { } } + private void handleDeviceInfo(byte[] value, int status) { + if (status == BluetoothGatt.GATT_SUCCESS) { + DeviceInfo info = new DeviceInfo(value); + getDevice().setFirmwareVersion(info.getFirmwareVersion()); + getDevice().sendDeviceUpdateIntent(getContext()); + } + } + + private void handleBatteryInfo(byte[] value, int status) { + if (status == BluetoothGatt.GATT_SUCCESS) { + BatteryInfo info = new BatteryInfo(value); + getDevice().setBatteryLevel((short) info.getLevelInPercent()); + getDevice().setBatteryState(info.getStatus()); + getDevice().sendDeviceUpdateIntent(getContext()); + } + } + private void handleUserInfoResult(byte[] value, int status) { // successfully transfered user info means we're initialized if (status == BluetoothGatt.GATT_SUCCESS) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceProtocol.java index 6a9bb9777..ff42b60ce 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceProtocol.java @@ -28,6 +28,10 @@ public abstract class GBDeviceProtocol { return null; } + public byte[] encodeBatteryInfoReq() { + return null; + } + public byte[] encodeAppInfoReq() { return null; }