From e28085e6af2ec6e6383411db72d4edcefb60d286 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 24 Sep 2017 23:03:11 +0200 Subject: [PATCH] Mi Band 2/ Bip: Do not send user info if not set up --- .../devices/miband2/MiBand2Support.java | 88 ++++++++++--------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java index 57013778..318385a1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java @@ -373,51 +373,57 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { */ private MiBand2Support setUserInfo(TransactionBuilder transaction) { + BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_8_USER_SETTINGS); + if (characteristic == null) { + return this; + } + + LOG.info("Attempting to set user info..."); Prefs prefs = GBApplication.getPrefs(); String alias = prefs.getString(MiBandConst.PREF_USER_ALIAS, null); - LOG.info("Attempting to set user info..."); - BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_8_USER_SETTINGS); - if (characteristic != null && alias != null) { - ActivityUser activityUser = new ActivityUser(); - int birth_year = activityUser.getYearOfBirth(); - byte birth_month = 7; // not in user attributes - byte birth_day = 1; // not in user attributes - byte sex = 2; // other - switch (activityUser.getGender()) { - case ActivityUser.GENDER_MALE: - sex = 0; - break; - case ActivityUser.GENDER_FEMALE: - sex = 1; - } - int height = activityUser.getHeightCm(); - int weight = activityUser.getWeightKg(); - int userid = alias.hashCode(); // hash from alias like mi1 + ActivityUser activityUser = new ActivityUser(); + int height = activityUser.getHeightCm(); + int weight = activityUser.getWeightKg(); + int birth_year = activityUser.getYearOfBirth(); + byte birth_month = 7; // not in user attributes + byte birth_day = 1; // not in user attributes - // FIXME: Do encoding like in PebbleProtocol, this is ugly - byte bytes[] = new byte[]{ - MiBand2Service.COMMAND_SET_USERINFO, - 0, - 0, - (byte) (birth_year & 0xff), - (byte) ((birth_year >> 8) & 0xff), - birth_month, - birth_day, - sex, - (byte) (height & 0xff), - (byte) ((height >> 8) & 0xff), - (byte) ((weight * 200) & 0xff), - (byte) (((weight * 200) >> 8) & 0xff), - (byte) (userid & 0xff), - (byte) ((userid >> 8) & 0xff), - (byte) ((userid >> 16) & 0xff), - (byte) ((userid >> 24) & 0xff) - }; - - transaction.write(characteristic, bytes); - } else { - LOG.warn("Unable to set user info"); + if (alias == null || weight == 0 || height == 0 || birth_year == 0) { + LOG.warn("Unable to set user info, make sure it is set up"); + return this; } + + byte sex = 2; // other + switch (activityUser.getGender()) { + case ActivityUser.GENDER_MALE: + sex = 0; + break; + case ActivityUser.GENDER_FEMALE: + sex = 1; + } + int userid = alias.hashCode(); // hash from alias like mi1 + + // FIXME: Do encoding like in PebbleProtocol, this is ugly + byte bytes[] = new byte[]{ + MiBand2Service.COMMAND_SET_USERINFO, + 0, + 0, + (byte) (birth_year & 0xff), + (byte) ((birth_year >> 8) & 0xff), + birth_month, + birth_day, + sex, + (byte) (height & 0xff), + (byte) ((height >> 8) & 0xff), + (byte) ((weight * 200) & 0xff), + (byte) (((weight * 200) >> 8) & 0xff), + (byte) (userid & 0xff), + (byte) ((userid >> 8) & 0xff), + (byte) ((userid >> 16) & 0xff), + (byte) ((userid >> 24) & 0xff) + }; + + transaction.write(characteristic, bytes); return this; }