Search code examples
androidgoogle-fit

How is the the correct way to request permission to query Google Fit sleep data on Android?


This is how I'm requesting permission to query step data:

if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACTIVITY_RECOGNITION)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
    String[] permissions = {Manifest.permission.ACTIVITY_RECOGNITION};
    requestPermissions(permissions, ACTIVITY_RECOGNITION_REQUEST_CODE);
} else {
    GoogleSignIn.requestPermissions(this, GOOGLE_FITNESS_SCOPE_REQUEST_CODE, acct,
            Fitness.SCOPE_ACTIVITY_READ);
}

And this works just fine for that. But as soon as I attempt to query a user's sleep data, I'm hit with this exception:

com.google.android.gms.common.api.ApiException: 4: The user must be signed in to make this API call.

I simply cannot figure out how to grab the sleep data correctly and I'm assuming it's to do with the permissions. But it definitely could be something else I think.


Solution

  • The problem was I wasn't successfully obtaining permission for accessing sleep data. Once that was fixed, this problem went away.