1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-19 03:20:21 +02:00

#15 support for reading firmware version and battery info

This commit is contained in:
cpfeiffer 2015-04-19 22:20:47 +02:00
parent 28272714aa
commit 2dcd95a1f5
5 changed files with 121 additions and 7 deletions

View File

@ -3,10 +3,12 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class GBDevice {
public static final String ACTION_DEVICE_CHANGED
= "nodomain.freeyourgadget.gadgetbride.gbdevice.action.device_changed";
private static final String TAG = GBDevice.class.getSimpleName();
private final String name;
private final String address;
@ -14,6 +16,11 @@ public class GBDevice {
private String firmwareVersion = null;
private State state = State.NOT_CONNECTED;
private short mBatteryLevel = 50; // unknown
private String mBatteryState;
public GBDevice(String address, String name, Type type) {
this.address = address;
this.name = name;
@ -101,4 +108,31 @@ public class GBDevice {
PEBBLE,
MIBAND
}
/**
* Ranges from 0-100 (percent)
* @return the battery level in range 0-100
*/
public short getBatteryLevel() {
return mBatteryLevel;
}
public void setBatteryLevel(short batteryLevel) {
if (mBatteryLevel >= 0 && mBatteryLevel <= 100) {
mBatteryLevel = batteryLevel;
} else {
Log.e(TAG, "Battery level musts be within range 0-100: " + batteryLevel);
}
}
/**
* Returns a string representation of the battery state.
*/
public String getBatteryState() {
return mBatteryState != null ? mBatteryState : "(unknown)";
}
public void setBatteryState(String batteryState) {
mBatteryState = batteryState;
}
}

View File

@ -0,0 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.miband;
public class AbstractInfo {
protected byte[] mData;
public AbstractInfo(byte[] data) {
mData = new byte[data.length];
System.arraycopy(data, 0, mData, 0, data.length);
}
}

View File

@ -0,0 +1,32 @@
package nodomain.freeyourgadget.gadgetbridge.miband;
public class BatteryInfo extends AbstractInfo {
public BatteryInfo(byte[] data) {
super(data);
}
public int getLevelInPercent() {
if (mData.length >= 1) {
return mData[0];
}
return 50; // actually unknown
}
// TODO: localization
public String getStatus() {
if (mData.length >= 10) {
int value = mData[9];
switch (value) {
case 1:
return "low";
case 2:
return "medium";
case 3:
return "full";
case 4:
return "not charging";
}
}
return "(unknown)";
}
}

View File

@ -0,0 +1,17 @@
package nodomain.freeyourgadget.gadgetbridge.miband;
import java.util.Locale;
public class DeviceInfo extends AbstractInfo {
public DeviceInfo(byte[] data) {
super(data);
}
public String getFirmwareVersion() {
if (mData.length == 16) {
int last = 15;
return String.format(Locale.US, "%d.%d.%d.%d", mData[last], mData[last-1], mData[last-2], mData[last-3]);
}
return "(unknown)"; // TODO: localization
}
}

View File

@ -1,9 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.miband;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;
import java.io.IOException;
import java.util.UUID;
@ -11,6 +7,9 @@ import nodomain.freeyourgadget.gadgetbridge.GBCommand;
import nodomain.freeyourgadget.gadgetbridge.GBDevice.State;
import nodomain.freeyourgadget.gadgetbridge.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.btle.TransactionBuilder;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;
public class MiBandSupport extends AbstractBTLEDeviceSupport {
@ -142,8 +141,13 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
@Override
public void onFirmwareVersionReq() {
// TODO Auto-generated method stub
try {
TransactionBuilder builder = performInitialized("Get MI Band Device Info");
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_DEVICE_INFO);
builder.read(characteristic).queue(getQueue());
} catch (IOException ex) {
Log.e(TAG, "Unable to read device info from MI", ex);
}
}
@Override
@ -164,6 +168,23 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if (MiBandService.UUID_CHARACTERISTIC_DEVICE_INFO.equals(characteristic.getUuid())) {
handleDeviceInfo(characteristic.getValue(), status);
}
}
private void handleDeviceInfo(byte[] value, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
getDevice().setFirmwareVersion(value.length + ":" + new String(value));
getDevice().sendDeviceUpdateIntent(getContext());
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
@ -198,7 +219,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
if (pairResult.length == 1) {
try {
byte b = pairResult[0];
Integer intValue = Integer.valueOf((int) b);
Integer intValue = Integer.valueOf(b);
if (intValue.intValue() == 2) {
Log.i(TAG, "Successfully paired MI device");
return;