Amulya Maheshwari
Amulya Maheshwari

Reputation: 61

BLE Bluetooth scanning is not working on Android 10 & 11 due to location permission

I am working on BLE Bluetooth scanning is working on all devices except the Android 10 & 11. After updating the application, Bluetooth scanning in Android 10 & 11 are not working. Sometimes even after the location permission is allowed, the application has to re-grant permission from the application settings. Why is this not being known while always getting true in the

if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){ //Code here.. }

Upvotes: 3

Views: 5788

Answers (3)

Marcnu
Marcnu

Reputation: 21

On recent Android versions (I think it's for Android 12 and above but I'm not certain), you need to not only have ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION but you also need the Location feature of the phone (GPS Mode) enabled.

You may also want to check if you need to provide ScanSettings for Low Latency scan (new feature of Android 8 aka API 26).

Here is how to open the Location activation menu if it is disabled:

try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Android 12 (API 31) or higher
        val locationManager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
        val isLocationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

        if (!isLocationEnabled) {
            // Request the user to enable location services
            val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
            requireContext().startActivity(intent)
            return@setOnClickListener
        }
    }
    /*val scanSettings = ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // Use low latency for faster scans
        .build()
    bluetoothAdapter.bluetoothLeScanner.startScan(null, scanSettings, leScanCallback)*/
    bluetoothAdapter.bluetoothLeScanner.startScan(leScanCallback)
} catch (e: SecurityException) {
    Log.e("Bluetooth", "Could not start Bluetooth scan", e)
    return@setOnClickListener
}

Upvotes: 0

Radhika bajaj
Radhika bajaj

Reputation: 155

Please verify that you have implemented the location permission check according to the os version

  1. Android 8 (≥8): background apps can only retrieve a user’s location a few times each hour.

  2. Before Android 10(<10), location permission was a single resource: apps only required permission once to use everywhere (foreground and background) and every time.

  3. Android 10(≥10), background location came as an independent resource. Apps have to request explicitly this permission besides the foreground one.

  4. Android 11(≥11), background location permission can’t be requested at the same time as others. Apps have to request it separately. Moreover, requesting this permission doesn’t prompt the user immediately as for the other permissions but it will take the user to the /Settings page/Location permission session so that the user can update the level of permission.

Note : sometimes when the application is installed in work mode profile we have to manually enable the permission from setting even we are allowing it from the application

Upvotes: 2

Michael Kotzjan
Michael Kotzjan

Reputation: 2663

Try to actually request the permission from the user on start of your app using requestPermissions:

// Request location permission, needed for BLE Scan
ActivityCompat.requestPermissions(this,
    new String[]{
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION},
    2);

You can find more information on how to handle this event better on this page of the docs.

Upvotes: 1

Related Questions