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

时间:2014-12-12 05:41:13

标签: java android google-maps google-fit

目前使用Google Fit API并在使用Sensors API时遇到一些问题。我正在尝试获取用户当前的应用程序锻炼速度,但文档有点令人困惑。

在此代码段中是来自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.");
                    }
                }
            });

//添加监听器

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

                }
            });


        }

目前,我正在获取所有其他数据类型值,如距离,心率,步数和当前活动,但无法获得用户当前的速度。 我做得对吗?

2 个答案:

答案 0 :(得分:3)

您可以尝试使用Google Fit Github存储库中的basichistorysessions sample

示例代码:

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

有关详细信息,请参阅此reading fitness data using sessions部分。

答案 1 :(得分:0)

要获得速度,应该派生dataSourceType。以下适用于我

<div class="line-chart-js" data-x='xkey' data-y='ykey' data-label='label' data-chart='<?php echo json_encode($yourArray); ?>'></div>