I managed to record and read the grouped data with the following code:
private void suscribeDataFitness(){
Fitness.RecordingApi.subscribe(mApiClient, DataType.AGGREGATE_HEART_RATE_SUMMARY)
.setResultCallback(mSubscribeResultCallback);
}
private void readDataFitnessHistory()
{
// Setting a start and end date using a range of 1 week before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = getDateInstance();
Log.d(TAG, "Range Start: " + dateFormat.format(startTime) + " Millis : " + startTime);
Log.d(TAG, "Range End: " + dateFormat.format(endTime) + " Millis : " + endTime);
final DataReadRequest readRequest = new DataReadRequest.Builder()
//.read(DataType.TYPE_STEP_COUNT_DELTA)
//.read(DataType.TYPE_HEART_RATE_BPM)
//.read(ESTIMATED_STEP_DELTAS)
.aggregate(DataType.AGGREGATE_HEART_RATE_SUMMARY)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.bucketByTime(1, TimeUnit.HOURS)
.build();
// Invoke the History API to fetch the data with the query and await the result of
// the read request.
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mApiClient, readRequest).await(1, TimeUnit.MINUTES);
showDataBuckets(dataReadResult);
}
final DataReadRequest readRequest = new DataReadRequest.Builder()
//.read(DataType.TYPE_STEP_COUNT_DELTA)
//.read(DataType.TYPE_HEART_RATE_BPM)
//.read(ESTIMATED_STEP_DELTAS)
.aggregate(DataType.AGGREGATE_HEART_RATE_SUMMARY)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.bucketByTime(1, TimeUnit.HOURS)
.build();
private void showDataBuckets(DataReadResult dataReadResult) {
//Used for aggregated data
if (dataReadResult.getBuckets().size() > 0) {
Log.d(TAG,"History: "+ "Number of buckets: " + dataReadResult.getBuckets().size());
for (Bucket bucket : dataReadResult.getBuckets()) {
List<DataSet> dataSets = bucket.getDataSets();
for (DataSet dataSet : dataSets) {
showDataSet(dataSet);
}
}
}
}
I hope I can help you.
A greeting