'How do I get the all time data from Google Fit API?
I need to get the number of steps the user walked since he started using the app till today. How do I do this using the Google FIT api?
Solution 1:[1]
You can use the Fitness History API that enables your app to perform bulk operations on the fitness store such as reading, inserting, updating, and deleting fitness data.
You need to create a DataReadRequest instance to read data from the fitness history.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();
java.text.DateFormat dateFormat = DateFormat.getDateInstance();
Log.e("History", "Range Start: " + dateFormat.format(startTime));
Log.e("History", "Range End: " + dateFormat.format(endTime));
//Check how many steps were walked and recorded in the last 7 days
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
You can also group this data together in Bucket objects by time or sessions. For this example, you request aggregated step data for the last week and bucket that data by day.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.HOUR_OF_DAY, -1);
long startTime = cal.getTimeInMillis();
DataSource dataSource = new DataSource.Builder()
.setAppPackageName(this)
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setName("Step Count")
.setType(DataSource.TYPE_RAW)
.build();
int stepCountDelta = 1000000;
DataSet dataSet = DataSet.create(dataSource);
DataPoint point = dataSet.createDataPoint()
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
point.getValue(Field.FIELD_STEPS).setInt(stepCountDelta);
dataSet.add(point);
This example from this tutorial uses aggregated data points where each DataPoint represents the number of steps walked in a day.
Check this related So questions:
Solution 2:[2]
I've had that question myself. What i did was build a DataRequest with:
endTime now (today):
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
and start time when the first version of Google fit app was released:
cal.set(2014, 9, 28);
cal.set(Calendar.HOUR_OF_DAY, 0); //so it get all day and not the current hour
cal.set(Calendar.MINUTE, 0); //so it get all day and not the current minute
long startTime = cal.getTimeInMillis();
DataReadRequest build: (you can bucket if you want)
DataReadRequest readRequest = new DataReadRequest.Builder()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.read(DataType.AGGREGATE_STEP_COUNT_DELTA)
.enableServerQueries()
.build();
This way you get all the data starting from 28 October 2014
Solution 3:[3]
I tried the solution given by JPL and it worked for me, don't use .bucketByTime() attribute, in the response you will get List of DataSet
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();
In response use:
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 | Community |
| Solution 2 | JPL |
| Solution 3 | Sujith V |
