From 5644ae592557cbd20b2cc21ef566b96a70e76b06 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 5 Nov 2018 23:27:29 +0100 Subject: [PATCH] Display measured hr value in Debug activity + cleanup Fixes #1320 --- .../activities/DebugActivity.java | 15 +++++++++- .../activities/FindPhoneActivity.java | 1 - .../gadgetbridge/model/DeviceService.java | 1 - .../service/devices/huami/HuamiSupport.java | 28 +++++++++---------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index 475d229c2..5be60e6a2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -28,6 +28,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; +import android.os.Parcelable; import android.support.v4.app.NavUtils; import android.support.v4.app.NotificationCompat; import android.support.v4.app.RemoteInput; @@ -44,11 +45,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.Serializable; import java.util.ArrayList; import java.util.Objects; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; @@ -82,6 +85,9 @@ public class DebugActivity extends AbstractGBActivity { GB.toast(context, "got wearable reply: " + reply, Toast.LENGTH_SHORT, GB.INFO); break; } + case DeviceService.ACTION_REALTIME_SAMPLES: + handleRealtimeSample(intent.getSerializableExtra(DeviceService.EXTRA_REALTIME_SAMPLE)); + break; default: LOG.info("ignoring intent action " + intent.getAction()); break; @@ -89,6 +95,13 @@ public class DebugActivity extends AbstractGBActivity { } }; + private void handleRealtimeSample(Serializable extra) { + if (extra instanceof ActivitySample) { + ActivitySample sample = (ActivitySample) extra; + GB.toast(this, "Heart Rate measured: " + sample.getHeartRate(), Toast.LENGTH_LONG, GB.INFO); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -96,7 +109,7 @@ public class DebugActivity extends AbstractGBActivity { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_REPLY); - filter.addAction(DeviceService.ACTION_HEARTRATE_MEASUREMENT); + filter.addAction(DeviceService.ACTION_REALTIME_SAMPLES); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); registerReceiver(mReceiver, filter); // for ACTION_REPLY diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FindPhoneActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FindPhoneActivity.java index eb1fb842b..aa62c74a4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FindPhoneActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FindPhoneActivity.java @@ -71,7 +71,6 @@ public class FindPhoneActivity extends AbstractGBActivity { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_FOUND); - filter.addAction(DeviceService.ACTION_HEARTRATE_MEASUREMENT); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); registerReceiver(mReceiver, filter); // for ACTION_FOUND diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index 974a1cbf2..0ea38d0e3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -59,7 +59,6 @@ public interface DeviceService extends EventHandler { String ACTION_ENABLE_REALTIME_HEARTRATE_MEASUREMENT = PREFIX + ".action.realtime_hr_measurement"; String ACTION_ENABLE_HEARTRATE_SLEEP_SUPPORT = PREFIX + ".action.enable_heartrate_sleep_support"; String ACTION_SET_HEARTRATE_MEASUREMENT_INTERVAL = PREFIX + ".action.set_heartrate_measurement_intervarl"; - String ACTION_HEARTRATE_MEASUREMENT = PREFIX + ".action.hr_measurement"; String ACTION_ADD_CALENDAREVENT = PREFIX + ".action.add_calendarevent"; String ACTION_DELETE_CALENDAREVENT = PREFIX + ".action.delete_calendarevent"; String ACTION_SEND_CONFIGURATION = PREFIX + ".action.send_configuration"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 3db363224..4783b527a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -20,10 +20,8 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huami; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.net.Uri; import android.support.v4.content.LocalBroadcastManager; import android.text.format.DateFormat; @@ -88,7 +86,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; -import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; @@ -105,11 +102,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateA import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.deviceinfo.DeviceInfoProfile; -import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.heartrate.HeartRateProfile; import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification; +import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.actions.StopNotificationAction; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband2.Mi2NotificationStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.miband2.Mi2TextNotificationStrategy; -import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.actions.StopNotificationAction; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.FetchActivityOperation; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.FetchSportsSummaryOperation; import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.InitOperation; @@ -151,7 +147,6 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(HuamiSupport.class); private final DeviceInfoProfile deviceInfoProfile; - private final HeartRateProfile heartRateProfile; private final IntentListener mListener = new IntentListener() { @Override public void notify(Intent intent) { @@ -201,8 +196,6 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { deviceInfoProfile = new DeviceInfoProfile<>(this); deviceInfoProfile.addListener(mListener); addSupportedProfile(deviceInfoProfile); - heartRateProfile = new HeartRateProfile<>(this); - addSupportedProfile(heartRateProfile); } @Override @@ -858,6 +851,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } try { TransactionBuilder builder = performInitialized("HeartRateTest"); + enableNotifyHeartRateMeasurements(true, builder); builder.write(characteristicHRControlPoint, stopHeartMeasurementContinuous); builder.write(characteristicHRControlPoint, stopHeartMeasurementManual); builder.write(characteristicHRControlPoint, startHeartMeasurementManual); @@ -874,13 +868,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } try { TransactionBuilder builder = performInitialized("Enable realtime heart rate measurement"); - if (heartRateNotifyEnabled != enable) { - BluetoothGattCharacteristic heartrateCharacteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT); - if (heartrateCharacteristic != null) { - builder.notify(heartrateCharacteristic, enable); - heartRateNotifyEnabled = enable; - } - } + enableNotifyHeartRateMeasurements(enable, builder); if (enable) { builder.write(characteristicHRControlPoint, stopHeartMeasurementManual); builder.write(characteristicHRControlPoint, startHeartMeasurementContinuous); @@ -894,6 +882,16 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport { } } + private void enableNotifyHeartRateMeasurements(boolean enable, TransactionBuilder builder) { + if (heartRateNotifyEnabled != enable) { + BluetoothGattCharacteristic heartrateCharacteristic = getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT); + if (heartrateCharacteristic != null) { + builder.notify(heartrateCharacteristic, enable); + heartRateNotifyEnabled = enable; + } + } + } + @Override public void onFindDevice(boolean start) { isLocatingDevice = start;