1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Reuse MiBandDateConverter

This commit is contained in:
cpfeiffer 2015-09-19 21:54:01 +02:00
parent 77b4bb9cf1
commit 3852fcd756
3 changed files with 31 additions and 22 deletions

View File

@ -4,24 +4,44 @@ import java.util.Calendar;
import java.util.GregorianCalendar;
public class MiBandDateConverter {
/**
* Creates a calendar object representing the current date and time.
*/
public static GregorianCalendar createCalendar() {
return new GregorianCalendar();
}
/**
* uses the standard algorithm to convert bytes received from the MiBand to a Calendar object
* @param value
* @return
*/
public static GregorianCalendar rawBytesToCalendar(byte[] value) {
GregorianCalendar timestamp = new GregorianCalendar();
if (value.length == 6) {
timestamp.set(Calendar.YEAR, (2000 + value[0]));
timestamp.set(Calendar.MONTH, value[1]);
timestamp.set(Calendar.DATE, value[2]);
timestamp.set(Calendar.HOUR_OF_DAY, value[3]);
timestamp.set(Calendar.MINUTE, value[4]);
timestamp.set(Calendar.SECOND, value[5]);
return rawBytesToCalendar(value, 0);
}
return createCalendar();
}
/**
* uses the standard algorithm to convert bytes received from the MiBand to a Calendar object
* @param value
* @return
*/
public static GregorianCalendar rawBytesToCalendar(byte[] value, int offset) {
if (value.length - offset >= 6) {
GregorianCalendar timestamp = new GregorianCalendar(
value[offset] + 2000,
value[offset + 1],
value[offset + 2],
value[offset + 3],
value[offset + 4],
value[offset + 5]);
return timestamp;
}
return timestamp;
return createCalendar();
}
/**

View File

@ -43,7 +43,7 @@ public class BatteryInfo extends AbstractInfo {
}
public GregorianCalendar getLastChargeTime() {
GregorianCalendar lastCharge = new GregorianCalendar();
GregorianCalendar lastCharge = MiBandDateConverter.createCalendar();
if (mData.length >= 10) {
lastCharge = MiBandDateConverter.rawBytesToCalendar(new byte[]{

View File

@ -107,7 +107,7 @@ public class FetchActivityOperation extends AbstractBTLEOperation<MiBandSupport>
// byte 0 is the data type: 1 means that each minute is represented by a triplet of bytes
int dataType = value[0];
// byte 1 to 6 represent a timestamp
GregorianCalendar timestamp = parseTimestamp(value, 1);
GregorianCalendar timestamp = MiBandDateConverter.rawBytesToCalendar(value, 1);
// counter of all data held by the band
int totalDataToRead = (value[7] & 0xff) | ((value[8] & 0xff) << 8);
@ -273,15 +273,4 @@ public class FetchActivityOperation extends AbstractBTLEOperation<MiBandSupport>
LOG.error("Unable to send ack to MI", ex);
}
}
private GregorianCalendar parseTimestamp(byte[] value, int offset) {
GregorianCalendar timestamp = new GregorianCalendar(
value[offset] + 2000,
value[offset + 1],
value[offset + 2],
value[offset + 3],
value[offset + 4],
value[offset + 5]);
return timestamp;
}
}