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()
}