From d407f95c53cea0dc23f972209b85b6ccadf72934 Mon Sep 17 00:00:00 2001 From: Ramin Shams Date: Fri, 28 Jul 2023 15:05:29 +1000 Subject: [PATCH] feat: exposed bmr readings on android --- packages/health/README.md | 2 +- .../cachet/plugins/health/HealthPlugin.kt | 28 +++++++++++++++++++ packages/health/lib/src/data_types.dart | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/health/README.md b/packages/health/README.md index 9dd3e54f0..69de126b6 100644 --- a/packages/health/README.md +++ b/packages/health/README.md @@ -24,7 +24,7 @@ Note that for Android, the target phone **needs** to have [Google Fit](https://w | **Data Type** | **Unit** | **iOS** | **Android (Google Fit)** | **Android (Health Connect)** | **Comments** | | --------------------------- | ----------------------- | ------- | ------------------------ | ---------------------------- | -------------------------------------- | | ACTIVE_ENERGY_BURNED | CALORIES | yes | yes | yes | | -| BASAL_ENERGY_BURNED | CALORIES | yes | | | | +| BASAL_ENERGY_BURNED | CALORIES | yes | yes | | | | BLOOD_GLUCOSE | MILLIGRAM_PER_DECILITER | yes | yes | yes | | | BLOOD_OXYGEN | PERCENTAGE | yes | yes | yes | | | BLOOD_PRESSURE_DIASTOLIC | MILLIMETER_OF_MERCURY | yes | yes | yes | | diff --git a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt index f72ccee8d..4087c6d66 100644 --- a/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt +++ b/packages/health/android/src/main/kotlin/cachet/plugins/health/HealthPlugin.kt @@ -78,6 +78,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) : private var STEPS = "STEPS" private var AGGREGATE_STEP_COUNT = "AGGREGATE_STEP_COUNT" private var ACTIVE_ENERGY_BURNED = "ACTIVE_ENERGY_BURNED" + private var BASAL_ENERGY_BURNED = "BASAL_ENERGY_BURNED" private var HEART_RATE = "HEART_RATE" private var BODY_TEMPERATURE = "BODY_TEMPERATURE" private var BLOOD_PRESSURE_SYSTOLIC = "BLOOD_PRESSURE_SYSTOLIC" @@ -418,6 +419,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) : STEPS -> DataType.TYPE_STEP_COUNT_DELTA AGGREGATE_STEP_COUNT -> DataType.AGGREGATE_STEP_COUNT_DELTA ACTIVE_ENERGY_BURNED -> DataType.TYPE_CALORIES_EXPENDED + BASAL_ENERGY_BURNED -> DataType.TYPE_BASAL_METABOLIC_RATE HEART_RATE -> DataType.TYPE_HEART_RATE_BPM BODY_TEMPERATURE -> HealthDataTypes.TYPE_BODY_TEMPERATURE BLOOD_PRESSURE_SYSTOLIC -> HealthDataTypes.TYPE_BLOOD_PRESSURE @@ -442,6 +444,7 @@ class HealthPlugin(private var channel: MethodChannel? = null) : WEIGHT -> Field.FIELD_WEIGHT STEPS -> Field.FIELD_STEPS ACTIVE_ENERGY_BURNED -> Field.FIELD_CALORIES + BASAL_ENERGY_BURNED -> Field.FIELD_CALORIES HEART_RATE -> Field.FIELD_BPM BODY_TEMPERATURE -> HealthFields.FIELD_BODY_TEMPERATURE BLOOD_PRESSURE_SYSTOLIC -> HealthFields.FIELD_BLOOD_PRESSURE_SYSTOLIC @@ -944,6 +947,31 @@ class HealthPlugin(private var channel: MethodChannel? = null) : ), ) } + /* + BMR is not available to be retrieved for every day (although it is reflected in Google Fit energy calcs for every single day) + Thus, a specific case is defined below for handling BMR, with "1" provided as startDate to the setTimeRange method. + A limit of "1" is also set through setLimit, to only get the latest value. + Note: + According to Google's documentation, since "the recorded BMR is instantaneous, the start time should not be set.". + However, startTime is oddly enough a required property. + */ + DataType.TYPE_BASAL_METABOLIC_RATE -> { + Fitness.getHistoryClient(activity!!.applicationContext, googleSignInAccount) + .readData( + DataReadRequest.Builder() + .read(dataType) + .setTimeRange(1, endTime, TimeUnit.MILLISECONDS) + .setLimit(1) + .build() + ) + .addOnSuccessListener(threadPoolExecutor!!, dataHandler(dataType, field, result)) + .addOnFailureListener( + errHandler( + result, + "There was an error getting BMR data!" + ), + ) + } else -> { Fitness.getHistoryClient(context!!.applicationContext, googleSignInAccount) .readData( diff --git a/packages/health/lib/src/data_types.dart b/packages/health/lib/src/data_types.dart index 22efdfad5..25f05ecbb 100644 --- a/packages/health/lib/src/data_types.dart +++ b/packages/health/lib/src/data_types.dart @@ -113,6 +113,7 @@ const List _dataTypeKeysIOS = [ /// List of data types available on Android const List _dataTypeKeysAndroid = [ HealthDataType.ACTIVE_ENERGY_BURNED, + HealthDataType.BASAL_ENERGY_BURNED, HealthDataType.BLOOD_GLUCOSE, HealthDataType.BLOOD_OXYGEN, HealthDataType.BLOOD_PRESSURE_DIASTOLIC,