Step count retrieved through Google Fit Api does not match Step count displayed in Google Fit Official App

给你一囗甜甜゛ 提交于 2019-11-27 19:31:07

I found the solution.

The Fit app does some additional processing on top of the steps. It estimates steps based on the activity when none are recorded.

If it can help someone : You need to use a custom DataSource of the package com.google.android.gms

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

And use this in your aggregate method like this :

DataReadRequest readRequest = new DataReadRequest.Builder()
            .aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

Google Play Services 7.3 (released 4/28/2015) added a new method to the HistoryApi.readDailyTotal, which matches the step count on Google Fit official app and is easier to use.

    PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(fitnessApiClient, DataType.AGGREGATE_STEP_COUNT_DELTA);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
        DataSet totalSet = totalResult.getTotal();
        steps = totalSet.isEmpty() ? -1 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
    }
sealskej

As Sameer Z. requested I'm posting full code to get values.

DataSource estimatedSteps = 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(estimatedSteps, DataType.AGGREGATE_STEP_COUNT_DELTA)
        .setTimeRange(startTimeSeconds, endTimeSeconds, TimeUnit.SECONDS)
        .bucketByTime(1, TimeUnit.DAYS)
        .enableServerQueries()
        .build();

PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi.readData(client, readRequest);
pendingResult.setResultCallback(new ResultCallback<DataReadResult>() {
    @Override
    public void onResult(@NonNull DataReadResult dataReadResult) {
        List<Bucket> allBuckets = dataReadResult.getBuckets();

        for (Bucket bucket : allBuckets) {
            long startAtSeconds = bucket.getStartTime(TimeUnit.SECONDS);

            Value stepsValue = getValue(bucket, DataType.TYPE_STEP_COUNT_DELTA, Field.FIELD_STEPS);
            int steps = stepsValue != null ? stepsValue.asInt() : 0;

            Log.d(TAG, String.format("startAtSeconds %s, steps %s", startAtSeconds, steps));
        }
    }
});

Google Fit sdk is a part of Google Play services.. The official Google Fit app also collects the same data from the Google Play services sdk but Google probably has added a few more code into the Google Fit app to make the data more accurate. It could also be a bug in the Google Fit app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!