问题
I have been reading about Google Fit API, specially the Sensors API (https://developers.google.com/fit/android/sensors), which says : The Sensors API provides access to raw sensor data streams from sensors available on the Android device and from sensors available in companion devices, such as wearables.
Also, watching Google's example video, they says "They could be sensors available on the Android Device or sensors available on companion devices. For example, for steps, Fit will use the steps counter on the phone or any connected wearable, whichever it thinks it's most accurate".
Reading this I understand that Fit API will read from the best sensor available, but using their example, with a wearable device connected, I only get one datasource to read steps from: the Datasource from my Android Phone.
I add the code:
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);
}
}
}
});
Here I only get the Datasource from my Android device, not from both my phone and my wearable (Moto 360 2gen.)
Have you guys got any clues about why I'm not getting my wearable Datasource in the result?.
Thanks.
回答1:
The SensorsApi.findDataSources method gets raw and derived sensors from the device that runs the Activity(In your case, it's the mobile device). If you want to use your wearable device as a data source, then you need first connect to it via Bluetooth. Try using the Bluetooth Scan API! Hope this helps.
来源:https://stackoverflow.com/questions/36107415/android-fitness-api-not-reading-data-from-wearable-sensors