Android bluetooth admin permissions

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:

  1. BLUETOOTH_CONNECT: required to connect to paired Bluetooth devices.
  2. BLUETOOTH_SCAN: equired to scan and pair nearby Bluetooth devices.
  3. 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!

Источник

Android 12 New Bluetooth Permissions

Bluetooth is the main dependency of our app. So, We already try to implement new Android 12 Bluetooth permissions. Our only resource is Android developers New Bluetooth permissions in Android 12. There is just saying add permissions

I add and I got runtime permissions for both and of course location(usual as pre 12)).
There is no other change in my codebase. Should be? I don’t know. So, the problem is my app can’t find the BLE device. I couldn’t find the reason. Do you have any suggestions or resources?

i am trying to add these permissions too but they dont exist in android studio. i only see the old ones (BLUETOOTH, BLUETOOTH_ADMIN, BLUETOOTH_ADMIN_PRIVILEGED). i set my targetSdkVersion to «S», my compileSdkVersion to «android-S», and my buildToolsVersion to «3.0.0-rc5». any idea?

I can see. My settings like this: compileSdkVersion «android-S» buildToolsVersion «30.0.3» targetSdkVersion 31

12 Answers 12

100% working solution : no need any 3rd party plugin

Kotlin code: //check android12+

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < requestMultiplePermissions.launch(arrayOf( Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT)) >else < val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) requestBluetooth.launch(enableBtIntent) >. private var requestBluetooth = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) < result ->if (result.resultCode == RESULT_OK) < //granted >else < //deny >> private val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) < permissions ->permissions.entries.forEach < Log.d("test006", "$= $") > > 

enter image description here

Not totally working for me, I’m on Android 10. I don’t see any popup asking for permissions, I immediately get to the part where the permissions are granted.

The application gets crashed if you Don’t allow permission for the first time. After that not able to get the permission dialog and got crashed every time.

In the manifest, add the following permissions:

Then, before executing a Bluetooth function, check the permission:

//--------------------------Java-------------------------- if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_DENIED) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < ActivityCompat.requestPermissions(MainActivity.this, new String[], 2); return; > > //--------------------------Kotlin-------------------------- if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_DENIED) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.BLUETOOTH_CONNECT), 2) return >> 
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_DENIED) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < ActivityCompat.requestPermissions(MainActivity.this, new String[], 2); return; > > mBTSocket.connect(); 

I just added to the manifest:

and then I requested those permissions from Main Activity as any other. For requesting permission I am using library

implementation 'pub.devrel:easypermissions:3.0.0' 

then you can just call this function

public static final String[] BLUETOOTH_PERMISSIONS_S = < Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT>; 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) < if (!EasyPermissions.hasPermissions(this, BLUETOOTH_PERMISSIONS_S)) < EasyPermissions.requestPermissions(this, message, yourRequestCode,BLUETOOTH_PERMISSIONS_S); >> 

and override onRequestPermissionResult

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)

UPDATE FOR JETPACK COMPOSE

If you are using jetpack compose you can handle it like this:

Create a list of your permissions inside of rememberMultiplePermissionState function

rememberMultiplePermissionsState( permissions = listOf( Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN ) ) 

Then observe a lifecycle events and on resume launch permission request

val lifecycleOwner = LocalLifecycleOwner.current DisposableEffect(key1 = lifecycleOwner, effect = < val observer = LifecycleEventObserver < _, event ->if(event == Lifecycle.Event.ON_START) < permissionsState.launchMultiplePermissionRequest() >> lifecycleOwner.lifecycle.addObserver(observer) onDispose < lifecycleOwner.lifecycle.removeObserver(observer) >>) 

Observe the permission state

permissionsState.permissions.forEach < permissionState ->when(permissionState.permission) < Manifest.permission.ACCESS_FINE_LOCATION -> < when < permissionState.hasPermission -><> > > > > > 

No particular reason, we were using in the old java project. Since we migrated to kotlin and compose we stopped using it.

It happens with Android 12 and Android 13. Adding new permissions could not solve the issue in my case: I have all related permissions set up for bluetooth and WiFi:

enter image description here

Manual solution(Screenshot added): Go to settings of the app and click on permissions. You will see Allowd and Denied(Not allowed) permissions. There will be «Nearby devices» permission in the «Not allowed» permissions list. Allow that one and the app will work without probem in Android 12 and Android 13.

If you want your app to initiate device discovery or manipulate Bluetooth settings, you must declare the BLUETOOTH_ADMIN permission in addition to the BLUETOOTH permission. Most apps need this permission solely for the ability to discover local Bluetooth devices. Don’t use the other abilities granted by this permission unless the app is a «power manager» that modifies Bluetooth settings upon user request. Declare the Bluetooth permission(s) in your app manifest file

from developer android we see you have to add

in your manifest file but you did not add it to discover other devices i think this is the resource of your problem

Thanks for your answer Barney and sorry for didn’t mention it before but BLUETOOTH_ADMIN is already added. It is not working with that also.

I had an error with BLUETOOTH_ADVERTISING missing in the android manifest when switching to Android 12

Basically, I just added checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED in my conditions where startAdvertising was called.

In android 12, after adding below line is working fine for me.

On Android 13, I followed the top voted answer and it fixed my «Bluetooth Scan» request issue. However, I was getting a new error for «Bluetooth Connect». After spending some time I was able to figure out that in Android 13, they made Bluetooth Connect a runtime request, so just having it in the manifest file isn’t enough. I had to overwrite my OnCreate in my MainActivity.cs file, so that it could get the permission at runtime.

private const int REQUEST_FINE_LOCATION_PERMISSION = 100; private const int REQUEST_BLUETOOTH_SCAN_PERMISSION = 101; private const int REQUEST_BACKGROUND_LOCATION_PERMISSION = 102; private const int REQUEST_BLUETOOTH_CONNECT_PERMISSION= 103; protected override void OnCreate(Bundle savedInstanceState) < base.OnCreate(savedInstanceState); RequestedOrientation = ScreenOrientation.Portrait; // Request the ACCESS_FINE_LOCATION permission at runtime if (CheckSelfPermission(Manifest.Permission.AccessFineLocation) != Permission.Granted) < RequestPermissions(new string[] < Manifest.Permission.AccessFineLocation >, REQUEST_FINE_LOCATION_PERMISSION); > if (CheckSelfPermission(Manifest.Permission.AccessBackgroundLocation) != Permission.Granted) < RequestPermissions(new string[] < Manifest.Permission.AccessBackgroundLocation >, REQUEST_BACKGROUND_LOCATION_PERMISSION); > // Request the BLUETOOTH_SCAN permission at runtime if (CheckSelfPermission(Manifest.Permission.BluetoothScan) != Permission.Granted) < RequestPermissions(new string[] < Manifest.Permission.BluetoothScan >, REQUEST_BLUETOOTH_SCAN_PERMISSION); > //Request the BLUETOOTH_CONNECT permission at runtime if (CheckSelfPermission(Manifest.Permission.BluetoothConnect) != Permission.Granted) < RequestPermissions(new string[] < Manifest.Permission.BluetoothConnect >, REQUEST_BLUETOOTH_CONNECT_PERMISSION); > > 

Once I added the runtime permission, it began to work on Android 13.

Источник

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.

Источник

Читайте также:  Elm327 bluetooth ford fusion
Оцените статью
Adblock
detector