3

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()
    }
LMB
  • 65
  • 2
  • 8
  • What have you tried? [Google documentation](https://developer.android.com/guide/topics/connectivity/bluetooth/permissions). – Klox Dec 01 '22 at 22:49
  • I edited the post , it's at this part of the code where I am getting the issue. – LMB Dec 03 '22 at 09:46

2 Answers2

2

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
        )
    }
}
Googlian
  • 6,077
  • 3
  • 38
  • 44
  • I had these already implemented but it still crashing. It shows me that the issue is on this part `if (bluetoothAdapter?.isDiscovering == true) { Logger.log(TAG, "cancel start discovery") bluetoothAdapter?.cancelDiscovery() } initBluetoothScanBroadcastReceiver() bluetoothAdapter?.startDiscovery()` – LMB Dec 03 '22 at 09:39
  • 1
    You can't check bluetoothAdapter without checking permission – Googlian Dec 03 '22 at 10:58
  • 1
    Request permission on top of private fun searchForPrinters() { – Googlian Dec 03 '22 at 10:58
  • Thanks, that was the problem, I didn't request permissions. – LMB Dec 04 '22 at 13:48
  • Welcome please upvote the answer if it's useful – Googlian Dec 04 '22 at 15:13
0

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:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72