Got «lacks permission android.permission.BLUETOOTH» on Specific device after android 12 update
but when i tried to connect on my printer, i got this error that makes me pulls my hair out.
2022-03-21 09:23:49.039 29022-29206/com.xxxx.xxxxx E/BThermalPrinterPlugin: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH java.lang.SecurityException: UID 10324 / PID 29022 lacks permission android.permission.BLUETOOTH at android.os.Parcel.createExceptionOrNull(Parcel.java:2425) at android.os.Parcel.createException(Parcel.java:2409) at android.os.Parcel.readException(Parcel.java:2392) at android.os.Parcel.readException(Parcel.java:2334) at android.bluetooth.IBluetoothSocketManager$Stub$Proxy.connectSocket(IBluetoothSocketManager.java:227) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404) at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin.lambda$connect$1$id-kakzaki-blue_thermal_printer-BlueThermalPrinterPlugin(BlueThermalPrinterPlugin.java:544) at id.kakzaki.blue_thermal_printer.BlueThermalPrinterPlugin$$ExternalSyntheticLambda1.run(Unknown Source:6) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:920)
is this sort of ROM/platform/device specific problem. android.permission.BLUETOOTH supposed to be used on android 11 and below because android 12 and above replaces it with android.permission.BLUETOOTH_CONNECT, right? then why i got those specific device errors in the first place?
i have tried in Samsung A52 with android 12 and got no problem at all.
i have added necessary permission on manifest
and runtime permission request
var perm = arrayListOf( Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_PHONE_STATE, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN ) //permission if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < // >= android 12 perm.add(Manifest.permission.BLUETOOTH_CONNECT) perm.add(Manifest.permission.BLUETOOTH_SCAN) perm.add(Manifest.permission.BLUETOOTH_ADVERTISE) > if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
How to set permissions for Android Bluetooth
I’m new to Android development. I’m trying to get a simple HelloWorld app going on my (rooted) phone — and the app is trying to enable Bluetooth. I’ve set the Bluetooth permissions in my manifest is as follows, but I’m getting a Permission Denial exception when I try to run the application on my phone via Eclipse:
3 Answers 3
The answer about what to include in your manifest.xml for bluetooth activity includes
The first three are of greater priority and as I’m sure you’re aware there are different cases when each dependency may be required. Hope it helps with your setup!
I’m not quite sure what the problem was here.
All I can say is that I reinstalled Eclipse and its plugins and now everything is working fine. Thanks for your help Mayra — I’ll up-mark your answer because of your helpful and friendly approach.
Next time try to do «everything» logical and then reinstall, therefore your computer techniques will strengthen.
The element types in the manifest are ordered. I think the uses-permission needs to be first under the tag.
Thanks for the quick response Mayra. I’ve tried moving the uses-permission tag around in the file and it doesn’t seem to fix the problem — and the example manifest files I’ve seen often seem to include the uses-permission tags at the bottom of the file — so I don’t think that the location of the tag is critical.
Ok, I think you are right. I had a problem with permissions before that I thought was solved by order, but I see other places in the documentation where things are out of order. What is the exact error message that you are seeing?
Down voted, because in SO, we vote-up for quality of answer. Unfortunately, in spite of best intentions, the answer is incorrect.
New Bluetooth permissions in Android 12
In this article, I will discuss the new Bluetooth permissions introduced in Android 12.
New permissions
For apps targeting Android 12 or higher, the following three new permissions are introduced:
- BLUETOOTH_CONNECT: required to connect to paired Bluetooth devices.
- BLUETOOTH_SCAN: equired to scan and pair nearby Bluetooth devices.
- BLUETOOTH_ADVERTISE: required to advertise to nearby Bluetooth devices.
For apps that also support older Android versions, we can declare the permissions like this:
. > android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" /> android:name="android.permission.BLUETOOTH_CONNECT" /> android:name="android.permission.BLUETOOTH_SCAN" /> android:name="android.permission.BLUETOOTH_ADVERTISE" /> .
No location permission?
Really? No location permission required now? Well, yes and no.
If the app uses Bluetooth to derive physical location, e.g. through BLE beacons, you still need to declare the ACCESS_FINE_LOCATION permission as before.
However, if the app does not derive physical locations, you can add the android:usesPermissionFlags=»neverForLocation» attribute to the BLUETOOTH_SCAN permission declaration:
. > android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" /> .
Request user approval
Unfortunately, when you run the app on Android 12 devices, you will get the SecurityException for missing the needed permissions. That’s because the new permissions are runtime permissions that must be approved by the user.
Assume the app doesn’t need to advertise, and doesn’t derive physical locations, we can request the permission like this:
class MainActivity : Activity() companion object private const val BLUETOOTH_PERMISSION_REQUEST_CODE = 9999 > override fun onCreate(savedInstanceState: Bundle?) super.onCreate(savedInstanceState) initializeBluetoothOrRequestPermission() ... > private fun initializeBluetoothOrRequestPermission() val requiredPermissions = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) listOf(Manifest.permission.ACCESS_FINE_LOCATION) > else listOf(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN) > val missingPermissions = requiredPermissions.filter < permission -> checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED > if (missingPermissions.isEmpty()) initializeBluetooth() > else requestPermissions(missingPermissions.toTypedArray(), BLUETOOTH_PERMISSION_REQUEST_CODE) > > private fun initializeBluetooth() < ... > override fun onRequestPermissionsResult(requestCode: Int, permissions: Arrayout String>, grantResults: IntArray) when (requestCode) BLUETOOTH_PERMISSION_REQUEST_CODE -> if (grantResults.none < it != PackageManager.PERMISSION_GRANTED >) // all permissions are granted initializeBluetooth() > else // some permissions are not granted > > else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) > > ... >
Conclusion
This is all you need to use the new Bluetooth permissions for Android 12 and above, and it’s really a great improvement for no longer requiring the location permission for scanning.
Let me know how if you have any questions and happy coding!