mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-26 10:35:50 +01:00
Centralize the calendar conversion to/from byte array.
This commit is contained in:
parent
7923e153e6
commit
677e0808bf
@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
||||
@ -130,4 +131,40 @@ public class MiBandCoordinator implements DeviceCoordinator {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
|
||||
return Integer.parseInt(prefs.getString(MiBandConst.PREF_MIBAND_FITNESS_GOAL, "10000"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* uses the standard algorithm to convert a Calendar object to a byte array to send to MiBand
|
||||
* @param timestamp
|
||||
* @return
|
||||
*/
|
||||
public static byte[] calendarToRawBytes(Calendar timestamp) {
|
||||
return new byte[]{
|
||||
(byte) (timestamp.get(Calendar.YEAR) - 2000),
|
||||
(byte) timestamp.get(Calendar.MONTH),
|
||||
(byte) timestamp.get(Calendar.DATE),
|
||||
(byte) timestamp.get(Calendar.HOUR_OF_DAY),
|
||||
(byte) timestamp.get(Calendar.MINUTE),
|
||||
(byte) timestamp.get(Calendar.SECOND)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
|
||||
|
||||
public class BatteryInfo extends AbstractInfo {
|
||||
@ -46,12 +46,9 @@ public class BatteryInfo extends AbstractInfo {
|
||||
GregorianCalendar lastCharge = new GregorianCalendar();
|
||||
|
||||
if (mData.length >= 10) {
|
||||
lastCharge.set(Calendar.YEAR, (2000 + mData[1]));
|
||||
lastCharge.set(Calendar.MONTH, mData[2]);
|
||||
lastCharge.set(Calendar.DATE, mData[3]);
|
||||
lastCharge.set(Calendar.HOUR_OF_DAY, mData[4]);
|
||||
lastCharge.set(Calendar.MINUTE, mData[5]);
|
||||
lastCharge.set(Calendar.SECOND, mData[6]);
|
||||
lastCharge = MiBandCoordinator.rawBytesToCalendar(new byte[]{
|
||||
mData[1], mData[2], mData[3], mData[4], mData[5], mData[6]
|
||||
});
|
||||
}
|
||||
|
||||
return lastCharge;
|
||||
|
@ -13,7 +13,6 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -429,14 +428,14 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
* @param builder
|
||||
*/
|
||||
private MiBandSupport setCurrentTime(TransactionBuilder builder) {
|
||||
Calendar now = GregorianCalendar.getInstance();
|
||||
byte[] nowBytes = MiBandCoordinator.calendarToRawBytes(GregorianCalendar.getInstance());
|
||||
byte[] time = new byte[]{
|
||||
(byte) (now.get(Calendar.YEAR) - 2000),
|
||||
(byte) now.get(Calendar.MONTH),
|
||||
(byte) now.get(Calendar.DATE),
|
||||
(byte) now.get(Calendar.HOUR_OF_DAY),
|
||||
(byte) now.get(Calendar.MINUTE),
|
||||
(byte) now.get(Calendar.SECOND),
|
||||
nowBytes[0],
|
||||
nowBytes[1],
|
||||
nowBytes[2],
|
||||
nowBytes[3],
|
||||
nowBytes[4],
|
||||
nowBytes[5],
|
||||
(byte) 0x0f,
|
||||
(byte) 0x0f,
|
||||
(byte) 0x0f,
|
||||
@ -685,17 +684,18 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||
* @param characteristic
|
||||
*/
|
||||
private void queueAlarm(Alarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
|
||||
Calendar alarmCal = alarm.getAlarmCal();
|
||||
byte[] alarmCalBytes = MiBandCoordinator.calendarToRawBytes(alarm.getAlarmCal());
|
||||
|
||||
byte[] alarmMessage = new byte[]{
|
||||
(byte) MiBandService.COMMAND_SET_TIMER,
|
||||
(byte) alarm.getIndex(),
|
||||
(byte) (alarm.isEnabled() ? 1 : 0),
|
||||
(byte) (alarmCal.get(Calendar.YEAR) - 2000),
|
||||
(byte) alarmCal.get(Calendar.MONTH),
|
||||
(byte) alarmCal.get(Calendar.DATE),
|
||||
(byte) alarmCal.get(Calendar.HOUR_OF_DAY),
|
||||
(byte) alarmCal.get(Calendar.MINUTE),
|
||||
(byte) alarmCal.get(Calendar.SECOND),
|
||||
alarmCalBytes[0],
|
||||
alarmCalBytes[1],
|
||||
alarmCalBytes[2],
|
||||
alarmCalBytes[3],
|
||||
alarmCalBytes[4],
|
||||
alarmCalBytes[5],
|
||||
(byte) (alarm.isSmartWakeup() ? 30 : 0),
|
||||
(byte) alarm.getRepetitionMask()
|
||||
};
|
||||
|
@ -19,6 +19,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandCoordinator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||
@ -224,14 +225,15 @@ public class FetchActivityOperation extends AbstractBTLEOperation<MiBandSupport>
|
||||
* @param bytesTransferred
|
||||
*/
|
||||
private void sendAckDataTransfer(Calendar time, int bytesTransferred) {
|
||||
byte[] ackTime = MiBandCoordinator.calendarToRawBytes(time);
|
||||
byte[] ack = new byte[]{
|
||||
MiBandService.COMMAND_CONFIRM_ACTIVITY_DATA_TRANSFER_COMPLETE,
|
||||
(byte) (time.get(Calendar.YEAR) - 2000),
|
||||
(byte) time.get(Calendar.MONTH),
|
||||
(byte) time.get(Calendar.DATE),
|
||||
(byte) time.get(Calendar.HOUR_OF_DAY),
|
||||
(byte) time.get(Calendar.MINUTE),
|
||||
(byte) time.get(Calendar.SECOND),
|
||||
ackTime[0],
|
||||
ackTime[1],
|
||||
ackTime[2],
|
||||
ackTime[3],
|
||||
ackTime[4],
|
||||
ackTime[5],
|
||||
(byte) (bytesTransferred & 0xff),
|
||||
(byte) (0xff & (bytesTransferred >> 8))
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user