如何从Google Fit REST API这样的Google Fit REST API获取步骤计数? [英] How to get step count from Google Fit REST API like Google Fit app?

查看:123
本文介绍了如何从Google Fit REST API这样的Google Fit REST API获取步骤计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个与Google Fit API配合使用的PHP应用程序,以收集用户的每日步数.

I'm developing a PHP application which work with Google Fit APIs to collect daily user's step count.

我想获取从"2015年1月15日00:00:00 GMT + 0700"到"2015年1月16日00:00:00 GMT + 0700"的步数. -首先,我得到了所有数据源. -然后,对于数据类型等于"com.google.step_count.delta"的每个数据源,我将获得上述时间戳之间的数据集,并将返回值加在一起.

I want to get my step count from "Jan 15 2015 00:00:00 GMT+0700" to "Jan 16 2015 00:00:00 GMT+0700". - First, I get all my datasources. - Then, with each datasource which have datatype equal to "com.google.step_count.delta", I get datasets between above timestamps and add return values together.

我的代码: https://gist.github.com/daitr-gu/472c4f18522172542cca
我的结果: https://gist.github.com/daitr-gu/1a7e11eb483a657bdc8b

My code: https://gist.github.com/daitr-gu/472c4f18522172542cca
My result: https://gist.github.com/daitr-gu/1a7e11eb483a657bdc8b

我发现,有许多数据源,它们返回不同的值.而且这些值与我在手机上的Google Fit应用中看到的值完全不同.

I found that, there are many datasources and they returns different values. And the values are too different from what I see in Google Fit app on my phone.

问题:
1. Google Fit应用程序使用哪个数据源来计算步数?
2.为什么数据源的价值和Google Fit的价值有所不同?
3.如何获得Google Fit的价值?

Questions:
1. Which datasource the Google Fit app use to calculate step count?
2. Why there are different between datasources's value and Google Fit value?
3. How can I get the Google Fit value?

推荐答案

我认为您看到的区别是Google使用History API和Sensors API的区别.如果您使用的是PHP,则通过可用的健身API查询Google Fit商店,这取决于应用程序通过录音API进行保存的能力.因此,您可能看不到设备可用的所有数据.

I think the difference you are seeing is the difference between how Google uses the History API and the Sensors API. If you are using PHP, you are querying the Google Fit Store via the available fitness API and this is then dependant on what the App has had the ability to save via the recording API. So you may not see all of the data the device has available.

我认为但不确定,Fit App使用传感器api.在该应用中,您可以按照Google Docs 传感器API 所述使用传感器API,并在您操作返回的数据时进行操作欲望.

I think, but don't know for certain, that the Fit App uses the sensors api. Within the App you can use the sensors API as described in the Google Docs Sensors API and manipulate the returned data as you desire.

下面显示了一种使用TYPE_STEP_COUNT_CUMULATIVE和TYPE_RAW的简单步骤

Below shows a simple way to get steps using TYPE_STEP_COUNT_CUMULATIVE and TYPE_RAW

Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
            // At least one datatype must be specified.
            .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
                    // Can specify whether data type is raw or derived.
            .setDataSourceTypes(DataSource.TYPE_RAW)
            .build())
            .setResultCallback(new ResultCallback<DataSourcesResult>() {
                @Override
                public void onResult(DataSourcesResult dataSourcesResult) {
                    Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
                    for (DataSource dataSource : dataSourcesResult.getDataSources()) {
                        Log.i(TAG, "Data source found: " + dataSource.toString());
                        Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());

                        //Let's register a listener to receive Activity data!
                        if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_CUMULATIVE) && mListener == null) {
                            Log.i(TAG, "Data source for TYPE_STEP_COUNT_CUMULATIVE found!  Registering.");
                            registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
                        }
                    }
                }
            });

private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {

    mListener = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {
            for (Field field : dataPoint.getDataType().getFields()) {
                Value val = dataPoint.getValue(field);
                Log.i(TAG, "Detected DataPoint field: " + field.getName());
                Log.i(TAG, "Detected DataPoint value: " + val);

                Log.i(TAG, "Difference in steps: " + (val.asInt()-previousValue));

                previousValue = val.asInt();
            }
        }
    };

    Fitness.SensorsApi.add(
            mClient,
            new SensorRequest.Builder()
                    .setDataSource(dataSource) // Optional but recommended for custom data sets.
                    .setDataType(dataType) // Can't be omitted.
                    .setSamplingRate(10, TimeUnit.SECONDS)
                    .build(),
            mListener)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Listener registered!");
                    } else {
                        Log.i(TAG, "Listener not registered.");
                    }
                }
            });
}

您可能会发现它给您带来的价值与Fit App所提供的价值更接近.但是,这显然仅在设备上可用,因此您将需要运行后台服务来更新外部数据库,这是Recording and History API为您提供的.

You may find this gives you a closer value to that given by the Fit App. However this is obviously only available on the device, so you would then need to run a background service that updated an external database, which is what the Recording and History APIs give you.

要确保在应用程序处于后台运行状态时将数据继续发送到Fitness Store,请注意,这也可能会更改您看到的值.

As a point of note to ensure that data continues to be sent to the Fitness Store when your app is in background you need to use the Recording API, this may also change the values you are seeing.

更新:

已经写了以上内容,我认为我应该对其进行测试.这是一个早晨的步行.

Having written the above I thought I ought to test it. This was from a mornings walking.

  • Apple iPhone 6 HealthKit:6,762
  • Apple iPhone 6我的应用程序:6,762
  • Android Nexus 6适合:6,920
  • Android Nexus 6我的应用:6,920(使用HistoryAPI)
  • Android Rest API估计步骤:6,928
  • Android Rest API merge_step_deltas:6,911

这是来自Google+帖子,您可以在此处

This is from a Google+ post you can find here

"merge_step_deltas会为您提供所有步数计数器的合并流.estimate_steps还考虑了活动,并在不存在活动时估算步数".

"merge_step_deltas gives you the merged stream of all your step counters. estimated_steps also takes into account activity, and estimates steps when there are none"

我还没有深入了解的就是使用上面显示的传感器,它只给了我2548个步骤.

The one I haven't got to the bottom of yet is the sensors using what I show above, it only gives me 2,548 steps.

另一个奇怪的事情是,一天后Fit向我展示了我做了6668个步骤,与Apple的结果非常接近,但是根据数据同步后最初显示给我的内容进行了重新计算.我的应用仍然显示6,920!

The other marginally curious thing is that a day later Fit shows me I did 6,668 steps, so closer to the Apple result, but a recalculation from what it initially showed me after the data had synced. My app still shows 6,920!

我没有测量它同步的时间.

The time for it all to sync I didn't measure.

这篇关于如何从Google Fit REST API这样的Google Fit REST API获取步骤计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆