1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-23 00:33:09 +02:00

BluetoothStandardWeightProfile: reuse common method to decode vendor specific scale user data;

This commit is contained in:
Krisjans Blukis
2021-07-29 15:27:09 +03:00
parent d68fcf8d66
commit 54d478543c
3 changed files with 95 additions and 190 deletions

View File

@@ -20,18 +20,12 @@
package com.health.openscale.core.bluetooth;
import android.content.Context;
import android.util.Pair;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import com.welie.blessed.BluetoothBytesParser;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.Vector;
import timber.log.Timber;
@@ -49,15 +43,10 @@ class BluetoothGattUuidBF600 extends BluetoothGattUuid {
public class BluetoothBeurerBF600 extends BluetoothStandardWeightProfile {
ScaleMeasurement scaleMeasurement;
private Vector<ScaleUser> scaleUserList;
static final int BF600_MAX_USERS = 8;
String deviceName;
public BluetoothBeurerBF600(Context context, String name) {
super(context);
scaleMeasurement = new ScaleMeasurement();
scaleUserList = new Vector<ScaleUser>();
deviceName = name;
}
@@ -101,7 +90,6 @@ public class BluetoothBeurerBF600 extends BluetoothStandardWeightProfile {
@Override
protected synchronized void requestVendorSpecificUserList() {
scaleUserList.clear();
BluetoothBytesParser parser = new BluetoothBytesParser();
parser.setIntValue(0x00, FORMAT_UINT8);
writeBytes(BluetoothGattUuidBF600.SERVICE_BEURER_CUSTOM_BF600, BluetoothGattUuidBF600.CHARACTERISTIC_BEURER_BF600_USER_LIST,
@@ -112,91 +100,10 @@ public class BluetoothBeurerBF600 extends BluetoothStandardWeightProfile {
@Override
public void onBluetoothNotify(UUID characteristic, byte[] value) {
if (characteristic.equals(BluetoothGattUuidBF600.CHARACTERISTIC_BEURER_BF600_USER_LIST)) {
Timber.d(String.format("Got user data: <%s>", byteInHex(value)));
BluetoothBytesParser parser = new BluetoothBytesParser(value);
int userListStatus = parser.getIntValue(FORMAT_UINT8);
if (userListStatus == 2) {
Timber.d("scale have no users!");
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
else if (userListStatus == 1) {
for (int i = 0; i < scaleUserList.size(); i++) {
if (i == 0) {
Timber.d("scale user list:");
}
Timber.d("\n" + (i + 1) + ". " + scaleUserList.get(i));
}
if ((scaleUserList.size() == 0)) {
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
if (getUserScaleIndex(selectedUser.getId()) == -1 || getUserScaleConsent(selectedUser.getId()) == -1) {
chooseExistingScaleUser(scaleUserList);
return;
}
resumeMachineState();
return;
}
int index = parser.getIntValue(FORMAT_UINT8);
String initials = parser.getStringValue();
int end = 3 > initials.length() ? initials.length() : 3;
initials = initials.substring(0, end);
if (initials.length() == 3) {
if (initials.charAt(0) == 0xff && initials.charAt(1) == 0xff && initials.charAt(2) == 0xff) {
initials = "";
}
}
parser.setOffset(5);
int year = parser.getIntValue(FORMAT_UINT16);
int month = parser.getIntValue(FORMAT_UINT8);
int day = parser.getIntValue(FORMAT_UINT8);
int height = parser.getIntValue(FORMAT_UINT8);
int gender = parser.getIntValue(FORMAT_UINT8);
int activityLevel = parser.getIntValue(FORMAT_UINT8);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
ScaleUser scaleUser = new ScaleUser(initials, calendar.getTime(), height, gender, activityLevel - 1);
scaleUser.setId(index);
scaleUserList.add(scaleUser);
if (scaleUserList.size() == BF600_MAX_USERS) {
chooseExistingScaleUser(scaleUserList);
}
handleVendorSpecificUserList(value);
}
else {
super.onBluetoothNotify(characteristic, value);
}
}
protected void chooseExistingScaleUser(Vector<ScaleUser> userList) {
final DateFormat dateFormat = DateFormat.getDateInstance();
int choicesCount = userList.size();
if (userList.size() < BF600_MAX_USERS) {
choicesCount = userList.size() + 1;
}
CharSequence[] choiceStrings = new String[choicesCount];
int indexArray[] = new int[choicesCount];
int selectedItem = -1;
for (int i = 0; i < userList.size(); ++i) {
ScaleUser u = userList.get(i);
String name = u.getUserName();
choiceStrings[i] = (name.length() > 0 ? name : String.format("P%02d", u.getId()))
+ " " + (u.getGender().isMale() ? "male" : "female")
+ " " + "height:" + u.getBodyHeight()
+ " birthday:" + dateFormat.format(u.getBirthday())
+ " " + "AL:" + (u.getActivityLevel().toInt() + 1);
indexArray[i] = u.getId();
}
if (userList.size() < BF600_MAX_USERS) {
choiceStrings[userList.size()] = "Create new user on scale.";
indexArray[userList.size()] = -1;
}
Pair<CharSequence[], int[]> choices = new Pair(choiceStrings, indexArray);
chooseScaleUserUi(choices);
}
}

View File

@@ -23,6 +23,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Pair;
import com.health.openscale.R;
import com.health.openscale.core.OpenScale;
@@ -30,10 +31,13 @@ import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.welie.blessed.BluetoothBytesParser;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.Random;
import java.util.Vector;
import timber.log.Timber;
@@ -62,6 +66,8 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
ScaleUser selectedUser;
ScaleMeasurement previousMeasurement;
protected boolean haveBatteryService;
protected Vector<ScaleUser> scaleUserList;
static final int VENDOR_SPECIFIC_MAX_USERS = 8;
public BluetoothStandardWeightProfile(Context context) {
super(context);
@@ -70,6 +76,7 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
this.registerNewUser = false;
previousMeasurement = null;
haveBatteryService = false;
scaleUserList = new Vector<ScaleUser>();
}
@Override
@@ -156,6 +163,7 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
setNotifyVendorSpecificUserList();
break;
case REQUEST_VENDOR_SPECIFIC_USER_LIST:
scaleUserList.clear();
requestVendorSpecificUserList();
break;
case REGISTER_NEW_SCALE_USER:
@@ -717,4 +725,89 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication {
reconnectOrSetSmState(SM_STEPS.SELECT_SCALE_USER, SM_STEPS.REQUEST_VENDOR_SPECIFIC_USER_LIST, uiHandler);
}
}
protected void handleVendorSpecificUserList(byte[] value) {
Timber.d(String.format("Got user data: <%s>", byteInHex(value)));
BluetoothBytesParser parser = new BluetoothBytesParser(value);
int userListStatus = parser.getIntValue(FORMAT_UINT8);
if (userListStatus == 2) {
Timber.d("scale have no users!");
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
else if (userListStatus == 1) {
for (int i = 0; i < scaleUserList.size(); i++) {
if (i == 0) {
Timber.d("scale user list:");
}
Timber.d("\n" + (i + 1) + ". " + scaleUserList.get(i));
}
if ((scaleUserList.size() == 0)) {
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
if (getUserScaleIndex(selectedUser.getId()) == -1 || getUserScaleConsent(selectedUser.getId()) == -1) {
chooseExistingScaleUser(scaleUserList);
return;
}
resumeMachineState();
return;
}
int index = parser.getIntValue(FORMAT_UINT8);
String initials = parser.getStringValue();
int end = 3 > initials.length() ? initials.length() : 3;
initials = initials.substring(0, end);
if (initials.length() == 3) {
if (initials.charAt(0) == 0xff && initials.charAt(1) == 0xff && initials.charAt(2) == 0xff) {
initials = "";
}
}
parser.setOffset(5);
int year = parser.getIntValue(FORMAT_UINT16);
int month = parser.getIntValue(FORMAT_UINT8);
int day = parser.getIntValue(FORMAT_UINT8);
int height = parser.getIntValue(FORMAT_UINT8);
int gender = parser.getIntValue(FORMAT_UINT8);
int activityLevel = parser.getIntValue(FORMAT_UINT8);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
ScaleUser scaleUser = new ScaleUser(initials, calendar.getTime(), height, gender, activityLevel - 1);
scaleUser.setId(index);
scaleUserList.add(scaleUser);
if (scaleUserList.size() == VENDOR_SPECIFIC_MAX_USERS) {
chooseExistingScaleUser(scaleUserList);
}
}
protected void chooseExistingScaleUser(Vector<ScaleUser> userList) {
final DateFormat dateFormat = DateFormat.getDateInstance();
int choicesCount = userList.size();
if (userList.size() < VENDOR_SPECIFIC_MAX_USERS) {
choicesCount = userList.size() + 1;
}
CharSequence[] choiceStrings = new String[choicesCount];
int indexArray[] = new int[choicesCount];
int selectedItem = -1;
for (int i = 0; i < userList.size(); ++i) {
ScaleUser u = userList.get(i);
String name = u.getUserName();
choiceStrings[i] = (name.length() > 0 ? name : String.format("P%02d", u.getId()))
+ " " + (u.getGender().isMale() ? "male" : "female")
+ " " + "height:" + u.getBodyHeight()
+ " birthday:" + dateFormat.format(u.getBirthday())
+ " " + "AL:" + (u.getActivityLevel().toInt() + 1);
indexArray[i] = u.getId();
}
if (userList.size() < VENDOR_SPECIFIC_MAX_USERS) {
choiceStrings[userList.size()] = "Create new user on scale.";
indexArray[userList.size()] = -1;
}
Pair<CharSequence[], int[]> choices = new Pair(choiceStrings, indexArray);
chooseScaleUserUi(choices);
}
}

View File

@@ -20,18 +20,10 @@
package com.health.openscale.core.bluetooth;
import android.content.Context;
import android.util.Pair;
import com.health.openscale.core.datatypes.ScaleMeasurement;
import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.core.utils.Converters;
import com.welie.blessed.BluetoothBytesParser;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.Vector;
import timber.log.Timber;
@@ -45,15 +37,10 @@ class BluetoothGattUuidSBF77 extends BluetoothGattUuid {
public class BluetoothSwpSBF77 extends BluetoothStandardWeightProfile {
ScaleMeasurement scaleMeasurement;
private Vector<ScaleUser> scaleUserList;
static final int SBF77_MAX_USERS = 8;
String deviceName;
public BluetoothSwpSBF77(Context context, String name) {
super(context);
scaleMeasurement = new ScaleMeasurement();
scaleUserList = new Vector<ScaleUser>();
deviceName = name;
}
@@ -78,7 +65,6 @@ public class BluetoothSwpSBF77 extends BluetoothStandardWeightProfile {
@Override
protected synchronized void requestVendorSpecificUserList() {
scaleUserList.clear();
BluetoothBytesParser parser = new BluetoothBytesParser();
parser.setIntValue(0x00, FORMAT_UINT8);
writeBytes(BluetoothGattUuidSBF77.SERVICE_CUSTOM_SBF77, BluetoothGattUuidSBF77.CHARACTERISTIC_SBF77_USER_LIST,
@@ -89,91 +75,10 @@ public class BluetoothSwpSBF77 extends BluetoothStandardWeightProfile {
@Override
public void onBluetoothNotify(UUID characteristic, byte[] value) {
if (characteristic.equals(BluetoothGattUuidSBF77.CHARACTERISTIC_SBF77_USER_LIST)) {
Timber.d(String.format("Got user data: <%s>", byteInHex(value)));
BluetoothBytesParser parser = new BluetoothBytesParser(value);
int userListStatus = parser.getIntValue(FORMAT_UINT8);
if (userListStatus == 2) {
Timber.d("scale have no users!");
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
else if (userListStatus == 1) {
for (int i = 0; i < scaleUserList.size(); i++) {
if (i == 0) {
Timber.d("scale user list:");
}
Timber.d("\n" + (i + 1) + ". " + scaleUserList.get(i));
}
if ((scaleUserList.size() == 0)) {
storeUserScaleConsentCode(selectedUser.getId(), -1);
storeUserScaleIndex(selectedUser.getId(), -1);
jumpNextToStepNr(SM_STEPS.REGISTER_NEW_SCALE_USER.ordinal());
resumeMachineState();
return;
}
if (getUserScaleIndex(selectedUser.getId()) == -1 || getUserScaleConsent(selectedUser.getId()) == -1) {
chooseExistingScaleUser(scaleUserList);
return;
}
resumeMachineState();
return;
}
int index = parser.getIntValue(FORMAT_UINT8);
String initials = parser.getStringValue();
int end = 3 > initials.length() ? initials.length() : 3;
initials = initials.substring(0, end);
if (initials.length() == 3) {
if (initials.charAt(0) == 0xff && initials.charAt(1) == 0xff && initials.charAt(2) == 0xff) {
initials = "";
}
}
parser.setOffset(5);
int year = parser.getIntValue(FORMAT_UINT16);
int month = parser.getIntValue(FORMAT_UINT8);
int day = parser.getIntValue(FORMAT_UINT8);
int height = parser.getIntValue(FORMAT_UINT8);
int gender = parser.getIntValue(FORMAT_UINT8);
int activityLevel = parser.getIntValue(FORMAT_UINT8);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
ScaleUser scaleUser = new ScaleUser(initials, calendar.getTime(), height, gender, activityLevel - 1);
scaleUser.setId(index);
scaleUserList.add(scaleUser);
if (scaleUserList.size() == SBF77_MAX_USERS) {
chooseExistingScaleUser(scaleUserList);
}
handleVendorSpecificUserList(value);
}
else {
super.onBluetoothNotify(characteristic, value);
}
}
protected void chooseExistingScaleUser(Vector<ScaleUser> userList) {
final DateFormat dateFormat = DateFormat.getDateInstance();
int choicesCount = userList.size();
if (userList.size() < SBF77_MAX_USERS) {
choicesCount = userList.size() + 1;
}
CharSequence[] choiceStrings = new String[choicesCount];
int indexArray[] = new int[choicesCount];
int selectedItem = -1;
for (int i = 0; i < userList.size(); ++i) {
ScaleUser u = userList.get(i);
String name = u.getUserName();
choiceStrings[i] = (name.length() > 0 ? name : String.format("P%02d", u.getId()))
+ " " + (u.getGender().isMale() ? "male" : "female")
+ " " + "height:" + u.getBodyHeight()
+ " birthday:" + dateFormat.format(u.getBirthday())
+ " " + "AL:" + (u.getActivityLevel().toInt() + 1);
indexArray[i] = u.getId();
}
if (userList.size() < SBF77_MAX_USERS) {
choiceStrings[userList.size()] = "Create new user on scale.";
indexArray[userList.size()] = -1;
}
Pair<CharSequence[], int[]> choices = new Pair(choiceStrings, indexArray);
chooseScaleUserUi(choices);
}
}