问题
I have some code that has worked for months in production which suddenly has stopped working yesterday in all my apps. I use Google Fit to retrieve locations via the Fitness.SensorClient API. Locations permissions are requested correctly to the user at installation time (both the generic Android FINE_LOCATION permission and the permission to read and store locations into Google Fit). I create the Fitness Option in this way:
setFitnessOptions(
FitnessOptions.builder()
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_WRITE));
I then look for the DataSources in this way:
GoogleSignInAccount lastSignedAccount = GoogleSignIn.getAccountForExtension(context, getFitnessOptions());
if (lastSignedAccount != null) {
Fitness.getSensorsClient(context, lastSignedAccount)
.findDataSources(
new DataSourcesRequest.Builder()
.setDataTypes(DataType.TYPE_LOCATION_SAMPLE)
.setDataSourceTypes(DataSource.TYPE_RAW)
.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());
...
The value of the parameter dataSources in OnSuccessListener is an empty list. If I try other data types .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA), dataSources is not an empty list. However neither DataType.TYPE_LOCATION_SAMPLE nor DataType.TYPE_LOCATION_TRACK return any data source.
I have checked the release notes of google services and nothing relevant seems to have changed. The code above seems to be equivalent to all the examples provided by Google, e.g. this one
Does anyone have an idea of why suddenly that code has stopped working? Thanks
UPDATE: I have verified that it does not work with Android 6, 7, 8, or 9.
回答1:
I have found the solution. In another part of my code I was subscribing using GoogleSignIn.getLastSignedInAccount. Apparently this may cause a mismatch and the locations are not returned. Changing the subscription to GoogleSignIn.getAccountForExtension solved the issue
来源:https://stackoverflow.com/questions/57833033/android-google-fit-not-finding-data-sources-for-datatype-type-location-sample