1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 07:21:40 +02:00

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)

This commit is contained in:
jensMF
2022-04-15 19:30:31 +02:00
committed by GitHub
parent 11eca544a5
commit 15670013ef
2 changed files with 26 additions and 9 deletions

View File

@@ -17,8 +17,9 @@
/* /*
* Based on source-code by weliem/blessed-android * 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_UINT16;
import static com.welie.blessed.BluetoothBytesParser.FORMAT_UINT8; import static com.welie.blessed.BluetoothBytesParser.FORMAT_UINT8;
@@ -193,11 +194,16 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
case SET_SCALE_USER_DATA: case SET_SCALE_USER_DATA:
if (registerNewUser) { if (registerNewUser) {
writeUserDataToScale(); 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; break;
case REQUEST_MEASUREMENT: case REQUEST_MEASUREMENT:
if (registerNewUser) { if (registerNewUser) {
requestMeasurement(); requestMeasurement();
stopMachineState();
sendMessage(R.string.info_step_on_scale_for_reference, 0); sendMessage(R.string.info_step_on_scale_for_reference, 0);
} }
break; break;
@@ -246,9 +252,14 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
String modelNumber = parser.getStringValue(0); String modelNumber = parser.getStringValue(0);
Timber.d(String.format("Received modelnumber: %s", modelNumber)); 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); 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 { else {
Timber.d(String.format("Notification from unhandled characteristic: %s, value: [%s]", Timber.d(String.format("Notification from unhandled characteristic: %s, value: [%s]",
characteristic.toString(), byteInHex(value))); characteristic.toString(), byteInHex(value)));
@@ -351,6 +362,7 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
OpenScale.getInstance().updateScaleUser(selectedUser); OpenScale.getInstance().updateScaleUser(selectedUser);
} }
registerNewUser = false; registerNewUser = false;
resumeMachineState();
} }
} }
@@ -614,9 +626,11 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
protected void writeBirthday() { protected void writeBirthday() {
BluetoothBytesParser parser = new BluetoothBytesParser(); 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, 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) { protected Calendar dateToCalender(Date date) {
@@ -653,7 +667,9 @@ public abstract class BluetoothStandardWeightProfile extends BluetoothCommunicat
protected void setChangeIncrement() { protected void setChangeIncrement() {
BluetoothBytesParser parser = new BluetoothBytesParser(); 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, writeBytes(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_CHANGE_INCREMENT,
parser.getValue()); parser.getValue());
} }

View File

@@ -196,15 +196,16 @@ public class BluetoothSettingsFragment extends Fragment {
private void onDeviceFound(final ScanResult bleScanResult) { private void onDeviceFound(final ScanResult bleScanResult) {
BluetoothDevice device = bleScanResult.getDevice(); 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; return;
} }
BluetoothDeviceView deviceView = new BluetoothDeviceView(requireContext()); BluetoothDeviceView deviceView = new BluetoothDeviceView(context);
deviceView.setDeviceName(formatDeviceName(bleScanResult.getDevice())); deviceView.setDeviceName(formatDeviceName(bleScanResult.getDevice()));
BluetoothCommunication btDevice = BluetoothFactory.createDeviceDriver(requireContext(), device.getName()); BluetoothCommunication btDevice = BluetoothFactory.createDeviceDriver(context, device.getName());
if (btDevice != null) { if (btDevice != null) {
Timber.d("Found supported device %s (driver: %s)", Timber.d("Found supported device %s (driver: %s)",
formatDeviceName(device), btDevice.driverName()); formatDeviceName(device), btDevice.driverName());
@@ -216,7 +217,7 @@ public class BluetoothSettingsFragment extends Fragment {
Timber.d("Found unsupported device %s", Timber.d("Found unsupported device %s",
formatDeviceName(device)); formatDeviceName(device));
deviceView.setIcon(R.drawable.ic_bluetooth_device_not_supported); 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); deviceView.setEnabled(false);
if (OpenScale.DEBUG_MODE) { if (OpenScale.DEBUG_MODE) {