1

I am trying to get steps, distance, calories from google fit API. I use GoogleApiClient for accessing data from Google Fit. I ask per Google Fit permission also allow to access all data from Google Fit:

This permission pop up comes every time. I need to allow 4 to 5 times then I can possible to get steps from Google Fit .

What I need to do to get Google Fit permission very first time. So please help me to solve this issue.

mGoogleApiClient1 = new GoogleApiClient.Builder(this)
    .addApi(Fitness.HISTORY_API)
    .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
    .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
    .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
    .addConnectionCallbacks(this)
    .enableAutoManage(this, Constants.googleApi, this)
    .build();

2. I use below API to get steps distance and calories Please tell why I am not getting consistent data from Google Fit. It always get steps data little more than original steps from Google Fit.

// get Steps data from bellow api
private void getStepDataForToday() {
    DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient1, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
    showDataSet(result.getTotal());
}

private void getDistanceDataForToday() {
    DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient1, DataType.TYPE_DISTANCE_DELTA ).await(1, TimeUnit.MINUTES);
    showDataSet(result.getTotal());
}

private void getCaloriesDataForToday() {
    DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient1, DataType.TYPE_CALORIES_EXPENDED ).await(1, TimeUnit.MINUTES);
    showDataSet(result.getTotal());
}

Thank you and waiting for help

siegi
  • 5,646
  • 2
  • 30
  • 42
kiran kirve
  • 11
  • 1
  • 2

1 Answers1

1

For the Fit permission, make sure or double check if you follow properly the direction in Authorization in Android.

For the steps, distance and calories data you got that is little more than original steps from Google Fit?

It is stated in the FAQ of Google FIT that:

Because of how Google Fit updates are released, it may be possible that the Google Fit app has a more recent version of our data analysis code than Google Play Services, which is released less often.

Our backends, however, always have the most recent version of the data, and everything gets processed there eventually and should match up after a couple of cloud syncs. Let us know if that’s not the case.

We’re trying to make this situation better by making it possible for apps to request that we process a cloud sync instantly so that everything is in sync, and also by making it so that syncs happen automatically when significant data is entered or changes.

This could also be caused by not reading the proper data sources. To match the value of Google Fit, you should be reading data like 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(ESTIMATED_STEP_DELTAS,    DataType.AGGREGATE_STEP_COUNT_DELTA)
       .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
       .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
       .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
       .bucketByTime(1, TimeUnit.DAYS)
       .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
       .build();

For more information, check this related SO question.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31