react-native-google-fit: sleep syncing return "Failure: 4" caused by mixing deprecated code with not deprecated code
### Sync sleep data error
after react-native-google-fit got support for sleep hours, we started facing intermittent issues that prevent customers to read and sync sleep records, the customer is only able to read and sync steps records. It also caused other issue related to disconnect functionality., after some research, testing, and debugging we realized that the issue was caused because at the library level we are mixing deprecated code(steps) with new code (sleep), apparently, the deprecated implementation do not request for all the required permissions to read sleep records.
looking at android developers tools, we found a specific error that mentions that we do not have enough permission to read sleep records
Failure: 4: The user must be signed in to make this API call
that error message was also returned on disconnect call after we did exhaustive research about those errors, the posts mention some issues related to the cert key was not added correctly to google cloud, but apparently our configuration was right.
the way to fix this was by creating a function into SleepHistory.java to fix that state when a customer is partially connected(able to read steps records, but not able to read sleep records), that function was placed in to addOnFailureListener of Fitness.getSessionsClient(this.mReactContext, gsa), and the function looks like this
`
private void fixDisconnectedState(int requestCode) {
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_READ)
.build();
// Get currently signed in account (or null)
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(SleepHistory.this.mReactContext);
if (!GoogleSignIn.hasPermissions(account, fitnessOptions)) {
// Note: this launches a sign-in flow, however the code to detect
// the result of the sign-in flow and retry the API call is not
// shown here.
GoogleSignIn.requestPermissions(this.mReactContext.getCurrentActivity(), requestCode, account, fitnessOptions);
return;
}
}
`
we know this probably could be improved.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (1 by maintainers)
@davidguerrerolopez since your solution includes permission check, I believe the problem comes from the missing permission and I also checked the official doc. Not sure if this is the problem but I wish you can try this out in .js instead of native side, it should just do the same trick, if the problem is still reproducible
I assume you do authentication status check before calling googlefit function. This check can be added into it as well and promote the permission panel as well right after user login. I know it’s a little verbose, request in native side can run silently but complete dismiss the user right. I’m still not sure where to put for part of code like this (native side or js side) .