From 483ef27a4f6c5822bbb7ce415f952457d1248f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sun, 30 Apr 2023 17:03:05 +0100 Subject: [PATCH] Zepp OS: Set region on user info This affects available features (eg. Alexa). Defaults to the previous value of "unknown" for now, and no UI. Alexa requires a region where it is available, such as Germany ("de"). --- .../DeviceSettingsPreferenceConst.java | 1 + .../devices/huami/Huami2021Service.java | 7 ++++++ .../service/btle/BLETypeConversions.java | 13 +++++++++++ .../devices/huami/Huami2021Support.java | 22 ++++++++++++++----- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index c3ca80002..8e992cebe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -45,6 +45,7 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_LANGUAGE = "language"; public static final String PREF_LANGUAGE_AUTO = "auto"; + public static final String PREF_DEVICE_REGION = "device_region"; public static final String PREF_DATEFORMAT = "dateformat"; public static final String PREF_TIMEFORMAT = "timeformat"; public static final String PREF_TIMEFORMAT_24H = "24h"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/Huami2021Service.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/Huami2021Service.java index 9bb84832a..e9b198b5f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/Huami2021Service.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huami/Huami2021Service.java @@ -256,4 +256,11 @@ public class Huami2021Service { */ public static final byte WEATHER_CMD_SET_DEFAULT_LOCATION = 0x09; public static final byte WEATHER_CMD_DEFAULT_LOCATION_ACK = 0x0a; + + /** + * User Info, for {@link Huami2021Service#CHUNKED2021_ENDPOINT_USER_INFO}. + */ + public static final byte USER_INFO_CMD_SET = 0x01; + public static final byte USER_INFO_CMD_SET_ACK = 0x02; + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLETypeConversions.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLETypeConversions.java index 1fea73174..7814442e6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLETypeConversions.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BLETypeConversions.java @@ -212,6 +212,19 @@ public class BLETypeConversions { }; } + public static byte[] fromUint64(long value) { + return new byte[] { + (byte) (value & 0xff), + (byte) ((value >> 8) & 0xff), + (byte) ((value >> 16) & 0xff), + (byte) ((value >> 24) & 0xff), + (byte) ((value >> 32) & 0xff), + (byte) ((value >> 40) & 0xff), + (byte) ((value >> 48) & 0xff), + (byte) ((value >> 56) & 0xff), + }; + } + public static byte fromUint8(int value) { return (byte) (value & 0xff); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java index de1f9c474..2da14fa18 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/Huami2021Support.java @@ -517,6 +517,8 @@ public abstract class Huami2021Support extends HuamiSupport { LOG.info("Attempting to set user info..."); final Prefs prefs = GBApplication.getPrefs(); + final Prefs devicePrefs = getDevicePrefs(); + final String alias = prefs.getString(PREF_USER_NAME, null); final ActivityUser activityUser = new ActivityUser(); final int height = activityUser.getHeightCm(); @@ -524,6 +526,7 @@ public abstract class Huami2021Support extends HuamiSupport { final int birthYear = activityUser.getYearOfBirth(); final byte birthMonth = 7; // not in user attributes final byte birthDay = 1; // not in user attributes + final String region = devicePrefs.getString(DeviceSettingsPreferenceConst.PREF_DEVICE_REGION, "unknown"); if (alias == null || weight == 0 || height == 0 || birthYear == 0) { LOG.warn("Unable to set user info, make sure it is set up"); @@ -543,16 +546,18 @@ public abstract class Huami2021Support extends HuamiSupport { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - baos.write(new byte[]{0x01, 0x4f, 0x07, 0x00, 0x00}); + baos.write(USER_INFO_CMD_SET); + baos.write(new byte[]{0x4f, 0x07, 0x00, 0x00}); baos.write(fromUint16(birthYear)); baos.write(birthMonth); baos.write(birthDay); baos.write(genderByte); - baos.write((byte) height); - baos.write((byte) 0); // TODO ? + baos.write(fromUint16(height)); baos.write(fromUint16(weight * 200)); - baos.write(BLETypeConversions.fromUint32(userid)); - baos.write(new byte[]{0x00, 0x00, 0x00, 0x00, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x00, 0x09}); // TODO ? + baos.write(BLETypeConversions.fromUint64(userid)); + baos.write(region.getBytes(StandardCharsets.UTF_8)); + baos.write(0); + baos.write(0x09); // TODO ? baos.write(alias.getBytes(StandardCharsets.UTF_8)); baos.write((byte) 0); @@ -2501,7 +2506,12 @@ public abstract class Huami2021Support extends HuamiSupport { } protected void handle2021UserInfo(final byte[] payload) { - // TODO handle2021UserInfo + switch (payload[0]) { + case USER_INFO_CMD_SET_ACK: + LOG.info("Got user info set ack, status = {}", payload[1]); + return; + } + LOG.warn("Unexpected user info payload byte {}", String.format("0x%02x", payload[0])); }