简体   繁体   English

如何使用Google Fit的API获取用户当前的速度?

[英]How to get the user's current speed using Google Fit's API?

Working with the Google Fit API at the moment and having a bit of trouble with the Sensors API. 目前使用Google Fit API并且在使用Sensors API时遇到一些问题。 I'm trying to get user's current speed for my app's workouts but the documentation is a bit confusing. 我正在尝试获取用户当前的应用程序锻炼速度,但文档有点令人困惑。

In this code snippet is an example from Google's info page: 在此代码段中是Google信息页面中的示例:

Fitness.SensorsApi.add(
            mClient,
            new SensorRequest.Builder()
                    // Optional but recommended for custom data sets.
                    .setDataType(DataType.TYPE_SPEED)// Can't be omitted.
                    .setSamplingRate(1, TimeUnit.SECONDS).setAccuracyMode(SensorRequest.ACCURACY_MODE_HIGH)
                    .build(), mListener3)
            .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.");
                    }
                }
            });

//Adding a Listener //添加监听器

mListener3 = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {


           final float speed = dataPoint.getValue(Field.FIELD_SPEED).asFloat();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Log.i(TAG, "In Speed" + speed );
                    speedTxtView.setText("" + speed );

                }
            });


        }

Currently, I am getting all other datatype values like distance, heart rate ,step count and current activity but unable to get user's current speed. 目前,我正在获取所有其他数据类型值,如距离,心率,步数和当前活动,但无法获得用户当前的速度。 Is i am doing correctly? 我做得对吗?

You could try the basichistorysessions sample from Google Fit Github repository. 您可以尝试使用Google Fit Github存储库中的basichistorysessions示例

sample code: 示例代码:

  // Build a session read request
        SessionReadRequest readRequest = new SessionReadRequest.Builder()
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .read(DataType.TYPE_SPEED)
                .setSessionName(SAMPLE_SESSION_NAME)
                .build();


// Invoke the Sessions API to fetch the session with the query and wait for the result
                    // of the read request.
                    SessionReadResult sessionReadResult =
                            Fitness.SessionsApi.readSession(mClient, readRequest)
                                    .await(1, TimeUnit.MINUTES);

                    // Get a list of the sessions that match the criteria to check the result.
                    Log.i(TAG, "Session read was successful. Number of returned sessions is: "
                            + sessionReadResult.getSessions().size());
                    for (Session session : sessionReadResult.getSessions()) {
                        // Process the session
                        dumpSession(session);

                        // Process the data sets for this session
                        List<DataSet> dataSets = sessionReadResult.getDataSet(session);
                        for (DataSet dataSet : dataSets) {
                            dumpDataSet(dataSet);
                        }
                    }

You can refer to this reading fitness data using sessions section for more information. 您可以使用会话部分参考此阅读健身数据以获取更多信息。

To get the speed, dataSourceType should be derived. 为了获得速度,应该派生dataSourceType。 The following works for me 以下适用于我

        Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_SPEED)
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .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());
                    }
                }
            });

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

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