简体   繁体   中英

Why [onDataPoint] of google fit not call?

I using this code to test google fit : android-fit . This code is working and not occur any exception , but it can't call into function [onDataPoint] when it register Fitness Data

    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());
                                }
                            }
                        });

    }
}

This is Result of Logcat outputed:

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!

Log display message [Listener registered!] but Why [onDataPoint] of google fit not call?

I had try remove .setDataSourceTypes(DataSource.TYPE_RAW) , but it still not go to [onDataPoint]

It will work if you change DataSource.TYPE_RAW to DataSource.TYPE_DERIVED And make sure that you are using the 5.0 and above. If you will use the version 4.4 you have to use DataSource.TYPE_ROW

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM