简体   繁体   English

使用谷歌飞度使用传感器进行步骤计数

[英]Steps Count using sensor with google fit

I am using Google health kit in my application . 我在应用程序中使用Google Health Kit。 I know that Health kit doesn't provide the Sensor Steps Count directly .I read the google fit Documentation And i found that we can use Recording api for Step Count in background . 我知道Health Kit不能直接提供Sensor Steps Count。我阅读了google fit文档,发现可以在后台使用Recording api进行Step Count。 So if it is possible to use Recording api and Sensor api To get the step Counts in background ,Please Tell me how to achieve this. 因此,如果可以使用Recording api和Sensor api来获取后台的Counts步骤,请告诉我如何实现。 I Want to sense the user activity and how many steps user took during that activity in background . 我想在后台感测用户活动以及用户在该活动期间执行了多少步骤。 Any help Would be appreciated . 任何帮助,将不胜感激 。

As per the google fit documentation if my application subscribe for recording a data type then it will record the data of that type and store it into HISTORYAPI even if my app is not running. 根据谷歌适合文档,如果我的应用程序订阅记录数据类型,那么它将记录该类型的数据并将其存储到HISTORYAPI中,即使我的应用程序未运行也是如此。 This is the subscription code 这是订阅代码

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");
                }
            }
        });

Now i have subscribe for the steps and activity. 现在,我已经订阅了步骤和活动。 But till now it is not sensing anything . 但是到目前为止,它什么都没有感觉到。 Can anyone explain What is the purpose of subscribe recording a datatype . 任何人都可以解释预订记录数据类型的目的是什么。

I just googled about google fit API, it seems the sensor API is use to get the data from the sensor such as user's heart rate, the Recording api is used to collect the data, such as the geolocation data when user running is, and a history api is used to edit the record data. 我只是用Google Fit API搜索Google,似乎传感器API用于从传感器获取数据,例如用户的心律,Recording API用于收集数据,例如当用户正在运行时的地理位置数据,以及history api用于编辑记录数据。 The data which is recorded is collected by google fit in the background, this data can be stored in the cloud, but where is this data stored in local device or will this data be stored in local device? 记录的数据由Google在后台收集,该数据可以存储在云中,但是此数据存储在本地设备中的什么位置,或者该数据将存储在本地设备中? I did't see any information about it, and in your code I didn't see it too. 我没有看到任何有关它的信息,在您的代码中我也没有看到它。 I didn't do any project using google fit API, sorry, can't help more. 我没有使用Google Fit API进行任何项目,对不起,无法提供更多帮助。

I know that this is an old question, but I needed help on this too so i'll try to help: 我知道这是一个老问题,但是我也需要帮助,因此我将尽力帮助:

Your activity needs to implements this: 您的活动需要实现以下目的:

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

Create this variable before onCreate : public GoogleApiClient mGoogleApiClient = null; 在onCreate之前创建此变量: public GoogleApiClient mGoogleApiClient = null;

Inside onCreate write this : 在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();

Don't forget the OAuth authentication. 不要忘记OAuth身份验证。

Then you need this methods: 然后,您需要以下方法:

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) {

}

After this, you will create this method, read the step counter(Google Fit API count them) 之后,您将创建此方法,阅读步骤计数器(Google Fit API对它们进行计数)

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

This is the showDataSet that is inside displayStepDataForToday() 这是位于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)
            }
        }

    }

Finally you need to create a class (inside your activity) to use the displayStepDataForToday() method 最后,您需要创建一个类(在您的活动内部)以使用displayStepDataForToday()方法

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

You just need to start the activity when you want and then get the values of it. 您只需要在需要时启动活动,然后获取它的值即可。 This runs in background and updates as I want. 它在后台运行,并根据需要进行更新。 If you want this code to update faster you can do some research, but I think that you need to change this line: 如果您希望此代码更新得更快,可以进行一些研究,但是我认为您需要更改此行:

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

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

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