Search code examples
androidgoogle-fit

How do I get distance using fitness api?


I am using Google Fit API, be specific using HistoryApi

I am using Fitness.HistoryApi.readDailyTotal to get steps, calories and distance. From that I got steps and calories but distance is creating problem. But onResult is not getting called. Any help would be highly appreciated.

PendingResult<DailyTotalResult> distanceResult = Fitness.HistoryApi
    .readDailyTotal(mClient, DataType.TYPE_DISTANCE_DELTA);

distanceResult.setResultCallback(new ResultCallback<DailyTotalResult>() {
    @Override
    public void onResult(DailyTotalResult dailyTotalResult) {
        if (dailyTotalResult.getStatus().isSuccess()) {
            DataSet totalSet = dailyTotalResult.getTotal();
            long distance = totalSet.isEmpty()? 0: totalSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE).asInt();
            Log.i("-------------", "distance= " + distance);
        }
    }
});

From above code if (dailyTotalResult.getStatus().isSuccess()) { this is false in case of distance and it returns true while I try to get steps and calories.

All above code is run in background thread.


Solution

  • Can you please try adding below scope ?

    .addScope(new Scope(Scopes.FITNESS_LOCATION_READ))

    Also upgrade with new fit api:

    compile 'com.google.android.gms:play-services-fitness:10.0.0'