diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java index 2972de7a8..942972cb7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/dafit/DaFitConstants.java @@ -16,6 +16,10 @@ along with this program. If not, see . */ 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); + } + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java index 6f7192d94..bc313a00b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/dafit/DaFitDeviceSupport.java @@ -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