0

I need an example java code of Bluetooth permissions. Android 12.

I know that I have to put this in the AndroidManifest.xml

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

From my research I think I have to use these tools to get my phone to ask me for Bluetooth permissions:

ActivityCompat
ContextCompat
PackageManager
onRequestPermissionsResult

But no luck after many hours of tests. I even tried using libraries from GitHub to ask for "easy" permissions for me but no luck:

https://github.com/googlesamples/easypermissions https://github.com/guolindev/PermissionX

I tried lowering my target SDK version so I don't need to ask for the user entry but "AndroidX" gave me an error telling that its not compatible with lower SDK's.

It would help me a ton if you have a basic working Bluetooth app java code so I can figure out how to code my own app.


Ken Bow
  • 21
  • 2

2 Answers2

0

For the new Bluetooth permissions on Android 12, you need these permissions:

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

You can refer to this blog post for more details.

xizzhu
  • 895
  • 6
  • 9
0

For bluetooth permission on Android 12, you need these permission, depend on your usage:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

In your activity, you can add this to check the permission is allowed or not:

private static final int ACCESS_COARSE_LOCATION_RESULT_CODE = 4;
    private static final int BLUETOOTH_RESULT_CODE = 5;
    private static final int DANGEROUS_RESULT_CODE = 1;

if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        ACCESS_COARSE_LOCATION_RESULT_CODE);
            }
            else if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.BLUETOOTH},
                        BLUETOOTH_RESULT_CODE);
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_SCAN},
                            DANGEROUS_RESULT_CODE);
                }
            }
            else if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.BLUETOOTH_CONNECT},
                            DANGEROUS_RESULT_CODE);
                }
            }

To understand more, you can check the documentation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
akimPeaceYo
  • 3
  • 1
  • 1
  • 4