无法使Google Fit Recording API正常运行

时间:2016-01-29 02:56:14

标签: android google-fit

我是Android Wearable和Google Fit的新手。我正在调查Google Fit Recording API的工作原理,方法是在主要活动中使用以下代码创建一个简单的Android应用程序(这些应用程序来自Google的Basic Recording API示例):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         ...
         buildFitnessClient();
    }

    private void buildFitnessClient() {
            // Create the Google API Client
            mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.RECORDING_API)
            .addScope(Fitness.SCOPE_LOCATION_READ_WRITE)
            .addScope(Fitness.SCOPE_ACTIVITY_READ_WRITE)
            .addScope(Fitness.SCOPE_BODY_READ_WRITE)
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {

                        @Override
                        public void onConnected(Bundle bundle) {
                            Log.i(TAG, "Connected!!!");
                            // Now you can make calls to the Fitness APIs.  What to do?
                            // Subscribe to some data sources!
                            subscribe();
                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                            // If your connection to the sensor gets lost at some point,
                            // you'll be able to determine the reason and react to it here.
                            if (i == ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                            } else if (i == ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                Log.i(TAG, "Connection lost.  Reason: Service Disconnected");
                            }
                        }
                    }
            )
            .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.i(TAG, "Google Play services connection failed. Cause: " +
                            result.toString());
                    Snackbar.make(
                            MainActivity.this.findViewById(R.id.main_activity_view),
                            "Exception while connecting to Google Play services: " +
                                    result.getErrorMessage(),
                            Snackbar.LENGTH_INDEFINITE).show();
                }
            })
            .build();
}

/**
 * Subscribe to an available {@link DataType}. Subscriptions can exist across application
 * instances (so data is recorded even after the application closes down).  When creating
 * a new subscription, it may already exist from a previous invocation of this app.  If
 * the subscription already exists, the method is a no-op.  However, you can check this with
 * a special success code.
 */
public void subscribe() {
    // To create a subscription, invoke the Recording API. As soon as the subscription is
    // active, fitness data will start recording.
    // [START subscribe_to_datatype]
    Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_DELTA)
                                      .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        if (status.getStatusCode()
                                == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                            Log.i(TAG, "Existing subscription for activity detected.");
                        } else {
                            Log.i(TAG, "Successfully subscribed!");
                        }
                    } else {
                        Log.i(TAG, "There was a problem subscribing: " + status.getStatusMessage());
                    }
                }
    });
    // [END subscribe_to_datatype]
}

我不确定录制API是如何工作的。根据Google Fit文档,API会自动处理步骤跟踪(在本例中)并将步骤数存储到Fitness Store中。所以我期望该应用程序的工作是启动应用程序,走一段时间,并在检查我的Google健身帐户时,应该增加步骤数。

然而,这从未发生过。

如果我的理解不对,请你纠正我,或者如果是,请指出正确的方向让这段代码有效。

干杯

更新一个多小时后,我的Google健身数据显示步数从3033增加(之前由Fit应用生成,然后被移除以安全地测试此应用) 5011.增加的数量是非常令人困惑的,因为我通过摇动我的手机模拟步骤,当然我没有清酒2000次!我使用Sensor API实时显示步数,这些数字的总数仅低于200。

此外,我每10-20分钟手动检查一次数据,我确信数据更新需要1个多小时。根据Google Fit的文档,数据存储(进入Fitness Store)是以“节省电池的方式”自动完成的。然而,它没有清楚地提到如何做到这一点,即频率如何。

如果有人可以帮助我解决这些问题,那将会很棒。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您可以使用每项活动的置信度来过滤它吗?

例如。 可能检测到步行的值的示例可以是:

walking -> 20.0
in_vehicle -> 10.0
biking -> 66.6