Mi Band 2/ Bip: Do not send user info if not set up

This commit is contained in:
Andreas Shimokawa 2017-09-24 23:03:11 +02:00
parent 717eb6ba58
commit e28085e6af
1 changed files with 47 additions and 41 deletions

View File

@ -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;
}