We are working on an app that needs to scan for iBeacons in background. We did a lot of optimizations and now it works well on Kitkat and Lollipop devices without consuming much battery.
On Marshmallow we require fine location permission to scan for iBeacons (coarse location has delays). This drains a lot of battery even if we have the same battery saving logic in place.
Is there a way that we can release fine location permission when the device is still (no movement) and acquire it when we see a movement? Since the user would have provided the runtime permission at app launch, re-acquiring that permission shouldn't bring the permission dialog again.
If there is way to achieve this, it would help us save the battery drain problem.
A few points:
Scanning for BLE devices requires either android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION It doesn't matter which one an app obtains.
In my tests, there is no difference in BLE device scanning performance between either of the above permissions. For this reason, the Android Beacon Library documentation suggests requesting android.permission.ACCESS_COARSE_LOCATION, and declares this permission in its manifest.
Declaring, requesting or obtaining a permission has no affect on battery drain. It is actually doing location-specific operations (BLE scanning, requesting GPS location updates, etc.) that affects battery usage.
The description of the problem says the app uses more battery when android.permission.ACCESS_FINE_LOCATION is obtained vs. android.permission.ACCESS_COARSE_LOCATION. Because of the points above, I do not believe the difference is explained beacon scanning. A possible alternative explanation is that the app is requesting location updates from the operating system (since I can't see the code, I don't know if this is true -- it's just a theory). If ACCESS_FINE_LOCATION is obtained, then that will unlock the GPS_PROVIDER, potentially powering up the GPS and causing the additional battery drain described.
See: http://developer.android.com/guide/topics/location/strategies.html
Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)
Related
Some surveys and analytics showed that users very often turn down Location Permission request appearing in the mobile app even if this permission is crucial for the app, e.g. the app performs automatic scan for devices in a venue that handle business logic and exchange data with the mobile app. The permission request turn down rate is so high that it becomes problematic for business. Is there any way in Android to avoid this, e.g. to declare some verified devices that can be scanned without permission request? I thought of https://developer.android.com/guide/topics/connectivity/companion-device-pairing but it also means popping up requests for manual work for the user. Is there any way today to do it behind the scenes?
Unfortunately this is not possible for privacy and security reasons. Enabling BLE on Android devices goes hand in hand with the location permission. The only possible workaround for this is potentially if you use classic Bluetooth instead of BLE. I am not 100% sure about this one, but it is mentioned in this article:-
No one can use BLE scan API without having Location Permissions, and,
on most devices, it is useless without active Location Services.
However, Bluetooth Low Energy is an extension to so-called Bluetooth
Classic and interestingly enough Google decided this API does not need
location permissions to be called.
This is an OS requirement which cannot be overcome. You might've already seen this on the Android developer page for BLE:-
In order to use Bluetooth features in your application, you must
declare two permissions. The first of these is BLUETOOTH. You need
this permission to perform any Bluetooth communication, such as
requesting a connection, accepting a connection, and transferring
data.
The other permission that you must declare is ACCESS_FINE_LOCATION.
Your app needs this permission because a Bluetooth scan can be used to
gather information about the location of the user. This information
may come from the user's own devices, as well as Bluetooth beacons in
use at locations such as shops and transit facilities.
You can find more info about this here:-
The Ultimate Guide to Android BLE Development
Location needs to be enabled for BLE on Android
Bluetooth versus Location Permission
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.
I have an estimote beacon, i want to display a notification, when user comes in beacon range.
I was checking estimote beacon's android integration document, and found out that it requires location permission.
Estomote official app also does not work, if location permission is not granted.
My question is how to make beacon work without asking location permission, or if beacon does not work without location permission then what's the reason of using beacon, or am i missing something very important.
Android 6+ requires an app to obtain either COARSE_LOCATION or FINE_LOCATION permission at runtime in order to detect Bluetooth LE beacons or do Bluetooth LE scans at all. Without the permission, the OS blocks beacon detections. This is not an Estimote-specific issue -- it applies to scanning for any Bluetooth LE device.
This requirement was added to protect the privacy of users. The theory is that a runtime permission dialog makes it clear to a user that the app is accessing the user's location and ensures the user is aware of this and consents to the practice.
google play store monitor the bad behavior of Excessive Wi-Fi Scanning in the Background.
not able to identify the issue
i have add bellow permission in manifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
please suggest appropriate response.
thanks
Assuming your app is using FusedLocationProvider. If you subscribe to retrieve location from GooglePlayServices in background more often than once per 15 minutes, your app will be blamed for excessive WiFi scanning.
https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html
Activities should strongly consider removing all location request when
entering the background (for example at onPause()), or at least swap
the request to a larger interval and lower quality.
If device is setup in high accuracy location mode, the device will attempt to use wifi scans in order to resolve its location. Hence you have to consider increasing active interval for retrieving location updates.
public LocationRequest setInterval (long millis)
Excessive Wi-Fi Scanning in the Background occur, Android vitals considers Wi-Fi scanning excessive when an app performs more than 4 scans per hour. Normally this issue can be reported by Android vital via play console. Otherwise we can use Battery historian tool to find background wifi scanning for each application.
We rectify this problem
If possible, your app should be performing Wi-Fi scans while the app is running in the foreground. Foreground services automatically present notifications; performing Wi-Fi scans in the foreground thus makes the user aware of the why and when Wi-Fi scans take place on their device.
I developed an app that work with iBeacons and this week i update it to work on Marshmallow. Until the update it didn't require GPS, but after the update it says that to find beacons in background it need gps. i don't find any reference on it. does someone know why Android Marshmallow requires GPS to find iBeacons?
I use altBeacon library to locate the beacons.
edit:
I'll clarify the question. Is the GPS involved in finding Beacons. Except for the permissions.
No, it is not necessary to have GPS turned on or even have a device with a GPS radio to detect beacons in Android 6. The GPS radio, if present is not activated when scanning for Bluetooth beacons.
The confusion comes from the fact that Android 6 newly requires apps to obtain either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions to be allowed to perform bluetooth scans. This change is simply motivated by informing users that bluetooth scanning can be used to infer their location (like with beacons). But the need to get this permission does not mean there is any technical dependency on the GPS radio. Consider that Android also can use WiFi access points and cell towers to infer location.
On some devices, notably the Nexus 5, there have been reports that disabling Location Services (Settings -> Location -> Off) disables the ability to scan for bluetooth devices. It is unclear whether this is a bug in that Nexus 5 build image or a new security restriction by Android that will be followed on other models in the future. See here for more info. Regardless, having the GPS radio on is not a requirement for Location Services being enabled.
Full disclosure: I am the lead developer on the Android Beacon Library open source project.
from http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
To access the hardware identifiers of nearby external devices via
Bluetooth and Wi-Fi scans, your app must now have the
ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:
It's not GPS as such that it needs, but because beacons are a technology that can also pin point your location, the location permission is required.
This is probably a permissions issue. iBeacon uses BT Low Energy, and in Android 6, you need location permission to initiate a scan:
To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions
Source: Android 6.0 changes