简体   繁体   English

Google Fit计步器历史记录不返回任何内容

[英]Google Fit step counter history returns nothing

I want to write an app which displays all steps done this week by asking the Google Fit API. 我想编写一个应用程序,通过询问Google Fit API来显示本周完成的所有步骤。 Unfortunately my History API call always returns empty DataSets. 不幸的是,我的History API调用始终返回空的DataSet。

For debugging I am using a Nexus 9 Tablet with Google Fit installed. 为了进行调试,我使用的是装有Google Fit的Nexus 9平板电脑。 I added steps manually in the Google Fit app. 我在Google Fit应用中手动添加了步骤。

This is my code to get the steps 这是我获取步骤的代码

OnCreate: OnCreate:

...
mApiClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addApi(Fitness.RECORDING_API)
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
...

Function to get steps 获取步骤的功能

private void showSteps() {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.WEEK_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

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

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


    if (dataReadResult.getBuckets().size() > 0) {
        Log.i(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) {
                Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                for (DataPoint dp : dataSet.getDataPoints()) {
                    Log.i(TAG, "Data point:");
                    Log.i(TAG, "\tType: " + dp.getDataType().getName());
                    Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                    Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                    for(Field field : dp.getDataType().getFields()) {
                        Log.i(TAG, "\tField: " + field.getName() +
                                " Value: " + dp.getValue(field));
                    }
                }
            }
        }
    } else if (dataReadResult.getDataSets().size() > 0) {
        Log.i(TAG, "Number of returned DataSets is: "
                + dataReadResult.getDataSets().size());
        for (DataSet dataSet : dataReadResult.getDataSets()) {
            Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            for (DataPoint dp : dataSet.getDataPoints()) {
                Log.i(TAG, "Data point:");
                Log.i(TAG, "\tType: " + dp.getDataType().getName());
                Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                for (Field field : dp.getDataType().getFields()) {
                    Log.i(TAG, "\tField: " + field.getName() +
                            " Value: " + dp.getValue(field));
                }
            }
        }
    }

I receive buckets and they have dataSets but they never have any DataPoints. 我收到了存储桶,它们有数据集,但是它们从没有任何数据点。 The code just finishes without errors. 代码刚刚完成而没有错误。 The ApiClient returns that it is indeed connected to Google Fit. ApiClient返回它确实已连接到Google Fit。

Why does the History API not return my steps I added to the Google Fit app? 为什么History API不返回我添加到Google Fit应用中的步骤?

Thanks in advance! 提前致谢!

As it turns out the code was fine. 事实证明,代码很好。 I noticed that I was logged into a different google account on my tablet when I added the steps. 添加步骤时,我注意到我在平板电脑上登录了其他Google帐户。 Obviously the account has to be the same. 显然,帐户必须相同。

Try adding .enableServerQueries() to your Data read request. 尝试将.enableServerQueries()添加到您的数据读取请求中。 It fetches data from Google fit server when not available locally. 当本地不可用时,它将从Google Fit服务器获取数据。

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

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