为什么 google 的 [onDataPoint] 适合不调用?

Why [onDataPoint] of google fit not call?

我使用此代码来测试 google 适合:android-fit。 此代码有效,未发生任何异常,但在注册健身数据时无法调用函数 [onDataPoint]

    private OnDataPointListener mListener;
    ]
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);          
        setContentView(R.layout.activity_main);         

        if (hasRuntimePermissions()) {
            findFitnessDataSourcesWrapper();
        } else {
            requestRuntimePermissions();
        }
    }


    private void findFitnessDataSourcesWrapper() {
        if (hasOAuthPermission()) {
            findFitnessDataSources();
        } else {
            requestOAuthPermission();
        }
    }


    private FitnessOptions getFitnessSignInOptions() {
        return FitnessOptions.builder().addDataType(DataType.TYPE_LOCATION_SAMPLE).build();
    }


    private boolean hasOAuthPermission() {
        FitnessOptions fitnessOptions = getFitnessSignInOptions();
        return GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
    }


    private void requestOAuthPermission() {
        FitnessOptions fitnessOptions = getFitnessSignInOptions();
        GoogleSignIn.requestPermissions(
                this,
                REQUEST_OAUTH_REQUEST_CODE,
                GoogleSignIn.getLastSignedInAccount(this),
                fitnessOptions);
    }


    @Override
    protected void onResume() {
        super.onResume();


        findFitnessDataSourcesWrapper();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_OAUTH_REQUEST_CODE) {
                findFitnessDataSources();
            }
        }
    }

    private void findFitnessDataSources() {

        Fitness.getSensorsClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .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());

                                    if (dataSource.getDataType().equals(DataType.TYPE_LOCATION_SAMPLE)
                                            && mListener == null) {
                                        Log.i(TAG, "Data source for LOCATION_SAMPLE found!  Registering.");
                                        registerFitnessDataListener(dataSource, DataType.TYPE_LOCATION_SAMPLE);
                                    }
                                }
                            }
                        })
                .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) {
                        Log.i(TAG, "onDataPoint");
                        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) 
                                .setDataType(dataType) 
                                .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());
                                }
                            }
                        });

    }
}

这是 Logcat 输出的结果:

02-28 18:36:41.973 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Data source found: DataSource{raw:Application{com.google.android.gms::null}:Device{SHARP:SH-08E:daa952d0::1:2}:live_location:DataType{com.google.location.sample[latitude(f), longitude(f), accuracy(f), altitude(f)]}}
02-28 18:36:41.973 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Data Source type: com.google.location.sample
02-28 18:36:41.973 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Data source for LOCATION_SAMPLE found!  Registering.
02-28 18:36:42.013 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Data source found: DataSource{raw:Application{com.google.android.gms::null}:Device{SHARP:SH-08E:daa952d0::1:2}:live_location:DataType{com.google.location.sample[latitude(f), longitude(f), accuracy(f), altitude(f)]}}
02-28 18:36:42.013 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Data Source type: com.google.location.sample
02-28 18:36:42.243 22519-22519/vnit.com.testrealm I/BasicSensorsApi: Listener registered!

日志显示消息 [Listener registered!] 但为什么 google 的 [onDataPoint] 不适合调用?

我曾尝试删除 .setDataSourceTypes(DataSource.TYPE_RAW),但它仍然没有转到 [onDataPoint]

DataSource.TYPE_RAW 更改为 DataSource.TYPE_DERIVED 即可 并确保您使用的是 5.0 及以上版本。如果您要使用 4.4 版,则必须使用 DataSource.TYPE_ROW