1

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?

Ramya Ramesh
  • 299
  • 3
  • 18

3 Answers3

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:

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59
  • now if want to get the data ever since the user started using the app, till today, what will be the startTime and endTime? And what bucketing strategy should i use? – Ramya Ramesh Apr 12 '16 at 09:01
  • My question was how to find out the number of steps walked ever since the time user installed the app, till now. Your answer is totally irrelavant. – Ramya Ramesh Apr 12 '16 at 10:27
  • Based from my example, you can use `DataReadRequest` to check how many steps were walked and recorded. Once you have your `DataReadRequest` created, you can request data from the `GoogleApiClient` using the `Fitness.HistoryApi.readData` command as stated [here](http://code.tutsplus.com/tutorials/google-fit-for-android-history-api--cms-25856). If the call returns successfully, you are able to access the user's fitness data from the `DataReadResult` object. – abielita Apr 12 '16 at 10:52
  • @abielita Can you please help me with sleep data in Google Fit? Here is my question : http://stackoverflow.com/q/43780050/7382665 – Shruti May 04 '17 at 10:21
1

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

JPL
  • 33
  • 4
0

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);
                }
Sujith V
  • 1
  • 1