我使用此代码来测试google fit: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!


日志显示消息[已注册侦听器!]但是为什么Google fit的[onDataPoint]无法调用?

我尝试删除.setDataSourceTypes(DataSource.TYPE_RAW),但仍然无法转到[onDataPoint]

最佳答案

如果将DataSource.TYPE_RAW更改为DataSource.TYPE_DERIVED,它将起作用
并确保您使用的是5.0及更高版本。如果要使用版本4.4,则必须使用DataSource.TYPE_ROW

关于android - 为什么Google Fit的[onDataPoint]无法调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49029004/

10-10 16:02