【问题标题】:Steps Count using sensor with google fit使用带有 google fit 的传感器计算步数
【发布时间】:2016-07-20 01:38:19
【问题描述】:

我在我的应用程序中使用 Google 健康包。我知道 Health kit 不直接提供传感器步数。我阅读了 google fit 文档,我发现我们可以在后台使用 Recording api 进行步数。因此,如果可以使用 Recording api 和 Sensor api 在后台获取步数,请告诉我如何实现这一点。我想在后台感知用户活动以及用户在该活动期间采取了多少步。任何帮助将不胜感激。

根据 google fit 文档,如果我的应用程序订阅记录数据类型,那么即使我的应用程序没有运行,它也会记录该类型的数据并将其存储到 HISTORYAPI 中。这是订阅代码

Fitness.RecordingApi.subscribe(fitnessClient, DataType.TYPE_ACTIVITY_SAMPLE)
    .setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            if (status.isSuccess()) {
                if (status.getStatusCode()
                        == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                    Log.e(TAG, "Existing subscription for activity detected.");
                } else {
                    Log.e(TAG, "Successfully subscribed activity !");
                }
            } else {
                Log.e(TAG, "There was a problem subscribing.");
            }
        }
    });


Fitness.RecordingApi.subscribe(fitnessClient,DataType.TYPE_STEP_COUNT_DELTA).
        setResultCallback(new ResultCallback<Status>() {

            @Override
            public void onResult(Status arg0) {
                if(arg0.isSuccess()){
                    Log.e("Steps Recording","Subcribe");
                }
            }
        });

现在我已经订阅了步骤和活动。但直到现在它什么都没有感应到。谁能解释订阅记录数据类型的目的是什么。

【问题讨论】:

    标签: android google-fit


    【解决方案1】:

    我刚刚在google上搜索了google fit API,似乎sensor API是用来从传感器获取数据的,比如用户的心率,Recording api是用来收集数据的,比如用户跑步时的地理位置数据, 并且使用历史 api 来编辑记录数据。记录的数据是google fit在后台收集的,这些数据可以存储在云端,但是这些数据存储在本地设备的哪里,或者这些数据会存储在本地设备中吗?我没有看到任何关于它的信息,在你的代码中我也没有看到它。我没有使用 google fit API 做任何项目,抱歉,帮不上忙。

    【讨论】:

    • 请再次检查问题..我已经更新了它
    【解决方案2】:

    我知道这是一个老问题,但我也需要这方面的帮助,所以我会尽力提供帮助:

    您的活动需要实现这一点:

    implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

    在 onCreate 之前创建这个变量:public GoogleApiClient mGoogleApiClient = null;

    在 onCreate 里面写这个:

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Fitness.HISTORY_API)
                    .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
                    .addConnectionCallbacks(this)
                    .enableAutoManage(this, 0, this)
                    .build();
    

    不要忘记 OAuth 身份验证。

    那么你需要这个方法:

    public void onConnected(@Nullable Bundle bundle) {
            Log.e("HistoryAPI", "onConnected");
        }
    @Override
        public void onConnectionSuspended(int i) {
            Log.e("HistoryAPI", "onConnectionSuspended");
        }
    
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.e("HistoryAPI", "onConnectionFailed");
    }
    
    
    public void onClick(View v) {
    
    }
    

    在此之后,您将创建此方法,读取计步器(Google Fit API 计算它们)

    public void displayStepDataForToday() {
            DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
            showDataSet(result.getTotal());
        }
    

    这是displayStepDataForToday()里面的showDataSet

    private void showDataSet(DataSet dataSet) {
            Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName());
            DateFormat dateFormat = DateFormat.getDateInstance();
            DateFormat timeFormat = DateFormat.getTimeInstance();
    
            for (DataPoint dp : dataSet.getDataPoints()) {
                Log.e("History", "Data point:");
                Log.e("History", "\tType: " + dp.getDataType().getName());
                Log.e("History", "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                Log.e("History", "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                for(Field field : dp.getDataType().getFields()) {
                    Log.e("History", "\tField: " + field.getName() +
                            " Value: " + dp.getValue(field));
                    //writeToFile(dp.getValue(field).asInt());
                    //this is how I save the data (wit the writeToFile)
                }
            }
    
        }
    

    最后,您需要创建一个类(在您的活动中)以使用displayStepDataForToday() 方法

    public class ViewTodaysStepCountTask extends AsyncTask<Void, Void, Void> {
            protected Void doInBackground(Void... params) {
                displayStepDataForToday();
                return null;
            }
        }
    

    您只需要在需要时启动活动,然后获取它的值。这在后台运行并根据我的需要进行更新。如果您希望此代码更新得更快,您可以进行一些研究,但我认为您需要更改此行:

     public void displayStepDataForToday() {
            DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多