LMB
LMB

Reputation: 75

Issue with Need android.permission.BLUETOOTH_SCAN permission for AttributionSource

I am using a printer d11 and trying to connect it to my device using Bluetooth. But I am encountering this error: Need android. permission.BLUETOOTH_SCAN permission for AttributionSource.

I have already added the permissions and all but I'm still having errors, it points me out that the problem is here.

@SuppressLint("MissingPermission")
private fun searchForPrinters() {
        val bluetoothManager =
            requireActivity().getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        bluetoothAdapter = bluetoothManager.adapter
        if (bluetoothAdapter == null) {
            AlertsUtil.showBaseOkWarningDialog(
                requireActivity(),
                getString(R.string.bluetooth_not_available),
                ""
            )
            return
        } else if (!bluetoothAdapter!!.isEnabled) {
            askToEnableBluetooth()
            return
        }

        // The line that causes the problem
        if (bluetoothAdapter?.isDiscovering == true) {
            Logger.log(TAG, "cancel start discovery")
            bluetoothAdapter?.cancelDiscovery()
        }

        initBluetoothScanBroadcastReceiver()
        bluetoothAdapter?.startDiscovery()
    }

Upvotes: 3

Views: 9307

Answers (2)

Googlian
Googlian

Reputation: 6703

Add the below permissions on the Manifest.xml file

<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" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

Checking whether permissions are granted by the user

private fun isPermissionsGranted(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
    } else {
        ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    }
}

Request user permission to proceed with Bluetooth Scan.

if (!isPermissionsGranted(activity)) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        val permissions = mutableSetOf(
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
        )

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            permissions.add(Manifest.permission.BLUETOOTH_CONNECT)
            permissions.add(Manifest.permission.BLUETOOTH_SCAN)
        }

        ActivityCompat.requestPermissions(
            activity, permissions.toTypedArray(), 600
        )
    }
}

Upvotes: 3

Youssif Saeed
Youssif Saeed

Reputation: 13285

In order to use Bluetooth properly on your Android device, the following permissions need to be added to your manifest file:-

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

Then these permissions should be requested at runtime as can be seen in the links below:-

Upvotes: 0

Related Questions