Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
    }
like image 247
LMB Avatar asked Oct 23 '25 07:10

LMB


1 Answers

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
        )
    }
}
like image 177
Googlian Avatar answered Oct 24 '25 23:10

Googlian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!