0

With the following code, i could get the response but with a empty dataset. where as in googlefit app, steps are shown. how to get exact steps shown in googlefit app through history api? i tried for weight, calories.. every time, response is coming as a empty dataset! I have wasted last 3 days on this.

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

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();

PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData(mClient, readRequest);
  pendingResult.setResultCallback(new ResultCallback<DataReadResult>(){
    @Override
    public void onResult(@NonNull DataReadResult dataReadResult) {
      parseData(dataReadResult);
    }
  });

private static void parseData(DataReadResult dataReadResult) {
if (dataReadResult.getBuckets().size() > 0) {
  Log.i(TAG,
      "Number of returned buckets of DataSets is: " + dataReadResult.getBuckets().size());
  for (Bucket bucket : dataReadResult.getBuckets()) {
    List<DataSet> dataSets = bucket.getDataSets();
    for (DataSet dataSet : dataSets) {
      dumpDataSet(dataSet);
    }
  }
} else if (dataReadResult.getDataSets().size() > 0) {
  Log.i(TAG, "Number of returned DataSets is: " + dataReadResult.getDataSets().size());
  for (DataSet dataSet : dataReadResult.getDataSets()) {
    dumpDataSet(dataSet);
  }
}
  }
Srinivas
  • 21
  • 6

2 Answers2

0

Related SO thread.

Before you retrieve data using History API you have to record it first by using the Recording API:

Your app can record the user's step count by using the Recording API to create a subscription to the DataType.TYPE_STEP_COUNT_CUMULATIVE data type, as shown in the following example:

public void subscribe() {
    // To create a subscription, invoke the Recording API. As soon as the subscription is
    // active, fitness data will start recording.
    Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        if (status.getStatusCode()
                                == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                            Log.i(TAG, "Existing subscription for activity detected.");
                        } else {
                            Log.i(TAG, "Successfully subscribed!");
                        }
                    } else {
                        Log.w(TAG, "There was a problem subscribing.");
                    }
                }
            });
}

Google Fit stores the step data from the subscription even if the app is not running, and restores the subscription when the system restarts. Your app can read the daily step total from the user's fitness history.

This exact same question was addressed in the Google Fit FAQs which is found here.

The Fit app uses a specific data source for steps. It adds the following functionality on top of the default merged steps stream:

Steps recorded during biking, driving, and some other non-moving activities are removed. When an activity that involves steps (such as walking or running) doesn't have a reasonable number of steps associated with it, steps for the activity are estimated. You can access the "estimated" steps stream as shown here:

Via the REST API:

derived:com.google.step_count.delta:com.google.android.gms:estimated_step>s Via the Android API:

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

Note that even when using the right data source, your step count may still be different from that of the Google Fit app. This could be due to one of the following reasons:

On Wear, the Fit MicroApp when connected will display step counts queried on the phone and transferred over via the Wearable APIs. Other MicroApps accessing local-only data will only get watch steps. We are working on making this easier for developers. Sometimes the step calculation code for the Google Fit app is updated with bug fixes before we are able to release the fixes to developers (which requires a Google Play Services release). We are also working on making it possible for developers to access fixes at the same time.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
0

My way: case DataStepCounterRequest:

            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();


            return new DataReadRequest.Builder()
                    .aggregate(ds, DataType.AGGREGATE_STEP_COUNT_DELTA)
                    .bucketByTime(1, TimeUnit.DAYS)
                    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                    .enableServerQueries()
                    .build();
NikolayZ
  • 1
  • 4