'Finding Error in android studio java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT

I am new to Android studio. I am trying to connect to brother label printer QL-820NMB with the new Android APK (SDK 31) with android version 12 and the device is also android 12 itself. I tried pairing the device it gets paired. But when I tried to access the paired devices in the code finding the error in the line

Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getBondedDevices();

And Exception is...

System.err: java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 0000, packageName = com.xyz.xyz, attributionTag = null, token = android.os.xyz, next = null }: AdapterService getBondedDevices

I have provided all the manifest for permissions .

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

and also updated the mobile updates recommended and also updated the BrotherPrintLibrary jar to aar as recommended file.

But still unable to access the Bluetooth and getting the same Security Exception as above is there any solution or can I know am I missing any configuration issue in the mobile or the code.

I forgot to mention the same APK is working fine in Android 7 device but not in Android 12.



Solution 1:[1]

Seems, you missed BLUETOOTH_ADMIN Permission.

https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashok Mutyala