The iOS Google Fit app shows Resting Heart Rate as one of it's metrics (I have not seen it in the Android Google Fit app, however). I would like to show Resting Heart Rate in an Android app using Google Fit Data. However, the Android Google Fit API does not seem to present this data.
I have tried a read request both by DataType.TYPE_HEART_RATE_BPM
and by aggregating DataType.TYPE_HEART_RATE_BPM
and DataType.AGGREGATE_HEART_RATE_SUMMARY
as shown here:
DataReadRequest.Builder()
.aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.enableServerQueries()
.setTimeRange(startDate.time, endDate.time, TimeUnit.MILLISECONDS)
.build()
The summary gives lowest, highest, and average for a particular time range, but not resting heart rate. Is there a way to get the calculated resting heart rate from the Google Fit Android API?
I use the Google Fit API but with Python, so I am unsure if this helps.
I set the scope in the Google API console:
https://www.googleapis.com/auth/fitness.heart_rate.read
I then select this datasource to retrieve the resting heart rate (RHR) for that day (or time period):
derived:com.google.heart_rate.bpm:com.google.android.gms:resting_heart_rate<-merge_heart_rate_bpm
The JSON response is as follows, the RHR is shown in "fpval" key.
{
"minStartTimeNs": "1606262400000000000",
"maxEndTimeNs": "1606344504000000000",
"dataSourceId": "derived:com.google.heart_rate.bpm:com.google.android.gms:resting_heart_rate<-merge_heart_rate_bpm",
"point": [
{
"startTimeNanos": "1606320000000000000",
"endTimeNanos": "1606320000000000000",
"dataTypeName": "com.google.heart_rate.bpm",
"originDataSourceId": "derived:com.google.heart_rate.bpm:com.google.android.gms:resting_heart_rate<-merge_heart_rate_bpm",
"value": [
{
"fpVal": 64.49201202392578, <----- RHR
"mapVal": []
}
],
"modifiedTimeMillis": "1606323455964"
}
]
}
As far as I can tell there is no data type for resting heart rate in the Android API. Using the information shared by Mr L I surmised that dataSourceId
is constructed from four Android API calls. It appears that dataSourceId
is composed as follows:
data source type
:data type
:app package name
:stream name
which corresponds to:
derived
:com.google.heart_rate.bpm
:com.google.android.gms
:resting_heart_rate<-merge_heart_rate_bpm
This corresponds to the following DataSource.Builder() functions in the Android API:
"com.google.android.gms"
"resting_heart_rate<-merge_heart_rate_bpm"
Here is my solution:
DataReadRequest.Builder()
.aggregate(DataSource.Builder()
.setType(DataSource.TYPE_DERIVED)
.setDataType(DataType.TYPE_HEART_RATE_BPM)
.setAppPackageName("com.google.android.gms")
.setStreamName("resting_heart_rate<-merge_heart_rate_bpm")
.build())
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start, end, TimeUnit.SECONDS).build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With