'How to get all data of a particular DataType from google fitness store in android

I need to get all data of a particular DataType eg: "DataType.TYPE_HEART_RATE_BPM" from the fitness store. In the following code, I am requesting data with startTime and endTime and i am getting the response, but i want all the data of the particular DataType and not the data stored within the time interval. I have tried out the code given in How do I get the all time data from Google Fit API? but it didn't work

    Calendar calendar= Calendar.getInstance();
    calendar.setTime(new Date());
    long endTime=calendar.getTimeInMillis();
    calendar.add(Calendar.WEEK_OF_YEAR, -1);
    long startTime = calendar.getTimeInMillis();

    DataReadRequest readRequest = new DataReadRequest.Builder()
    .read(DataType.TYPE_HEART_RATE_BPM)
    .bucketByTime(5, TimeUnit.MINUTES)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .enableServerQueries()
    .build();


Solution 1:[1]

This worked for me. Do not use .bucketByTime() attribute. The response will be a List of DataSet. Starting date is Google Fit's initial release date (https://en.wikipedia.org/wiki/Google_Fit)

Calendar cal = Calendar.getInstance();

Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(2014, 9, 28);
cal.set(Calendar.HOUR_OF_DAY, 0); 
cal.set(Calendar.MINUTE, 0);
long startTime = cal.getTimeInMillis();

DataReadRequest readRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build();

To read the response:

if (response.getDataSets().size()>0){
       for (DataSet dataSet :response.getDataSets())
              dumpDataSet(dataSet);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stanley mbote