Da Fit: Add time sync

This commit is contained in:
krzys-h 2019-12-28 14:11:42 +01:00 committed by Arjan Schrijver
parent ca7d9e19af
commit ae6983e2ad
2 changed files with 50 additions and 1 deletions

View File

@ -16,6 +16,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.dafit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
@ -255,4 +259,33 @@ public class DaFitConstants {
public static final byte TRAINING_TYPE_TENNIS = 9;
public static final byte TRAINING_TYPE_RUGBY = 10;
public static final byte TRAINING_TYPE_GOLF = 11;
// The watch stores all dates in GMT+8 time zone with seconds resolution
// These helper functions convert between the watch time representation and local system representation
public static int LocalTimeToWatchTime(Date localTime)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
String format = simpleDateFormat.format(localTime);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
try {
return (int)(simpleDateFormat.parse(format).getTime() / 1000);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
public static Date WatchTimeToLocalTime(int watchTime)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String format = simpleDateFormat.format(new Date((long)watchTime * 1000));
simpleDateFormat.setTimeZone(TimeZone.getDefault());
try {
return simpleDateFormat.parse(format);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
}

View File

@ -27,7 +27,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
@ -102,6 +104,7 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
builder.notify(getCharacteristic(DaFitConstants.UUID_CHARACTERISTIC_DATA_IN), true);
deviceInfoProfile.requestDeviceInfo(builder);
setTime(builder);
batteryInfoProfile.requestBatteryInfo(builder);
batteryInfoProfile.enableNotify(builder);
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
@ -197,9 +200,22 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport {
// not supported :(
}
private void setTime(TransactionBuilder builder) {
ByteBuffer buffer = ByteBuffer.allocate(5);
buffer.putInt(DaFitConstants.LocalTimeToWatchTime(new Date())); // The watch is hardcoded to GMT+8 internally...
buffer.put((byte)8); // I guess this means GMT+8 but changing it has no effect at all (it was hardcoded in the original app too)
sendPacket(builder, DaFitPacketOut.buildPacket(DaFitConstants.CMD_SYNC_TIME, buffer.array()));
}
@Override
public void onSetTime() {
// TODO
try {
TransactionBuilder builder = performInitialized("onSetTime");
setTime(builder);
builder.queue(getQueue());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override