Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Android 12 Bluetooth Permissions #752

Closed
mattinger opened this issue May 20, 2021 · 32 comments · Fixed by #762 or #787
Closed

Support Android 12 Bluetooth Permissions #752

mattinger opened this issue May 20, 2021 · 32 comments · Fixed by #762 or #787

Comments

@mattinger
Copy link

mattinger commented May 20, 2021

With Android 12 comes two new bluetooth permissions that will replace Location Permission:

  • Bluetooth Scan
  • Bluetooth Connect

The library needs to be updated to reflect these states. In addition, Bluetooth Connect is only
required if you're going to open a gatt connection to the the device, so it shouldn't block the
operation of the library. Currently the RxBleClient.state is a progressive check that ends up with an isReadyForScanning
when all permissions and services have turned on.

I'd recommend that this be broken out into two states. Once for scanning and one for connecting.

https://developer.android.com/about/versions/12/features/bluetooth-permissions

@ArturMytsko
Copy link

Is there any progress on that taks or estimates?
Android 12 Beta 2 was released yesterday, we are not far away from the final APIs (in August)

@AndHain
Copy link

AndHain commented Jul 8, 2021

Are there any updates to this task?
Will the library be ready for the upcoming changes in Android 12?

@hosek
Copy link

hosek commented Jul 9, 2021

Btw there is also ACCESS_BACKGROUND_LOCATION missing for API 29+ (can't perform scan without it)
https://developer.android.com/about/versions/10/privacy/changes#app-access-device-location
Just adding note here https://github.com/Polidea/RxAndroidBle#permissions will be helpful

@SaqibJDev
Copy link

Is there even a plan for this change? It would be really helpful not to force users to turn on location only for Bluetooth.

@dariuszseweryn
Copy link
Owner

I have it on my plate. I am currently running this project on my free time which I do not have plenty unfortunately. 🤷‍♂️

@dariuszseweryn
Copy link
Owner

Is there any info whether on Android 12 / API 31 scan results will get returned if Location Services are turned off while the scan is running?
I do not have access to any device that can run Android 12.

@mattinger
Copy link
Author

Btw there is also ACCESS_BACKGROUND_LOCATION missing for API 29+ (can't perform scan without it)
https://developer.android.com/about/versions/10/privacy/changes#app-access-device-location
Just adding note here https://github.com/Polidea/RxAndroidBle#permissions will be helpful

@hosek You don't need background location to scan while the app is active. I agree that if you want to build an app that's scanning while the app is backgrounded you'll need it. But that should only be necessary under certain circumstances where you're maybe looking to recognize being in range of certain devices. But in general, you don't need it.

@mattinger
Copy link
Author

Is there any info whether on Android 12 / API 31 scan results will get returned if Location Services are turned off while the scan is running?
I do not have access to any device that can run Android 12.

That's a great question. Usually if you're targeting an older SDK (in this case Android 11/Api 30), you'd fallback to the previous behavior, or something compatible to that. I don't see anything in particular about this scenario though in the feature docs: https://developer.android.com/about/versions/12/features/bluetooth-permissions

@joaquinchg
Copy link

Is there any info whether on Android 12 / API 31 scan results will get returned if Location Services are turned off while the scan is running?
I do not have access to any device that can run Android 12.

Hi @dariuszseweryn, I have access to a Pixel 5 running the current Beta of Android 12. I'm interested in RxAndroidBle supporting API 31, I wouldn't mind testing anything and provide logs that could help to close this issue.

@dariuszseweryn
Copy link
Owner

Awesome, thank you for the proposition. I've got some support from a Google employee and I think I have all needed knowledge about how the new OS is working.
I have now a metric tone of things to manage in my private life and it is hard for me to schedule work. When I will have something workable I will share it. Plus if anyone would like to get involved in writing the actual code / helping me — I can draw out a set of tasks to do and at least some hints on how to achieve them.

@mattinger
Copy link
Author

I will check with the appropriate people at work if i can contribute (have to go through a whole process). But there's definitely some other suggestions of things i'd like to support (like ble encryption during gatt connect via a pre-computed pin). That's definitely a big need i have coming down the pipe and i don't see anything in the library that supports it.

@remonh87
Copy link

@mattinger did you already work on this issue? Would be great to have this functionality in. We also need this for our flutter library. Let me know if I can help out.

@sung2063
Copy link

sung2063 commented Oct 5, 2021

Any progress on Android 12 Bluetooth permissions?

@dariuszseweryn
Copy link
Owner

I have just pushed 1.13.0 — please check it out as I do not have capabilities myself.

@dmacpro91
Copy link

dmacpro91 commented Oct 6, 2021

Getting the following on the latest Android 12 version on a Pixel 5 with 1.13.0.

This is right after a start scan.

Screen Shot 2021-10-06 at 6 16 00 PM

Screen Shot 2021-10-06 at 6 17 51 PM

Edit:
I already have the aforementioned permissions defined in my manifest.
Screen Shot 2021-10-06 at 6 19 02 PM

@dariuszseweryn
Copy link
Owner

What target SDK?

@dmacpro91
Copy link

dmacpro91 commented Oct 7, 2021

Originally this was targeting 29, but after updating the targetSDKVersion to 31, I'm now getting this error.

LOCATION_PERMISSION_NOT_GRANTED for rxBleClient.getState() before starting the scan.

I have confirmed that location permissions have been granted as my app also uses this for GPS Speed readings.

Edit:
I've also added in the changes where you have to request 'ACCESS_FINE_LOCATION' along with 'ACCESS_COARSE_LOCATION' in a single runtime request when targeting Android 12.

I can confirm that my callbacks are showing that I have all the necessary permissions required for scanning.

Edit #2:
My bad, I didn't realize we had to also request BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions manually after app-launch. After adding those in scanning is working again!

@dariuszseweryn
Copy link
Owner

Yeah, I need to update two more javadocs. Could you confirm that when the app uses target SDK <31 the OS will still complain about no BLUETOOTH_SCAN permissions?

@mattinger
Copy link
Author

Originally this was targeting 29, but after updating the targetSDKVersion to 31, I'm now getting this error.

LOCATION_PERMISSION_NOT_GRANTED for rxBleClient.getState() before starting the scan.

I have confirmed that location permissions have been granted as my app also uses this for GPS Speed readings.

Edit: I've also added in the changes where you have to request 'ACCESS_FINE_LOCATION' along with 'ACCESS_COARSE_LOCATION' in a single runtime request when targeting Android 12.

I can confirm that my callbacks are showing that I have all the necessary permissions required for scanning.

Edit #2: My bad, I didn't realize we had to also request BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions manually after app-launch. After adding those in scanning is working again!

So are you saying if we want to scan, we have to request all 3 (BLUETOOTH_SCAN plus the two location permissions)? I would think if set the neverForLocation you wouldn't need that.

@dmacpro91
Copy link

Yeah, I need to update two more javadocs. Could you confirm that when the app uses target SDK <31 the OS will still complain about no BLUETOOTH_SCAN permissions?

Yes, on a fresh install without the BLUETOOTH_SCAN/BLUETOOTH_CONNECT permissions declared in the manifest AND manually requested, my app targeting API 29 still can't scan for devices. It errors out with

Screen Shot 2021-10-07 at 2 24 09 PM

So are you saying if we want to scan, we have to request all 3 (BLUETOOTH_SCAN plus the two location permissions)? I would think if set the neverForLocation you wouldn't need that.

In my application we use the precise location anyways for GPS readings, but from my understanding if you're just trying to scan BT devices (and use the neverForLocation flag) that should be all you need for Android 12.

@mattinger
Copy link
Author

@dariuszseweryn Just looking for some confirmation on what permissions it checks based on OS.
Assuming you set neverForLocation, would isReadyForScanning adapter to the OS?

For 30 and below: check fine location / coarse location
For 31 and higher: check bluetooth scan

Our app does both a scan and a connect, so we'll likely have to wait until your changes are ready for breaking the state into 2 pieces. It's just unclear to me what the behavior is for the current version that's published.

@dariuszseweryn
Copy link
Owner

What is being checked depends on both deviceSdk and application targetSdk
If the deviceSdk is 30 then ACCESS_FINE_LOCATION
If the deviceSdk is 31 and targetSdk is 31 then BLUETOOTH_SCAN assuming naverForLocation flag is used

@mattinger
Copy link
Author

@dariuszseweryn Is there any api which can give us the required permissions? I've written a few helper functions to do things like return the permissions we need to ask for, decide if location services needs to be on, etc, but it makes more sense for the library to have these.

@dariuszseweryn
Copy link
Owner

See RxBleClient#getRecommendedScanRuntimePermissions

@remonh87
Copy link

I can confirm it works like intended many thanks! 🙏

@williamgoodhew
Copy link

williamgoodhew commented Oct 28, 2021

I have added the following to my manifest.xml

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

I am also trying to request permission for

const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT
)

I am getting the following error message in the Android Studio logs -
java.lang.IllegalArgumentException: permission is null

Does anyone know how you request permission for BLUETOOTH_CONNECT or BLUETOOTH_SCAN?

I have tried requesting both using the PermissionsAndroid.request() function but I still get the same error.

Whenever I try requesting a different kind of permission I am not getting any errors - only with the bluetooth permissions.

@dariuszseweryn
Copy link
Owner

@williamgoodhew I am afraid that you should ask for support from the PermissionsAndroid package provider. This is and RxAndroidBle library repository which is a native Android lib, not Javascript (as I assume from the code snippet)

@williamgoodhew
Copy link

sorry I got confused with the ble-plx library you guys used to do.

@dmacpro91
Copy link

I have added the following to my manifest.xml

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

I am also trying to request permission for

const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT
)

I am getting the following error message in the Android Studio logs - java.lang.IllegalArgumentException: permission is null

Does anyone know how you request permission for BLUETOOTH_CONNECT or BLUETOOTH_SCAN?

I have tried requesting both using the PermissionsAndroid.request() function but I still get the same error.

Whenever I try requesting a different kind of permission I am not getting any errors - only with the bluetooth permissions.

Update the following in your build.gradle:

compileSdkVersion 31
buildToolsVersion '31.0.0'

@techker
Copy link

techker commented Nov 9, 2021

is i possible to update the Sample-Kotlin for these fixes?Cause on OS12 it doesn't work.Thx

@mattinger
Copy link
Author

@dariuszseweryn Is there any plan to add functions around permission checks for gatt connections? Maybe something like isPermissionRequiredToConnect and getRecommendedConnectRuntimePermissions

@nrbrook
Copy link
Contributor

nrbrook commented Mar 3, 2022

@mattinger I am implementing this in #768

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment