Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to read daily resting heart rate from the Google Fit API?

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?

like image 322
Steve Zeidner Avatar asked Nov 05 '19 15:11

Steve Zeidner


2 Answers

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"
        }
    ]
}
like image 178
Mr L Avatar answered Nov 07 '22 11:11

Mr L


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:

  • setType: value passed should be DataSource.TYPE_DERIVED
  • setDataType: value passed should be DataType.TYPE_HEART_RATE_BPM
  • setAppPackageName: value passed should be "com.google.android.gms"
  • setStreamName: value passed should be "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()
like image 1
trukvl Avatar answered Nov 07 '22 10:11

trukvl