Google FIT api returns different step counts

我怕爱的太早我们不能终老 提交于 2019-12-12 02:17:35

问题


Stepcount is different when accessed by getDailyTotal and getData bucketed by day. Please help.

Fitness.HistoryApi.readDailyTotal(App.gClient, DataType.TYPE_STEP_COUNT_DELTA)-> returns step count as 1515

private void getStepsDataHistory(long startTime, long endTime) {
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.bucketByTime(1, TimeUnit.DAYS)
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();

-> returns data for a week, where today's steps =2877

EDIT: I also get this warning in the logcat: Couldn't find aggregated data in Shared Pref for DataType{com.google.step_count.delta{steps(i)}}.


回答1:


Please make sure that the start time and end time you gave are correct i.e from 12Am midnight to now. However readDailyTotal can be useful if you require step data for use in areas where you are unable to show the permissions panel (for example, Android Wear watch faces). So i advised you to use Aggregate method in that way you will get quiet equal step count as google fit app . Check this out .

public int getStepsCount(long startTime, long endTime) {
    PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi
            .readData(
                    fitnessClient,
                    new DataReadRequest.Builder()
                            .aggregate(DataType.TYPE_STEP_COUNT_DELTA,
                                    DataType.AGGREGATE_STEP_COUNT_DELTA)
                            .bucketByTime(1, TimeUnit.DAYS)
                            .setTimeRange(startTime, endTime,
                                    TimeUnit.MILLISECONDS)
                                    .build());
    int steps = 0;
    DataReadResult dataReadResult = pendingResult.await();
    if (dataReadResult.getBuckets().size() > 0) {
        for (Bucket bucket : dataReadResult.getBuckets()) {
            List<DataSet> dataSets = bucket.getDataSets();
            for (DataSet dataSet : dataSets) {
                for (DataPoint dp : dataSet.getDataPoints()) {
                    for (Field field : dp.getDataType().getFields()) {
                        steps += dp.getValue(field).asInt();
                    }
                }
            }
        }
    } else if (dataReadResult.getDataSets().size() > 0) {
        for (DataSet dataSet : dataReadResult.getDataSets()) {
            for (DataPoint dp : dataSet.getDataPoints()) {
                for (Field field : dp.getDataType().getFields()) {
                    steps += dp.getValue(field).asInt();
                }
            }
        }
    }
    return steps;
}


来源:https://stackoverflow.com/questions/35546979/google-fit-api-returns-different-step-counts

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