0

Android app giving:

java.lang.SecurityException: UID 10457 / PID 24525 lacks permission android.permission.BLUETOOTH

When I try to connect with Bluetooth printer using BluetoothDevice().connect() in Xiaomi android 12

I am trying to connect with Bluetooth thermal but am not able to do it in android 12

Sweta Jain
  • 3,248
  • 6
  • 30
  • 50

1 Answers1

0

First you have to add user-permission.

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

And then your codes, you have to check if the user is using Android 12. If yes, check the permission Bluetooth. And then you can able to use the Bluetooth.

Example here I use library from here. https://github.com/Karumi/Dexter

private void checkPermissionBluetooth() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        final ArrayList<String> listPermission = new ArrayList<>();
        listPermission.add(Manifest.permission.BLUETOOTH_CONNECT);
        listPermission.add(Manifest.permission.BLUETOOTH_SCAN);

        Dexter.withContext(Kpp02NewActivity.this).withPermissions(listPermission).withListener(new MultiplePermissionsListener() {
            @Override
            public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
                threadBluetoothEnable();
            }

            @Override
            public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {

            }
        }).check();
    } else {
        threadBluetoothEnable();
    }
}
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46