Read the Daily Step Total

This section demonstrates reading current daily step count data using the Fit Android API and Fit REST API.

Android

Your app can read the current daily step total by calling HistoryApi.readDailyTotal, as shown in the following example:

private class VerifyDataTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {

        long total = 0;

        PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mClient, DataType.TYPE_STEP_COUNT_DELTA);
        DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
        if (totalResult.getStatus().isSuccess()) {
            DataSet totalSet = totalResult.getTotal();
            total = totalSet.isEmpty()
                    ? 0
                    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
        } else {
            Log.w(TAG, "There was a problem getting the step count.");
        }

        Log.i(TAG, "Total steps: " + total);

        return null;
    }
}

The daily total is computed from midnight of the current day on the device's current timezone.

To get the same daily step count as the Fit app, create a data source using the com.google.android.gmsapp package, as shown in the following example:

final DataSource ds = new DataSource.Builder()
  .setAppPackageName("com.google.android.gms")
  .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
  .setType(DataSource.TYPE_DERIVED)
  .setStreamName("estimated_steps")
  .build();

final DataReadRequest req = new DataReadRequest.Builder()
.aggregate(ds, DataType.AGGREGATE_STEP_COUNT_DELTA)
    .bucketByTime(1, TimeUnit.DAYS)
  .setTimeRange(timeBounds[0], timeBounds[1], TimeUnit.MILLISECONDS)
  .build();

For more information about working with aggregate data sources, see Work with the Fitness History.

REST

Your app can read the current daily step count total across all data sources by making a POST request and querying the com.google.step_count.delta data type for the specified time period.

HTTP method

POST

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

Request body

{
  "aggregateBy": [{
    "dataTypeName": "com.google.step_count.delta",
    "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1438705622000,
  "endTimeMillis": 1439310422000
}

Curl command

curl \
-X POST \
-H "Content-Type: application/json;encoding=utf-8" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d @aggregate.json \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

Send feedback about...