From 15670013efba785cbe03ce23d1f6c2592269e3b6 Mon Sep 17 00:00:00 2001 From: jensMF Date: Fri, 15 Apr 2022 19:30:31 +0200 Subject: [PATCH] fixed new user registration for BF105 (and may be other scales) and fixed crashes because of Fragment BluetoothSettingsFragment not attached to a context. error(s) (#806) --- .../BluetoothStandardWeightProfile.java | 26 +++++++++++++++---- .../BluetoothSettingsFragment.java | 9 ++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothStandardWeightProfile.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothStandardWeightProfile.java index d87efe0e..6c99424a 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothStandardWeightProfile.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothStandardWeightProfile.java @@ -17,8 +17,9 @@ /* * Based on source-code by weliem/blessed-android */ -package com.health.openscale.core.bluetooth; + package com.health.openscale.core.bluetooth; +import static com.welie.blessed.BluetoothBytesParser.FORMAT_UINT32; import static com.welie.blessed.BluetoothBytesParser.FORMAT_UINT16; import static com.welie.blessed.BluetoothBytesParser.FORMAT_UINT8; @@ -193,11 +194,16 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat case SET_SCALE_USER_DATA: if (registerNewUser) { writeUserDataToScale(); + // stopping machine state to have all user data written, before the reference measurment starts, otherwise the scale might not store the user + stopMachineState(); + // reading CHARACTERISTIC_CHANGE_INCREMENT to resume machine state + readBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_CHANGE_INCREMENT); } break; case REQUEST_MEASUREMENT: if (registerNewUser) { requestMeasurement(); + stopMachineState(); sendMessage(R.string.info_step_on_scale_for_reference, 0); } break; @@ -246,9 +252,14 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat String modelNumber = parser.getStringValue(0); Timber.d(String.format("Received modelnumber: %s", modelNumber)); } - else if(characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT)) { + else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT)) { handleUserControlPointNotify(value); } + else if (characteristic.equals(BluetoothGattUuid.CHARACTERISTIC_CHANGE_INCREMENT)) { + int increment = parser.getIntValue(FORMAT_UINT32); + Timber.d(String.format("Notification from CHARACTERISTIC_CHANGE_INCREMENT, value: %s", increment)); + resumeMachineState(); + } else { Timber.d(String.format("Notification from unhandled characteristic: %s, value: [%s]", characteristic.toString(), byteInHex(value))); @@ -351,6 +362,7 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat OpenScale.getInstance().updateScaleUser(selectedUser); } registerNewUser = false; + resumeMachineState(); } } @@ -614,9 +626,11 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat protected void writeBirthday() { BluetoothBytesParser parser = new BluetoothBytesParser(); - parser.setDateTime(dateToCalender(this.selectedUser.getBirthday())); + Calendar userBirthday = dateToCalender(this.selectedUser.getBirthday()); + Timber.d(String.format("user Birthday: %tD", userBirthday)); + parser.setDateTime(userBirthday); writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_DATE_OF_BIRTH, - Arrays.copyOfRange(parser.getValue(), 0, 3)); + Arrays.copyOfRange(parser.getValue(), 0, 4)); } protected Calendar dateToCalender(Date date) { @@ -653,7 +667,9 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat protected void setChangeIncrement() { BluetoothBytesParser parser = new BluetoothBytesParser(); - parser.setIntValue(1, FORMAT_UINT8); + int i = 1; + Timber.d(String.format("Setting Change increment to %s", i)); + parser.setIntValue(i, FORMAT_UINT32); writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_CHANGE_INCREMENT, parser.getValue()); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java index ba8cf453..2af1ec6c 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java @@ -196,15 +196,16 @@ public class BluetoothSettingsFragment extends Fragment { private void onDeviceFound(final ScanResult bleScanResult) { BluetoothDevice device = bleScanResult.getDevice(); + Context context = getContext(); - if (device.getName() == null || foundDevices.containsKey(device.getAddress())) { + if (device.getName() == null || foundDevices.containsKey(device.getAddress()) || context == null) { return; } - BluetoothDeviceView deviceView = new BluetoothDeviceView(requireContext()); + BluetoothDeviceView deviceView = new BluetoothDeviceView(context); deviceView.setDeviceName(formatDeviceName(bleScanResult.getDevice())); - BluetoothCommunication btDevice = BluetoothFactory.createDeviceDriver(requireContext(), device.getName()); + BluetoothCommunication btDevice = BluetoothFactory.createDeviceDriver(context, device.getName()); if (btDevice != null) { Timber.d("Found supported device %s (driver: %s)", formatDeviceName(device), btDevice.driverName()); @@ -216,7 +217,7 @@ public class BluetoothSettingsFragment extends Fragment { Timber.d("Found unsupported device %s", formatDeviceName(device)); deviceView.setIcon(R.drawable.ic_bluetooth_device_not_supported); - deviceView.setSummaryText(requireContext().getString(R.string.label_bt_device_no_support)); + deviceView.setSummaryText(context.getString(R.string.label_bt_device_no_support)); deviceView.setEnabled(false); if (OpenScale.DEBUG_MODE) {