I'm interested in trying out the Google Fit API on Android to get some data from sensors, but I can't test it out due to the OAuth2 screen loading infinitely.
What I've done:
- Enabled the Google Fit API on the automatic Firebase-generated Google Cloud project that is related to the app.
- Have properly set up the app name, SHA1 fingerprint hash, and package name.
- Have set up the OAuth2 screen with my email being one of the testers and appropriate scopes added.
- Enabled the Google Sign-In method on the Firebase console, added both my SHA1 and SHA256 fingerprints, ensured my support email was selected in the Firebase project settings and replaced the google-services.json file in my project.
This is the code where I'm trying to get the user's permission for their Fitness data:
GoogleSignInOptionsExtension fitnessOptions =
FitnessOptions.builder()
.addDataType(DataType.TYPE_HEART_RATE_BPM, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_SLEEP_SEGMENT, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_READ)
.build();
GoogleSignInAccount googleSignInAccount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
permissionUtil = new PermissionUtil(requireContext());
permissionUtil.requestFitPermissions();
googleSignInAccount = GoogleSignIn.getAccountForExtension(requireContext(), fitnessOptions);
if (!GoogleSignIn.hasPermissions(googleSignInAccount, fitnessOptions)) {
GoogleSignIn.requestPermissions(Objects.requireNonNull(getActivity()),
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
googleSignInAccount,
fitnessOptions);
} else {
accessGoogleFit();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_moods, container, false);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE && resultCode == RESULT_OK) {
accessGoogleFit();
}
}
private void accessGoogleFit() {
Fitness.getSensorsClient(requireContext(), GoogleSignIn.getAccountForExtension(requireContext(), fitnessOptions))
.findDataSources(
new DataSourcesRequest.Builder()
.setDataTypes(DataType.TYPE_HEART_RATE_BPM,
DataType.TYPE_STEP_COUNT_DELTA,
DataType.TYPE_SLEEP_SEGMENT,
DataType.TYPE_ACTIVITY_SEGMENT,
DataType.TYPE_LOCATION_SAMPLE)
.setDataSourceTypes(DataSource.TYPE_RAW)
.build())
.addOnSuccessListener(new OnSuccessListener<List<DataSource>>() {
@Override
public void onSuccess(List<DataSource> dataSources) {
for (DataSource i : dataSources) {
Log.d(TAG, i.getStreamIdentifier());
Log.d(TAG, i.getDataType().getName());
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Find data sources request failed", e);
}
});
}
It's basically the same issue that this thread is talking about.
Any help is appreciated.