From 4b287a01671d8ed78205516c50a8d2808c3e857a Mon Sep 17 00:00:00 2001 From: Krisjans Blukis Date: Thu, 29 Jul 2021 14:21:30 +0300 Subject: [PATCH] BluetoothStandardWeightProfile: avoid hang if battery service is not implemented by connected device; --- .../BluetoothStandardWeightProfile.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 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 296570bf..d3c19a4d 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 @@ -61,6 +61,7 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication { protected boolean registerNewUser; ScaleUser selectedUser; ScaleMeasurement previousMeasurement; + protected boolean haveBatteryService; public BluetoothStandardWeightProfile(Context context) { super(context); @@ -68,6 +69,7 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication { this.selectedUser = OpenScale.getInstance().getSelectedScaleUser(); this.registerNewUser = false; previousMeasurement = null; + haveBatteryService = false; } @Override @@ -135,13 +137,20 @@ public class BluetoothStandardWeightProfile extends BluetoothCommunication { // Turn on notification for User Control Point setIndicationOn(BluetoothGattUuid.SERVICE_USER_DATA, BluetoothGattUuid.CHARACTERISTIC_USER_CONTROL_POINT); break; - case READ_BATTERY_LEVEL: - // read Battery Service - readBytes(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL); - break; case SET_NOTIFY_BATTERY_LEVEL: // Turn on notifications for Battery Service - setNotificationOn(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL); + if (setNotificationOn(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL)) { + haveBatteryService = true; + } + else { + haveBatteryService = false; + } + break; + case READ_BATTERY_LEVEL: + // read Battery Service + if (haveBatteryService) { + readBytes(BluetoothGattUuid.SERVICE_BATTERY_LEVEL, BluetoothGattUuid.CHARACTERISTIC_BATTERY_LEVEL); + } break; case SET_NOTIFY_VENDOR_SPECIFIC_USER_LIST: setNotifyVendorSpecificUserList();