如何使用Google Fit Sensors API测量跑步距离?

时间:2019-07-15 14:24:01

标签: android google-fit sensors

我想用Google Fit API测量跑步距离,将其存储在Google Fitness Store中,然后从那里获取距离数据。我以为我必须使用Sensors API来测量跑步距离?

我的代码基于Google的传感器API示例代码: https://github.com/googlesamples/androidfit/tree/master/BasicSensorsApi 他们的项目工作并调用了onDataPoint(),因此我可以看到从传感器测量的值。

onDataPoint()在我的项目中从未调用过,因此似乎传感器没有测量到任何东西。我该怎么做才能解决这个问题?

private void findFitnessDataSources() {
        // Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
        Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .findDataSources(
                        new DataSourcesRequest.Builder()
                                .setDataTypes(DataType.TYPE_DISTANCE_CUMULATIVE,
                                        DataType.TYPE_DISTANCE_DELTA)
                                .setDataSourceTypes(DataSource.TYPE_RAW, DataSource.TYPE_DERIVED)
                                .build())
                .addOnSuccessListener(
                        new OnSuccessListener<List<DataSource>>() {
                            @Override
                            public void onSuccess(List<DataSource> dataSources) {
                                for (DataSource dataSource : dataSources) {
                                    Log.i(TAG, "Data source found: " + dataSource.toString());
                                    Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
                                // Let's register a listener to receive Activity data!
                                if ((dataSource.getDataType().equals(DataType.TYPE_DISTANCE_CUMULATIVE) ||
                                        dataSource.getDataType().equals(DataType.TYPE_DISTANCE_DELTA)) && mListener == null)
                                {
                                    Log.i(TAG, "Data source  found!  Registering.");
                                    registerFitnessDataListener(dataSource, DataType.TYPE_DISTANCE_DELTA);
                                }
                            }
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e(TAG, "failed", e);
                        }
                    });
}


private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {
    mListener =
            new OnDataPointListener() {
                @Override
                public void onDataPoint(DataPoint dataPoint) {
                    for (Field field : dataPoint.getDataType().getFields()) {
                        Value val = dataPoint.getValue(field);
                        Log.i(TAG, "Detected DataPoint field: " + field.getName());
                        Log.i(TAG, "Detected DataPoint value: " + val);
                    }
                }
            };
    Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
            .add(
                    new SensorRequest.Builder()
                            .setDataSource(dataSource) // Optional but recommended for custom data sets.
                            .setDataType(dataType) // Can't be omitted.
                            .setSamplingRate(10, TimeUnit.SECONDS)
                            .build(),
                    mListener)
            .addOnCompleteListener(
                    new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Log.i(TAG, "Listener registered!");
                            } else {
                                Log.e(TAG, "Listener not registered.", task.getException());
                            }
                        }
                    });
}

0 个答案:

没有答案