1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-13 12:14:19 +02:00

get only last measurements of the mi scale

use another up and down symbol to avoid using bitmaps in android version > 6.x
This commit is contained in:
OliE
2017-05-21 17:30:51 +02:00
parent 10ced5bbc9
commit 7d08b98c49
2 changed files with 85 additions and 15 deletions

View File

@@ -52,7 +52,9 @@ public class BluetoothMiScale extends BluetoothCommunication {
private String btDeviceName; private String btDeviceName;
private int nextCmdState; private int nextCmdState;
private int nextInitState; private int nextInitState;
private int nextClearUpState;
private boolean initProcessOn; private boolean initProcessOn;
private boolean clearUpProcessOn;
public BluetoothMiScale(Context con) { public BluetoothMiScale(Context con) {
searchHandler = new Handler(); searchHandler = new Handler();
@@ -76,10 +78,6 @@ public class BluetoothMiScale extends BluetoothCommunication {
if (device.getName().equals(btDeviceName)) { if (device.getName().equals(btDeviceName)) {
Log.d("BluetoothMiScale", "Mi Scale found trying to connect..."); Log.d("BluetoothMiScale", "Mi Scale found trying to connect...");
final byte[] weightData = Arrays.copyOfRange(scanRecord, 21, 31);
weightData[0] = 0x62; // Set weight remove to false to come through parse bytes
parseBytes(weightData);
bluetoothGatt = device.connectGatt(context, false, gattCallback); bluetoothGatt = device.connectGatt(context, false, gattCallback);
searchHandler.removeCallbacksAndMessages(null); searchHandler.removeCallbacksAndMessages(null);
@@ -135,7 +133,7 @@ public class BluetoothMiScale extends BluetoothCommunication {
Log.d("GattCallback", "3 LSB Unknown: " + isBitSet(ctrlByte, 3)); Log.d("GattCallback", "3 LSB Unknown: " + isBitSet(ctrlByte, 3));
Log.d("GattCallback", "2 LSB Unknown: " + isBitSet(ctrlByte, 2)); Log.d("GattCallback", "2 LSB Unknown: " + isBitSet(ctrlByte, 2));
Log.d("GattCallback", "1 LSB Unknown: " + isBitSet(ctrlByte, 1)); Log.d("GattCallback", "1 LSB Unknown: " + isBitSet(ctrlByte, 1));
Log.d("GattCallback", "IsLBS: " + isBitSet(ctrlByte, 0)); */ Log.d("GattCallback", "IsLBS: " + isBitSet(ctrlByte, 0));*/
// Only if the value is stabilized and the weight is *not* removed, the date is valid // Only if the value is stabilized and the weight is *not* removed, the date is valid
if (isStabilized && !isWeightRemoved) { if (isStabilized && !isWeightRemoved) {
@@ -224,7 +222,10 @@ public class BluetoothMiScale extends BluetoothCommunication {
public void onServicesDiscovered(final BluetoothGatt gatt, int status) { public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
nextCmdState = 0; nextCmdState = 0;
nextInitState = 0; nextInitState = 0;
nextClearUpState = 0;
initProcessOn = false; initProcessOn = false;
clearUpProcessOn = false;
invokeNextBluetoothCmd(gatt); invokeNextBluetoothCmd(gatt);
} }
@@ -264,20 +265,76 @@ public class BluetoothMiScale extends BluetoothCommunication {
gatt.writeDescriptor(descriptor); gatt.writeDescriptor(descriptor);
break; break;
case 3: case 3:
// Invoke receiving history data // configure scale to get only last measurements
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
characteristic.setValue(new byte[]{(byte)0x01, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
gatt.writeCharacteristic(characteristic);
break;
case 4:
// set notification off for weight measurement history
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
gatt.setCharacteristicNotification(characteristic, false);
descriptor = characteristic.getDescriptor(WEIGHT_MEASUREMENT_CONFIG);
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
break;
case 5:
// set notification on for weight measurement history
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
gatt.setCharacteristicNotification(characteristic, true);
descriptor = characteristic.getDescriptor(WEIGHT_MEASUREMENT_CONFIG);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
break;
case 6:
// invoke receiving history data
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE) characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC); .getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
characteristic.setValue(new byte[]{0x02}); characteristic.setValue(new byte[]{0x02});
gatt.writeCharacteristic(characteristic); gatt.writeCharacteristic(characteristic);
break; break;
case 4: case 7:
// Stop receiving history data // end of command mode
/*characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE) break;
default:
Log.e("BluetoothMiScale", "Error invalid Bluetooth State");
break;
}
}
private void invokeClearUpBluetooth(BluetoothGatt gatt) {
BluetoothGattCharacteristic characteristic;
clearUpProcessOn = true;
switch (nextClearUpState) {
case 0:
// send stop command to mi scale
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC); .getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
characteristic.setValue(new byte[]{0x03}); characteristic.setValue(new byte[]{0x03});
gatt.writeCharacteristic(characteristic);*/ gatt.writeCharacteristic(characteristic);
break;
case 1:
// acknowledge that you received the last history data
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
characteristic.setValue(new byte[]{(byte)0x04, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF});
gatt.writeCharacteristic(characteristic);
break;
case 2:
// end of clear up process
break; break;
default: default:
Log.e("BluetoothMiScale", "Error invalid Bluetooth State"); Log.e("BluetoothMiScale", "Error invalid Bluetooth State");
@@ -349,7 +406,11 @@ public class BluetoothMiScale extends BluetoothCommunication {
if (initProcessOn) { if (initProcessOn) {
nextInitState++; nextInitState++;
invokeInitBluetoothCmd(gatt); invokeInitBluetoothCmd(gatt);
} else { } else if (clearUpProcessOn) {
nextClearUpState++;
invokeClearUpBluetooth(gatt);
}
else {
nextCmdState++; nextCmdState++;
invokeNextBluetoothCmd(gatt); invokeNextBluetoothCmd(gatt);
} }
@@ -362,7 +423,10 @@ public class BluetoothMiScale extends BluetoothCommunication {
if (initProcessOn) { if (initProcessOn) {
nextInitState++; nextInitState++;
invokeInitBluetoothCmd(gatt); invokeInitBluetoothCmd(gatt);
} else { } else if (clearUpProcessOn) {
nextClearUpState++;
invokeClearUpBluetooth(gatt);
}else {
nextCmdState++; nextCmdState++;
invokeNextBluetoothCmd(gatt); invokeNextBluetoothCmd(gatt);
} }
@@ -396,6 +460,12 @@ public class BluetoothMiScale extends BluetoothCommunication {
final byte[] data = characteristic.getValue(); final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) { if (data != null && data.length > 0) {
// Stop command from mi scale received
if (data[0] == 0x03) {
invokeClearUpBluetooth(gatt);
}
if (data.length == 20) { if (data.length == 20) {
final byte[] firstWeight = Arrays.copyOfRange(data, 0, 10); final byte[] firstWeight = Arrays.copyOfRange(data, 0, 10);
final byte[] secondWeight = Arrays.copyOfRange(data, 10, 20); final byte[] secondWeight = Arrays.copyOfRange(data, 10, 20);

View File

@@ -51,9 +51,9 @@ import static com.health.openscale.gui.views.MeasurementView.MeasurementViewMode
public abstract class MeasurementView extends TableLayout { public abstract class MeasurementView extends TableLayout {
public enum MeasurementViewMode {VIEW, EDIT, ADD}; public enum MeasurementViewMode {VIEW, EDIT, ADD};
private static String SYMBOL_UP = "↗"; private static String SYMBOL_UP = "➚";
private static String SYMBOL_NEUTRAL = "&#x2192"; private static String SYMBOL_NEUTRAL = "➙";
private static String SYMBOL_DOWN = "↘"; private static String SYMBOL_DOWN = "➘";
private TableRow measurementRow; private TableRow measurementRow;
private ImageView iconView; private ImageView iconView;