From 8138f385cb454db20e53878a35a22dadaf8a1159 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Mon, 26 Nov 2018 21:25:47 +0100 Subject: [PATCH] Handle user names with (only) strange characters --- .../core/bluetooth/BluetoothBeurerSanitas.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java index 8527c0db..bff22a10 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.Locale; import java.util.UUID; import timber.log.Timber; @@ -130,6 +131,14 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { return normalized.replaceAll("[^A-Za-z0-9]", ""); } + private String convertUserNameToScale(ScaleUser user) { + String normalized = normalizeString(user.getUserName()); + if (normalized.isEmpty()) { + return String.valueOf(user.getId()); + } + return normalized.toUpperCase(Locale.US); + } + public BluetoothBeurerSanitas(Context context, DeviceType deviceType) { super(context); @@ -361,7 +370,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { Calendar cal = Calendar.getInstance(); for (ScaleUser scaleUser : OpenScale.getInstance().getScaleUserList()) { - final String localName = normalizeString(scaleUser.getUserName()).toUpperCase(); + final String localName = convertUserNameToScale(scaleUser); cal.setTime(scaleUser.getBirthday()); final int year = cal.get(Calendar.YEAR); @@ -673,8 +682,8 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { Calendar cal = Calendar.getInstance(); cal.setTime(scaleUser.getBirthday()); - // We can only use up to 3 characters and have to handle them uppercase - byte[] nick = Arrays.copyOf(normalizeString(scaleUser.getUserName()).toUpperCase().getBytes(), 3); + // We can only use up to 3 characters (padding with 0 if needed) + byte[] nick = Arrays.copyOf(convertUserNameToScale(scaleUser).getBytes(), 3); byte year = (byte) (cal.get(Calendar.YEAR) - 1900); byte month = (byte) cal.get(Calendar.MONTH); byte day = (byte) cal.get(Calendar.DAY_OF_MONTH);