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