mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-10 10:44:47 +02:00
if the Mi Scale device is not already initialized then set weight measurement on and set the current time
This commit is contained in:
@@ -53,8 +53,9 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
private Handler searchHandler;
|
private Handler searchHandler;
|
||||||
private Context context;
|
private Context context;
|
||||||
private String btDeviceName;
|
private String btDeviceName;
|
||||||
private int nextState;
|
private int nextCmdState;
|
||||||
private boolean restartDiscovery;;
|
private int nextInitState;
|
||||||
|
private boolean initProcessOn;
|
||||||
|
|
||||||
public BluetoothMiScale(Context con) {
|
public BluetoothMiScale(Context con) {
|
||||||
searchHandler = new Handler();
|
searchHandler = new Handler();
|
||||||
@@ -130,8 +131,9 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
|
public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
|
||||||
restartDiscovery = false;
|
nextCmdState = 0;
|
||||||
nextState = 0;
|
nextInitState = 0;
|
||||||
|
initProcessOn = false;
|
||||||
|
|
||||||
invokeNextBluetoothCmd(gatt);
|
invokeNextBluetoothCmd(gatt);
|
||||||
}
|
}
|
||||||
@@ -140,9 +142,9 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
BluetoothGattCharacteristic characteristic;
|
BluetoothGattCharacteristic characteristic;
|
||||||
BluetoothGattDescriptor descriptor;
|
BluetoothGattDescriptor descriptor;
|
||||||
|
|
||||||
Log.d("BluetoothMiScale", "Bluetooth Cmd State: " + nextState);
|
Log.d("BluetoothMiScale", "Cmd State " + nextCmdState);
|
||||||
|
|
||||||
switch (nextState) {
|
switch (nextCmdState) {
|
||||||
case 0:
|
case 0:
|
||||||
// read device time
|
// read device time
|
||||||
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
|
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
|
||||||
@@ -188,34 +190,93 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
characteristic.setValue(new byte[]{0x03});
|
characteristic.setValue(new byte[]{0x03});
|
||||||
gatt.writeCharacteristic(characteristic);*/
|
gatt.writeCharacteristic(characteristic);*/
|
||||||
break;
|
break;
|
||||||
case 5:
|
|
||||||
// Stop state machine
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
Log.e("BluetoothMiScale", "Error invalid Bluetooth State");
|
Log.e("BluetoothMiScale", "Error invalid Bluetooth State");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void invokeInitBluetoothCmd(BluetoothGatt gatt) {
|
||||||
|
BluetoothGattCharacteristic characteristic;
|
||||||
|
BluetoothGattDescriptor descriptor;
|
||||||
|
|
||||||
|
initProcessOn = true;
|
||||||
|
|
||||||
|
Log.d("BluetoothMiScale", "Init State " + nextInitState);
|
||||||
|
|
||||||
|
switch (nextInitState) {
|
||||||
|
case 0:
|
||||||
|
// set current time
|
||||||
|
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
|
||||||
|
.getCharacteristic(WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC);
|
||||||
|
|
||||||
|
Log.d("BluetoothMiScale", "Set current time on Mi scale");
|
||||||
|
|
||||||
|
Calendar currentDateTime = Calendar.getInstance();
|
||||||
|
int year = currentDateTime.get(Calendar.YEAR);
|
||||||
|
byte month = (byte)(currentDateTime.get(Calendar.MONTH)+1);
|
||||||
|
byte day = (byte)currentDateTime.get(Calendar.DAY_OF_MONTH);
|
||||||
|
byte hour = (byte)currentDateTime.get(Calendar.HOUR_OF_DAY);
|
||||||
|
byte min = (byte)currentDateTime.get(Calendar.MINUTE);
|
||||||
|
byte sec = (byte)currentDateTime.get(Calendar.SECOND);
|
||||||
|
|
||||||
|
byte[] dateTimeByte = {(byte)(year), (byte)(year >> 8), month, day, hour, min, sec, 0x03, 0x00, 0x00};
|
||||||
|
|
||||||
|
characteristic.setValue(dateTimeByte);
|
||||||
|
gatt.writeCharacteristic(characteristic);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// 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 2:
|
||||||
|
// Set on history weight measurement
|
||||||
|
characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
|
||||||
|
.getCharacteristic(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC);
|
||||||
|
|
||||||
|
characteristic.setValue(new byte[]{(byte)0x01, (byte)0x96, (byte)0x8a, (byte)0xbd, (byte)0x62});
|
||||||
|
gatt.writeCharacteristic(characteristic);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
initProcessOn = false;
|
||||||
|
|
||||||
|
stopSearching();
|
||||||
|
startSearching(btDeviceName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDescriptorWrite(BluetoothGatt gatt,
|
public void onDescriptorWrite(BluetoothGatt gatt,
|
||||||
BluetoothGattDescriptor descriptor,
|
BluetoothGattDescriptor descriptor,
|
||||||
int status) {
|
int status) {
|
||||||
nextState++;
|
if (initProcessOn) {
|
||||||
invokeNextBluetoothCmd(gatt);
|
nextInitState++;
|
||||||
|
invokeInitBluetoothCmd(gatt);
|
||||||
|
} else {
|
||||||
|
nextCmdState++;
|
||||||
|
invokeNextBluetoothCmd(gatt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCharacteristicWrite (BluetoothGatt gatt,
|
public void onCharacteristicWrite (BluetoothGatt gatt,
|
||||||
BluetoothGattCharacteristic characteristic,
|
BluetoothGattCharacteristic characteristic,
|
||||||
int status) {
|
int status) {
|
||||||
if (restartDiscovery) {
|
if (initProcessOn) {
|
||||||
stopSearching();
|
nextInitState++;
|
||||||
startSearching(btDeviceName);
|
invokeInitBluetoothCmd(gatt);
|
||||||
|
} else {
|
||||||
|
nextCmdState++;
|
||||||
|
invokeNextBluetoothCmd(gatt);
|
||||||
}
|
}
|
||||||
|
|
||||||
nextState++;
|
|
||||||
invokeNextBluetoothCmd(gatt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -229,9 +290,9 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
|
|
||||||
if (currentYear != scaleYear) {
|
if (currentYear != scaleYear) {
|
||||||
Log.d("BluetoothMiScale", "Current year and scale year is different");
|
Log.d("BluetoothMiScale", "Current year and scale year is different");
|
||||||
setCurrentTimeOnDevice(gatt);
|
invokeInitBluetoothCmd(gatt);
|
||||||
} else {
|
} else {
|
||||||
nextState++;
|
nextCmdState++;
|
||||||
invokeNextBluetoothCmd(gatt);
|
invokeNextBluetoothCmd(gatt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,10 +302,6 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
BluetoothGattCharacteristic characteristic) {
|
BluetoothGattCharacteristic characteristic) {
|
||||||
final byte[] data = characteristic.getValue();
|
final byte[] data = characteristic.getValue();
|
||||||
|
|
||||||
Log.d("BluetoothMiScale", "Char changed");
|
|
||||||
|
|
||||||
printByteInHex(data);
|
|
||||||
|
|
||||||
if (data != null && data.length > 0) {
|
if (data != null && data.length > 0) {
|
||||||
if (data.length == 20) {
|
if (data.length == 20) {
|
||||||
final byte[] firstWeight = Arrays.copyOfRange(data, 0, 10);
|
final byte[] firstWeight = Arrays.copyOfRange(data, 0, 10);
|
||||||
@@ -260,29 +317,6 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCurrentTimeOnDevice(BluetoothGatt gatt) {
|
|
||||||
// set current time
|
|
||||||
BluetoothGattCharacteristic characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE)
|
|
||||||
.getCharacteristic(WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC);
|
|
||||||
|
|
||||||
Log.d("BluetoothMiScale", "Set current time on Mi scale");
|
|
||||||
|
|
||||||
Calendar currentDateTime = Calendar.getInstance();
|
|
||||||
int year = currentDateTime.get(Calendar.YEAR);
|
|
||||||
byte month = (byte)(currentDateTime.get(Calendar.MONTH)+1);
|
|
||||||
byte day = (byte)currentDateTime.get(Calendar.DAY_OF_MONTH);
|
|
||||||
byte hour = (byte)currentDateTime.get(Calendar.HOUR_OF_DAY);
|
|
||||||
byte min = (byte)currentDateTime.get(Calendar.MINUTE);
|
|
||||||
byte sec = (byte)currentDateTime.get(Calendar.SECOND);
|
|
||||||
|
|
||||||
byte[] dateTimeByte = {(byte)(year), (byte)(year >> 8), month, day, hour, min, sec, 0x03, 0x00, 0x00};
|
|
||||||
|
|
||||||
restartDiscovery = true;
|
|
||||||
|
|
||||||
characteristic.setValue(dateTimeByte);
|
|
||||||
gatt.writeCharacteristic(characteristic);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseBytes(byte[] weightBytes) {
|
private void parseBytes(byte[] weightBytes) {
|
||||||
try {
|
try {
|
||||||
float weight = 0.0f;
|
float weight = 0.0f;
|
||||||
@@ -322,17 +356,38 @@ public class BluetoothMiScale extends BluetoothCommunication {
|
|||||||
String date_string = year + "/" + month + "/" + day + "/" + hours + "/" + min;
|
String date_string = year + "/" + month + "/" + day + "/" + hours + "/" + min;
|
||||||
Date date_time = new SimpleDateFormat("yyyy/MM/dd/HH/mm").parse(date_string);
|
Date date_time = new SimpleDateFormat("yyyy/MM/dd/HH/mm").parse(date_string);
|
||||||
|
|
||||||
ScaleData scaleBtData = new ScaleData();
|
// Is the year plausible? Check if the year is in the range of 20 years...
|
||||||
|
if (validateDate(date_time, 20)) {
|
||||||
|
ScaleData scaleBtData = new ScaleData();
|
||||||
|
|
||||||
scaleBtData.weight = weight;
|
scaleBtData.weight = weight;
|
||||||
scaleBtData.date_time = date_time;
|
scaleBtData.date_time = date_time;
|
||||||
|
|
||||||
callbackBtHandler.obtainMessage(BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, scaleBtData).sendToTarget();
|
callbackBtHandler.obtainMessage(BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, scaleBtData).sendToTarget();
|
||||||
|
} else {
|
||||||
|
Log.e("BluetoothMiScale", "Invalid weight year " + year);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")").sendToTarget();
|
callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")").sendToTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean validateDate(Date weightDate, int range) {
|
||||||
|
|
||||||
|
Calendar currentDatePos = Calendar.getInstance();
|
||||||
|
currentDatePos.add(Calendar.YEAR, range);
|
||||||
|
|
||||||
|
Calendar currentDateNeg = Calendar.getInstance();
|
||||||
|
currentDateNeg.add(Calendar.YEAR, -range);
|
||||||
|
|
||||||
|
if (weightDate.before(currentDatePos.getTime()) && weightDate.after(currentDateNeg.getTime())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isBitSet(byte value, int bit) {
|
private boolean isBitSet(byte value, int bit) {
|
||||||
return (value & (1 << bit)) != 0;
|
return (value & (1 << bit)) != 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user