Sameeksha Jain
Sameeksha Jain

Reputation: 39

BLE scan callback not invoked on missing permission

I am using Samsung Note 8 (Android 9) and Samsung A50(Android 10).

I am doing BLE scan which needs Bluetooth as well as location permissions (Android >=23). I am NOT providing the Location permission to the App before starting the BLE scan.

While starting a scan on Note 8, I am getting onScanFailed() callback with error code 2 (SCAN_FAILED_APPLICATION_REGISTRATION_FAILED) when the location permission is not given.

While starting scan on A50, I do not get any callbacks. From the adb logs I can see internal log like BluetoothUtils: Permission denial: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATIONpermission to get scan results 07-28 21:03:35.720 2010 2650 D

I want to identify a scan failure has occurred because of missing permission. How do i do that?

Note- I am building an Android library, not an Android App so please suggest accordingly.

Upvotes: 1

Views: 756

Answers (2)

Nirav Panchal
Nirav Panchal

Reputation: 1

package com.example.bleblue.common

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

object permissionHandler {

    private val requiredPermissions: Array<String>
        get() {
            return when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> arrayOf(
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_ADVERTISE,
                    Manifest.permission.BLUETOOTH_CONNECT,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.POST_NOTIFICATIONS
                )
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> arrayOf(
                    Manifest.permission.BLUETOOTH_SCAN,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.BLUETOOTH_ADVERTISE,
                    Manifest.permission.BLUETOOTH_CONNECT,
                    Manifest.permission.ACCESS_FINE_LOCATION
                )
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> arrayOf(
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
                )
                else -> arrayOf(
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
                )
            }
        }

    fun hasPermissions(context: Context): Boolean {
        return requiredPermissions.all {
            ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
        }
    }

    fun requestPermissions(activity: AppCompatActivity) {
        val permissionsToRequest = requiredPermissions.filter {
            ContextCompat.checkSelfPermission(activity, it) != PackageManager.PERMISSION_GRANTED
        }.toTypedArray()

        if (permissionsToRequest.isNotEmpty()) {
            activity.requestPermissions(permissionsToRequest, REQUEST_CODE_REQUIRED_PERMISSIONS)
        }
    }

    const val REQUEST_CODE_REQUIRED_PERMISSIONS = 1
}

Upvotes: 0

Thomas Morris
Thomas Morris

Reputation: 822

There are a few things it could be. Firstly note for a BLE scan you need permission to location and bluetooth. Therefore, in your developing environment you will need to specify this. For example in android studio you need to go into the manifest and specifically give permission to both bluetooth and location. See below:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

However, this will not be enough as within your phone app you will need to give the permission from the user.

As you are running android you will need to give the location permission(and turn on location and bluetooth) then the scan will work.

To allow location go to:

  • Settings
  • Your apps
  • Find your app
  • App info
  • Permissions
  • Allow location permission

To summarise check your IDE has the permissions enabled. Then check on the app(on the phone) that the user has allowed the permission.

Upvotes: 1

Related Questions