I am tyrying to grab the Heart Rate Summary for one week from the Google Api, but I get no result just the datatype name. If I grab only for a specific hour the Api return the heart rate grabbed from mi Mi Band 5 every 5 minutes or so. If anyone could help I would be grateful.
Builder Function:
private fun queryFitnessData2(): DataReadRequest {
// [START build_read_data_request]
// Setting a start and end date using a range of 1 week before this moment.
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
val now = Date()
calendar.time = now
val endTime = calendar.timeInMillis
calendar.add(Calendar.WEEK_OF_YEAR, -1)
val startTime = calendar.timeInMillis
Log.i(TAG, "Range Start: ${dateFormat.format(startTime)}")
Log.i(TAG, "Range End: ${dateFormat.format(endTime)}")
return DataReadRequest.Builder()
.aggregate(DataType.TYPE_HEART_RATE_BPM, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.enableServerQueries()
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
}
Data Printer:
private fun printData(dataReadResult: DataReadResponse) {
// [START parse_read_data_result]
// If the DataReadRequest object specified aggregated data, dataReadResult will be returned
// as buckets containing DataSets, instead of just DataSets.
if (dataReadResult.buckets.isNotEmpty()) {
Log.i(TAG, "Number of returned buckets of DataSets is: " + dataReadResult.buckets.size)
for (bucket in dataReadResult.buckets) {
bucket.dataSets.forEach { dumpDataSet(it) }
}
} else if (dataReadResult.dataSets.isNotEmpty()) {
Log.i(TAG, "Number of returned DataSets is: " + dataReadResult.dataSets.size)
dataReadResult.dataSets.forEach { dumpDataSet(it) }
}
// [END parse_read_data_result]
}
Dataset Parser Function:
// [START parse_dataset]
private fun dumpDataSet(dataSet: DataSet) {
Log.i(TAG, "Data returned for Data type: ${dataSet.dataType.name}")
val dateFormat: DateFormat = getTimeInstance()
//THIS IS WHERE THE FOR LOOP DOESN'T WORK!!
for (dp in dataSet.dataPoints) {
Log.i(TAG, "Data point:")
Log.i(TAG, "\tType: ${dp.dataType.name}")
Log.i(TAG, "\tStart: ${dp.getStartTimeString()}")
Log.i(TAG, "\tEnd: ${dp.getEndTimeString()}")
dp.dataType.fields.forEach {
Log.i(TAG, "\tField: ${it.name} Value: ${dp.getValue(it)}")
}
}
}
// [END parse_dataset]
The only thing it gets logged is the data type which is the heart.rate.summary type.
Thefor loop doesn't work if I use the buckets for each day but works fine if I use them for each hour of the day. Any help is much appreciated.
I finally found a way to aggregate Heart Rate Data ( it works on my phone but not on the Android Studio's Emulator so have that in mind. First add a data type for the heart rate and the aggregated heart rate:
Then make the FitnessQuery Function as follows:
And finally the dataset dump in buckets where each bucket corresponds to one day:
Hope I helped anyone struggling with Google's horrible Api Documentation!