Android Bluetooth LE Scanning Background Location Permissions - android

I have an app, which implements Bluetooth LE Scanning (by extending BroadcastReceiver) to identify when the device is nearby a beacon.
I am experiencing some unexpected behaviour when testing on Android 10 & 11 with targetSDK = 30. I understand that in API 29 (Android 10) the background location permission was introduced and that this permission in required for Bluetooth LE Scanning.
However, when I test my app on Android 10 the scanning functionality works correctly without the ACCESS_BACKGROUND_LOCATION permission in my manifest. It identifies a nearby beacon if the app is in the foreground, the background or closed.
When I test my app on Android 11 the scanning functionality only works if the device is with range of a beacon and the app is brought from the background into the foreground. It doesn't work if the app is in the background or running in the foreground, only in this transition state.
If I add the ACCESS_BACKGROUND_LOCATION permission in my manifest and request this permission at runtime (in the incremental fashion Google enforce) then as long as the user has given at least foreground permissions then the scanning functionality works as expected.
I'm a bit confused by this behaviour specifically:
If the ACCESS_BACKGROUND_LOCATION permission was added in API 29 then why am I not seeing the behaviour I'm seeing on Android 11 on my Android 10 device?
Why do I need the ACCESS_BACKGROUND_LOCATION permission when even if a user only gives me foreground permissions (i.e. doesn't select 'Allow All The Time') then it still works and why is this needed for it to work in the foreground also?

Related

Does BLE connection require ACCESS_BACKGROUND_LOCATION permission in android 10?

I have job scheduler which searches BLE devices and connects in background. Android 10 introduced new ACCESS_BACKGROUND_LOCATION permission. Does ACCESS_BACKGROUND_LOCATION permission require to connection BLE device in background?
https://developer.android.com/guide/topics/connectivity/bluetooth#Permissions only mentions the app needs ACCESS_FINE_LOCATION, so I suspect you don't need ACCESS_BACKGROUND_LOCATION. But I see it likely this will be changed in the near future.
A quick look at https://android.googlesource.com/platform/packages/apps/Bluetooth/+/refs/tags/android-10.0.0_r32/src/com/android/bluetooth/gatt/GattService.java#1949 shows that only the fine permission seems to be needed, but I can have missed something.
I suggest you to simply try with only ACCESS_FINE_LOCATION to see what happens in the background.
I Think this article would help as it talks about location permission in android 11 and use of BLE.
Location.
also i would recommend you use Foreground services to give your user more insight on what your app is doing and allow them to determine if they need to give you app the permission once, or always.
Running a service in the foreground
Android documentation says:
If your app supports a service and can run on Android 10 (API level 29) or Android 11, you must also declare the ACCESS_BACKGROUND_LOCATION permission to discover Bluetooth devices. For more information on this requirement, see Access location in the background.
More information here: Android bluetooth permissions
So answer is: If you scan for devices or connect to them from service (even foreground service) you need to request for ACCESS_BACKGROUND_LOCATION permission.

Altbeacon can't find beacons on Samsung S8 when location is turned off since 2.16.2

I am building an Android app that scans two types of beacon each second using the android-beacon-library:
iBeacon type
custom beacon type
I am using the library since the 2.10 release and everything works fine.
But, when I try to implement the 2.16.2, 2.16.3 and 2.16.4 releases on my Samsung S8 (which is running on Android 9), I can't scan my Beacons when I turn off the location. I did not have this issue with the 2.16.1 release.
I also tried the reference app to check if I did something wrong, but I have the same bug with the reference app.
I may specify that I only have this issue with Samsung.
Thanks.
Recent versions of Android require multiple levels of location authorization for an app to detect beacons (bolded items show added requirements):
SDK 29+ (Android 10.0+)
Bluetooth must be turned on in settings
Location must be turned on in settings
Apps must have FINE_LOCATION permission in the manifest (COARSE_LOCATION is no longer sufficient)
Apps must ACCESS_BACKGROUND_LOCATION in the manifest to detect beacons when the app is not in the foreground with the screen on.
Apps must have dynamically obtained FINE_LOCATION permission from the user
Apps must have BLUETOOTH and BLUETOOTH_ADMIN permission in the manifest
SDK 23-28 (Android 6.0-9.x)
Bluetooth must be turned on in settings
Location must be turned on in settings
Apps must have COARSE_LOCATION permission or FINE_LOCATION permission in the manifest
Apps must have BLUETOOTH and BLUETOOTH_ADMIN permission in the manifest
Apps must have dynamically obtained COARSE_LOCATION permission or FINE_LOCATION permission from the user
SDK 18-22 (Android 4.3-Android 5.x)
Bluetooth must be turned on in settings
Apps must have COARSE_LOCATION permission or FINE_LOCATION permission in the manifest
Apps must have BLUETOOTH and BLUETOOTH_ADMIN permission in the manifest
The above restrictions have nothing specific to do with the Android Beacon Library -- they are an operating system requirement for any Bluetooth LE detection. If you see that some devices to not enforce these requirements under some conditions, then that is most likely an implementation hole on that specific platform. The more general rules still apply.

Enable to monitor beacon on android 6.0 using nexus 5 device

I am asking a follow up question to my previously asked question -> http://stackoverflow.com/questions/33607410/background-monitoring-of-eddystone-beacon-using-altbeacon-library-on-android-pla/33613116?noredirect=1#comment55139316_33613116.
Where i was able to successfully detect beacons both in the foreground and background until I found out that the same code was unable to detect the beacon when i try to run it on nexus 5 device running android 6.0.
Can anyone provide a explanation to why this is happening? Thanks.
Android 6.0 imposes two new requirements in order for apps to detect BLE Beacons:
Location Services must be turned on in settings. Settings -> Location -> On. The specific mode may be either "High Accuracy" or "Device Saving". This is a new requirement that appears to be being rolled out to all firmware builds from Google. Without this on, BLE scans discover no devices.
The app must be given runtime permissions by the user to ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. You must code your app to present one of these permission requests to the user. If not granted, the app will not be able to detect BLE Beacons.
A detailed explanation of this process is in my blog post here: Is Your Beacon App Ready For Android 6.0?
With Android 6.0, you now need to request location permissions at runtime in order to be able to discover beacons.
From our initial tests: if your app targets SDK < 23 (i.e., pre-Android 6.0), you will only need these permissions to detect beacons in the background. If your app targets SDK >= 23 (i.e., Android 6.0 or later), you will need these permissions to detect beacons both in the background and in the foreground. You also need to have Location enabled on your Android 6.0 device for both cases.
Google has a detailed guide on how to implement checking for and requesting runtime permissions:
http://developer.android.com/training/permissions/requesting.html
Specifically, you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. They both actually display the same message ("Allow APP_NAME to access your location?") to the user.

Bluetooth discover/scan code not working after update Android 6 on my Nexus 5

After the update (Android 6) my nexus 5, my running application not able to scan(find) the any bluetooth device. getBondedDevices() to get paired devices list working fine.
I have also test many sample code and application (I have put link below) of bluetooth functionality. all have the same problem, it's not able to find the bluetooth device.
But yes I have found only one application on play store which is working and it able to discover/scan the bluetooth device.
Not working apps on Android 6 (Nexus 5)
I have tested many app like, bluetrem, sample code by Android Developer and many more.
Working app
Bluetooth 4.0 Scanner
https://play.google.com/store/apps/details?id=com.bluemotionlabs.bluescan
Is Any one know what is the problem, is there any changes required on code to make compatible with Android 6
At least in Android 6.0 November 1, 2015 security patch, you not only need ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions but also need the location service actually turned on in order for Bluetooth scan to work.
I verified this in my app and a bunch of other BLE apps in Play Store, including Bluetooth 4.0 Scanner.
This means we have to make sure location is on before starting a scan, otherwise it will fail silently.
It works, this is a solution, try to turn on GPS after put ACCESS_FILE_LOCATION or ACCESS_COARSE_LOCATION and after ask the runtime permission
As it's in the change list of Android 6, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions for some bluetooth calls. Please check if you have it.

Android Wear (5.0) with Android Marshmallow's permission model

I'm in the middle of building a Wear App for an existing phone app.
We plan on targeting API 23 and modifying our phone app's location permissions to work with Android new runtime permission model (https://developer.android.com/training/permissions/index.html)
I was trying to figure out how the Wearable device running Android 5.0 is going to work once we do that. From what I've learnt when asking for locations for the Wearable from https://developer.android.com/training/articles/wear-location-detection.html:
The wearable may or may not have a GPS radio present. For wearables that do not have a radio, the location data comes from the tethered device.
We don’t need to worry about the source of location data – FusedLocationProvider (as part of the Google Play Service) handles this for you behind the scene.
We are responsible for handling the edge case where the wearable does not have GPS hardware (can be checked via hasSystemFeature()) and if the tethering is interrupted (can be detected via WearableListenerService). An example of this would be when the user goes for a run without the phone. In this case, we are expected to degrade the functionality gracefully.
What’s puzzling me is how this will work when we migrate to the new Marshmallow permission model.
Android Wear is not affected if the hardware has on board GPS since no wear devices run Android 6.0 and the old permission model applies. The problem would come about if a wear device with no GPS hardware is tethered to a device running Android 6.0. In this case, if the wear device requests for the location, it would passed on to the phone and the user has not been asked permission/or has disabled the permission, how will this work at all?
I'm imagining that need to hack around this:
On wearable app start, first check if the hardware has a GPS radio
If no, send a message to the phone to ask if holds the permission for requesting location (use the data sync APIs for this)
If the answer comes back false, then show a message to the user on the wearable and ask if they want to resolve it on the phone.
If the user answers “yes”, push a message to the phone to show an activity where it would just prompt for permission.
Is there a better way to do this?

Categories

Resources