Skip to content

Commit

Permalink
fixed Mi scale v1 bug with RxAndroidBle library
Browse files Browse the repository at this point in the history
  • Loading branch information
oliexdev committed Jan 27, 2019
1 parent 2fe3be9 commit d5ba8b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ protected void writeBytes(UUID characteristic, byte[] bytes) {
/**
* Read bytes from a Bluetooth device.
*
* @note onBluetoothRead() will be triggered if read command was successful.
* @note onBluetoothRead() will be triggered if read command was successful. nextMachineStep() needs to manually called!
*@param characteristic the Bluetooth UUID characteristic
*/
protected void readBytes(UUID characteristic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import com.polidea.rxandroidble2.RxBleClient;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -65,6 +64,7 @@ public void onBluetoothRead(UUID characteristic, byte[] value) {
setBtMachineState(BT_MACHINE_STATE.BT_CMD_STATE);
} else {
Timber.d("Current year and scale year is different");
nextMachineStateStep();
}
}

Expand Down Expand Up @@ -99,8 +99,7 @@ protected boolean nextInitCmd(int stateNr) {
switch (stateNr) {
case 0:
// read device time
readBytes(
BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME);
readBytes(BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME);
break;
case 1:
// set current time
Expand All @@ -114,21 +113,13 @@ protected boolean nextInitCmd(int stateNr) {

byte[] dateTimeByte = {(byte)(year), (byte)(year >> 8), month, day, hour, min, sec, 0x03, 0x00, 0x00};

writeBytes(
BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME, dateTimeByte);
writeBytes(BluetoothGattUuid.CHARACTERISTIC_CURRENT_TIME, dateTimeByte);
break;
case 2:
// set notification on for weight measurement history
setNotificationOn(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC
);
break;
case 3:
// Set on history weight measurement
byte[] magicBytes = new byte[]{(byte)0x01, (byte)0x96, (byte)0x8a, (byte)0xbd, (byte)0x62};

writeBytes(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, magicBytes);
writeBytes(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, magicBytes);
break;
default:
return false;
Expand All @@ -142,34 +133,22 @@ protected boolean nextBluetoothCmd(int stateNr) {
switch (stateNr) {
case 0:
// set notification on for weight measurement history
setNotificationOn(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC
);
setNotificationOn(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
break;
case 1:
// set notification on for weight measurement
setNotificationOn(
BluetoothGattUuid.CHARACTERISTIC_WEIGHT_MEASUREMENT
);
setNotificationOn(BluetoothGattUuid.CHARACTERISTIC_WEIGHT_MEASUREMENT);
break;
case 2:
// configure scale to get only last measurements
int uniqueNumber = getUniqueNumber();

byte[] userIdentifier = new byte[]{(byte)0x01, (byte)0xFF, (byte)0xFF, (byte) ((uniqueNumber & 0xFF00) >> 8), (byte) ((uniqueNumber & 0xFF) >> 0)};
writeBytes(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, userIdentifier);
writeBytes(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, userIdentifier);
break;
case 3:
// set notification on for weight measurement history
setNotificationOn(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC
);
break;
case 4:
// invoke receiving history data
writeBytes(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, new byte[]{0x02});
writeBytes(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, new byte[]{0x02});
break;
default:
return false;
Expand All @@ -184,16 +163,14 @@ protected boolean nextCleanUpCmd(int stateNr) {
switch (stateNr) {
case 0:
// send stop command to mi scale
writeBytes(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, new byte[]{0x03});
writeBytes(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, new byte[]{0x03});
break;
case 1:
// acknowledge that you received the last history data
int uniqueNumber = getUniqueNumber();

byte[] userIdentifier = new byte[]{(byte)0x04, (byte)0xFF, (byte)0xFF, (byte) ((uniqueNumber & 0xFF00) >> 8), (byte) ((uniqueNumber & 0xFF) >> 0)};
writeBytes(
WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, userIdentifier);
writeBytes(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, userIdentifier);
break;
default:
return false;
Expand Down

0 comments on commit d5ba8b5

Please sign in to comment.