2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2016-02-02 17:33:24 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
2015-10-18 20:54:22 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.DeviceInfo;
|
2015-08-17 22:43:42 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
|
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
public class UserInfo {
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final String btAddress;
|
|
|
|
private final String alias;
|
|
|
|
private final int gender;
|
|
|
|
private final int age;
|
|
|
|
private final int height;
|
|
|
|
private final int weight;
|
|
|
|
private final int type;
|
2015-04-19 02:37:29 +02:00
|
|
|
|
|
|
|
private byte[] data = new byte[20];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a default user info.
|
2015-04-19 11:28:03 +02:00
|
|
|
*
|
2015-04-19 02:37:29 +02:00
|
|
|
* @param btAddress the address of the MI Band to connect to.
|
|
|
|
*/
|
|
|
|
public static UserInfo getDefault(String btAddress) {
|
2016-02-02 17:33:24 +01:00
|
|
|
return new UserInfo(btAddress, "1550050550", ActivityUser.defaultUserGender, ActivityUser.defaultUserAge, ActivityUser.defaultUserHeightCm, ActivityUser.defaultUserWeightKg, 0);
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user info with the given data
|
2015-04-19 11:28:03 +02:00
|
|
|
*
|
2015-04-19 02:37:29 +02:00
|
|
|
* @param address the address of the MI Band to connect to.
|
2015-05-07 23:51:03 +02:00
|
|
|
* @throws IllegalArgumentException when the given values are not valid
|
2015-04-19 02:37:29 +02:00
|
|
|
*/
|
2015-05-07 23:51:03 +02:00
|
|
|
public static UserInfo create(String address, String alias, int gender, int age, int height, int weight, int type) throws IllegalArgumentException {
|
2015-05-09 23:13:47 +02:00
|
|
|
if (address == null || address.length() == 0 || alias == null || alias.length() == 0 || gender < 0 || age <= 0 || weight <= 0 || type < 0) {
|
2015-05-07 23:51:03 +02:00
|
|
|
throw new IllegalArgumentException("Invalid parameters");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return new UserInfo(address, alias, gender, age, height, weight, type);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new IllegalArgumentException("Illegal user info data", ex);
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-05-07 23:51:03 +02:00
|
|
|
/**
|
|
|
|
* Creates a user info with the given data
|
|
|
|
*
|
|
|
|
* @param address the address of the MI Band to connect to.
|
|
|
|
*/
|
|
|
|
private UserInfo(String address, String alias, int gender, int age, int height, int weight, int type) {
|
2015-04-19 02:37:29 +02:00
|
|
|
this.btAddress = address;
|
|
|
|
this.alias = alias;
|
|
|
|
this.gender = gender;
|
|
|
|
this.age = age;
|
|
|
|
this.height = height;
|
|
|
|
this.weight = weight;
|
|
|
|
this.type = type;
|
2015-10-18 20:54:22 +02:00
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
private int calculateUidFrom(String alias) {
|
2015-12-08 21:48:25 +01:00
|
|
|
int uid;
|
2015-10-18 20:54:22 +02:00
|
|
|
try {
|
|
|
|
uid = Integer.parseInt(alias);
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
uid = alias.hashCode(); // simple as that
|
|
|
|
}
|
|
|
|
return uid;
|
|
|
|
}
|
2015-04-19 02:37:29 +02:00
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
public byte[] getData(DeviceInfo mDeviceInfo) {
|
|
|
|
byte[] sequence = new byte[20];
|
2015-05-07 01:30:40 +02:00
|
|
|
int uid = calculateUidFrom(alias);
|
2015-10-18 20:54:22 +02:00
|
|
|
|
2015-04-19 02:37:29 +02:00
|
|
|
sequence[0] = (byte) uid;
|
|
|
|
sequence[1] = (byte) (uid >>> 8);
|
|
|
|
sequence[2] = (byte) (uid >>> 16);
|
|
|
|
sequence[3] = (byte) (uid >>> 24);
|
|
|
|
|
|
|
|
sequence[4] = (byte) (gender & 0xff);
|
|
|
|
sequence[5] = (byte) (age & 0xff);
|
|
|
|
sequence[6] = (byte) (height & 0xff);
|
|
|
|
sequence[7] = (byte) (weight & 0xff);
|
|
|
|
sequence[8] = (byte) (type & 0xff);
|
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
int aliasFrom = 9;
|
2015-12-08 21:48:25 +01:00
|
|
|
if (mDeviceInfo.isMili1A() || mDeviceInfo.isMilli1S()) {
|
2015-10-18 20:54:22 +02:00
|
|
|
sequence[9] = (byte) (mDeviceInfo.feature & 255);
|
|
|
|
sequence[10] = (byte) (mDeviceInfo.appearance & 255);
|
|
|
|
aliasFrom = 11;
|
2015-05-07 01:30:40 +02:00
|
|
|
}
|
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
byte[] aliasBytes = alias.substring(0, Math.min(alias.length(), 19-aliasFrom)).getBytes();
|
|
|
|
System.arraycopy(aliasBytes, 0, sequence, aliasFrom, aliasBytes.length);
|
|
|
|
|
|
|
|
byte[] crcSequence = Arrays.copyOf(sequence, 19);
|
|
|
|
sequence[19] = (byte) ((CheckSums.getCRC8(crcSequence) ^ Integer.parseInt(this.btAddress.substring(this.btAddress.length() - 2), 16)) & 0xff);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2015-10-18 20:54:22 +02:00
|
|
|
return sequence;
|
2015-04-19 02:37:29 +02:00
|
|
|
}
|
|
|
|
}
|