问题
I'm creating an app that tracks a user's steps using the google fit API. I've been stuck on getting the onDataPoint
method to be called more than once. My code is almost the exact copy of the google-fit sample code, except with location replaced by steps.
The sample code is found here. It's very similar to the code I am testing out with, with a small adjustment.
Here is my code. Any help would be greatly appreciated, I looked all over and couldn't find a solution.
private void findFitnessDataSources() {
// [START find_data_sources]
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_RAW)
.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());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)
&& mListener == null) {
Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_STEP_COUNT_DELTA);
}
}
}
});
}
private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {
// [START register_data_listener]
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.SensorsApi.add(
mClient,
new SensorRequest.Builder()
.setDataSource(dataSource) // Optional but recommended for custom data sets.
.setDataType(dataType) // Can't be omitted.
.setSamplingRate(1, TimeUnit.SECONDS)
.build(),
mListener)
.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.");
}
}
});
}
回答1:
Issue was solved in a later SO question and my suspicion was correct. The issue is that TYPE_STEP_COUNT_DELTA
is not a raw data type—Google uses a combination of sensors, accelerometer, machine learning, etc. to determine step count. So the .setDataSourceTypes(DataSource.TYPE_RAW)
line needs to be removed.
In fact, the OP of the other question noticed that just removing the line gives less accurate results than replacing it with DataSource.TYPE_DERIVED
, so that is advised.
回答2:
As i mentioned in my post that step_count of google fit uses derived datatype for everything whether It is cumulative,delta etc. I observed the results of my app for 10 iteration. I found that
- Google fit API which is provided by google only uses derived datatype for all step_count.
- You can also not specify the datatype ,but it can give variation in result when compared with Google Fit app.
The same thing is mentioned in Google Fit faq section. [https://developers.google.com/fit/faq
来源:https://stackoverflow.com/questions/38799685/google-fitness-sensors-ondatapoint-not-being-called