mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-02-10 23:57:31 +01:00
Da Fit: Add time sync
This commit is contained in:
parent
817b3ca1bc
commit
9e33fc2b88
@ -16,6 +16,10 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
package nodomain.freeyourgadget.gadgetbridge.devices.dafit;
|
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 java.util.UUID;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
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_TENNIS = 9;
|
||||||
public static final byte TRAINING_TYPE_RUGBY = 10;
|
public static final byte TRAINING_TYPE_RUGBY = 10;
|
||||||
public static final byte TRAINING_TYPE_GOLF = 11;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,9 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -102,6 +104,7 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport {
|
|||||||
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
|
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
|
||||||
builder.notify(getCharacteristic(DaFitConstants.UUID_CHARACTERISTIC_DATA_IN), true);
|
builder.notify(getCharacteristic(DaFitConstants.UUID_CHARACTERISTIC_DATA_IN), true);
|
||||||
deviceInfoProfile.requestDeviceInfo(builder);
|
deviceInfoProfile.requestDeviceInfo(builder);
|
||||||
|
setTime(builder);
|
||||||
batteryInfoProfile.requestBatteryInfo(builder);
|
batteryInfoProfile.requestBatteryInfo(builder);
|
||||||
batteryInfoProfile.enableNotify(builder);
|
batteryInfoProfile.enableNotify(builder);
|
||||||
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
|
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
|
||||||
@ -197,9 +200,22 @@ public class DaFitDeviceSupport extends AbstractBTLEDeviceSupport {
|
|||||||
// not supported :(
|
// 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
|
@Override
|
||||||
public void onSetTime() {
|
public void onSetTime() {
|
||||||
// TODO
|
try {
|
||||||
|
TransactionBuilder builder = performInitialized("onSetTime");
|
||||||
|
setTime(builder);
|
||||||
|
builder.queue(getQueue());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user