Problem with ACCESS_COARSE_LOCATION android permission

Viewed 195

Hi I have a problem with ACCESS_COARSE_LOCATION permission in an android application. I have added the permission to the manifest:

    <!-- Required for Nearby Connections API -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

and have requested the permission in my onStart() method:

    override fun onStart() {
    super.onStart()
    requestPermissions(arrayOf(
        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
        REQUEST_CODE_PERMISSIONS)
}

and checked if it is granted:

        if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
    ) {

        mConnectionsClient.startDiscovery(
            SERVICE_ID,
            mEndpointDiscoveryCallback,
            DiscoveryOptions.Builder().setStrategy(Strategy.P2P_STAR).build()
        )
            .addOnSuccessListener {
                debug("Success startDiscovery: $it")
            }
            .addOnFailureListener {
                debug("Failure startDiscovery: $it")
            }
    }

also when I check if the permission is granted, it says that it is but it stills shows error:

Failure startDiscovery: com.google.android.gms.common.api.ApiException: 8034: MISSING_PERMISSION_ACCESS_COARSE_LOCATION

Does anyone have any idea what the problem might be?

1 Answers

Make sure that your test device does not have "Location" turned off globally in settings.

Even if you request and subsequently check to positively make sure that you have been granted all the correct permissions to start discovery actually calling

Nearby.getConnectionsClient(...).startDiscovery(...)

will still return

com.google.android.gms.common.api.ApiException: 8034: MISSING_PERMISSION_ACCESS_COARSE_LOCATION

Related