Android: Google Fit not finding data sources for DataType.TYPE_LOCATION_SAMPLE

邮差的信 提交于 2019-12-30 10:36:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!