简体   繁体   English

Google Fit History API - 步骤值不正确

[英]Google Fit History API - incorrect values for steps

I'm trying to get the walked steps from today.我正在尝试从今天开始步行。 Therefor I've found 2 solutions.因此,我找到了 2 个解决方案。 1) 1)

private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 00);
    long startTime = cal.getTimeInMillis();

    final DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();

    DataReadResult dataReadResult =
            Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);

    DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);

    int totalSteps = 0;

    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            int steps = dp.getValue(field).asInt();

            totalSteps += steps;

        }
    }}

2) 2)

private void getStepsToday() {
    PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mGoogleApiFitnessClient, DataType.TYPE_STEP_COUNT_DELTA);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
        DataSet totalSet = totalResult.getTotal();
        int total = totalSet.isEmpty()
                ? 0
                : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
        publishTodaysStepData(total);
    } else {
        publishTodaysStepData(0);
    }
}

By using the first one I get for example 27 and by using the second one 1425 steps as the answer.通过使用第一个我得到例如 27 并使用第二个 1425 步作为答案。 The right one (after comparing with google fit app) should be 1425. Thus why is the first one not working?正确的(在与 google fit 应用程序比较后)应该是 1425。那么为什么第一个不起作用?

I also have the same problem by asking for steps from last week.我也有同样的问题,询问上周的步骤。 By using method 1 for steps from last week I realized I do get steps for the right days (sometimes even the right ones), but whenever the steps value is more than 50 (I think) the value is not correct.通过对上周的步骤使用方法 1,我意识到我确实在正确的日子(有时甚至是正确的日子)获得了步骤,但是每当步骤值超过 50(我认为)时,该值就不正确。

Does anyone has an answer to this strange behavior?有没有人对这种奇怪的行为有答案?

Try this 尝试这个

public int getStepsCount(long startTime, long endTime) {
    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();
    PendingResult<DataReadResult> pendingResult = Fitness.HistoryApi
            .readData(
                    fitnessClient,
                    new DataReadRequest.Builder()
                            .aggregate(ESTIMATED_STEP_DELTAS,
                                    DataType.AGGREGATE_STEP_COUNT_DELTA)
                            .bucketByTime(1, TimeUnit.HOURS)
                            .setTimeRange(startTime, endTime,
                                    TimeUnit.MILLISECONDS).build());
    int steps = 0;
    DataReadResult dataReadResult = pendingResult.await();
    if (dataReadResult.getBuckets().size() > 0) {
        //Log.e("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) {
                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;
}

Are you sure that TYPE_STEP_COUNT_DELTA is the correct DataType to request? 您确定TYPE_STEP_COUNT_DELTA是要请求的正确数据DataType吗? From the docs (their emphasis): 从文档(他们的重点):

In the com.google.step_count.delta data type, each data point represents the number of steps taken since the last reading . com.google.step_count.delta数据类型中,每个数据点代表自上次读取以来采取的步骤数。 When using this data type, each step is only ever reported once, in the reading immediately succeeding the step. 使用此数据类型时,在紧接该步骤的读取中,每个步骤仅报告一次。 By adding all of the values together for a period of time, the total number of steps during that period can be computed. 通过在一段时间内将所有值相加,可以计算出该时间段内的总步数。

As an example, if a user walked a total of 5 steps, with 3 different readings, the values for each reading might be [1, 2, 2]. 例如,如果用户总共走了5步,有3个不同的读数,则每个读数的值可能是[1、2、2]。

Do you get the expected results if you use AGGREGATE_STEP_COUNT_DELTA instead? 如果改用AGGREGATE_STEP_COUNT_DELTA您会得到预期的结果吗?

If you need step counts from readDailyTotal() and readData() to be equal, you have to specify the dataSource as: 如果需要readDailyTotal()和readData()的步数相等,则必须将dataSource指定为:

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

This count will match the count from Google FIT app. 此计数将与Google FIT应用中的计数匹配。 See here 看这里

I know this is a bit old but stumbled here and eventually found this reference page.我知道这有点旧,但在这里偶然发现并最终找到了这个参考页面。 I'm not sure if the packages have been upated but it says that the getDailyTotal() is the equvaliant to:我不确定这些包是否已经更新,但它说 getDailyTotal() 等同于:

   readData(client, new DataReadRequest.Builder()
       .setTimeRange(midnight.getMillis(), now.getMillis(), TimeUnit.MILLISECONDS)
       .bucketByTime(1, TimeUnit.DAYS)
       .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
       .build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM