1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +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,26 +4,46 @@ import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
public class MiBandDateConverter { 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 * uses the standard algorithm to convert bytes received from the MiBand to a Calendar object
* @param value * @param value
* @return * @return
*/ */
public static GregorianCalendar rawBytesToCalendar(byte[] value) { public static GregorianCalendar rawBytesToCalendar(byte[] value) {
GregorianCalendar timestamp = new GregorianCalendar();
if (value.length == 6) { if (value.length == 6) {
timestamp.set(Calendar.YEAR, (2000 + value[0])); return rawBytesToCalendar(value, 0);
timestamp.set(Calendar.MONTH, value[1]); }
timestamp.set(Calendar.DATE, value[2]); return createCalendar();
timestamp.set(Calendar.HOUR_OF_DAY, value[3]);
timestamp.set(Calendar.MINUTE, value[4]);
timestamp.set(Calendar.SECOND, value[5]);
} }
/**
* 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();
}
/** /**
* uses the standard algorithm to convert a Calendar object to a byte array to send to MiBand * uses the standard algorithm to convert a Calendar object to a byte array to send to MiBand
* @param timestamp * @param timestamp

View File

@ -43,7 +43,7 @@ public class BatteryInfo extends AbstractInfo {
} }
public GregorianCalendar getLastChargeTime() { public GregorianCalendar getLastChargeTime() {
GregorianCalendar lastCharge = new GregorianCalendar(); GregorianCalendar lastCharge = MiBandDateConverter.createCalendar();
if (mData.length >= 10) { if (mData.length >= 10) {
lastCharge = MiBandDateConverter.rawBytesToCalendar(new byte[]{ 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 // byte 0 is the data type: 1 means that each minute is represented by a triplet of bytes
int dataType = value[0]; int dataType = value[0];
// byte 1 to 6 represent a timestamp // 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 // counter of all data held by the band
int totalDataToRead = (value[7] & 0xff) | ((value[8] & 0xff) << 8); 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); 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;
}
} }