Ble beacon Android Oreo and P not waking app from doze mode - android

I have an app that uses Bluetooth Beacons. When app is in doze mode proximity notifications from bluetooth beacons are not waking the phone. It is only when I unlock phone that the notification is delivered. This issue is the same for foreground or app that has been killed.

Starting with Android 6.0, Doze mode blocks any bluetooth scan callbacks or other events, including beacon detections. I discussed this issue with Android Bluetooth engineers during a session at Google I/O 2015. Their position was that because sensed motion will exit Doze mode, this is not an issue, since a phone typically cannot discover new bluetooth beacons when it is motionless and therefore presumably in the same place.
While this is true for most use cases, there are a few exceptions: (1) On a train or other vehicle in a single direction on a straight path, the accelerometer won't detect motion and the phone may go into Doze despite being in motion and potentially seeing new beacons. (2) In some use cases, beacon advertisements are built to be dynamic, so they appear and disappear in the same case. In both of these situations Android devices are simply unable to detect new beacon advertisements if they are in Doze mode.
This is an unfortunate limitation you must simply design around.
EDIT: Starting with Android 7 there are two types of Doze mode, heavy and light. Light doze mode kicks in when the screen is off but the phone is not motionless. Heavy doze mode kicks in when the screen is off and the device is motionless for a period of time. In light doze mode, beacon detections are delivered normally. In heavy doze mode, they are not supposed to be according to Google. However, on some devices they do. I see them delivered in heavy doze mode on a Samsung Galaxy S8 with Android 8.0 with a foreground service running.

Related

Android BLE Scan Stops after a couple of minutes in background

I faced the Android scan stopping issue in background when I am developing my COVID contact tracing app for my company. Here is what I have tried:
Add foreground service
Disable all the battery related optimization options in the phones
Enable the application running in background
Tesging devices:Galaxy S20 and Xperia with Android 10, Huawei with Android 8.
The scan stops immediately when going to background if you don't disable those battery optimization settings and application background update. After you disabled those settings, the scan can run about a couple of minutes(~5 minutes), then still stops. From the blog of David:http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8, it seems that it is impossible to scan continuesly in background, because the JobScheduler will restart every 15 minutes while each scan lasts ~10 minutes at most. Is this the reality, or this is the best solution that I can scan 10 minutes at every 15 mins cycle?
Background beacon detections are tricky to implement because many small issues can trip you up, and the specific issues vary by Android version, Android manufacturer and sometimes model. While Android 8+ restricts background ranging to every 15 mins using the Job Scheduler, if you add a Foreground Service, you can unlock unlimited background ranging.
A few tips:
Focus first on the Galaxy S20 as Samsung behaviors are better documented and closer to vanilla Android. (Ideally you would test on a Pixel device first.) Only once it works on Samsung, move on to the others.
Using the Android Beacon Library reference app configured with the built-in foreground service, I have seen detections of a standard iBeacon or AltBeacon continue on a Galaxy S10 indefinitely in the background, even with a 1.1 second scan period. See if you can reproduce the same.
Be careful of Doze mode. If the CPU is put to sleep due to the phone being motionless with the screen off and not charging, your detections will stop. You can defeat Doze mode with wake locks, but it has a punishing impact on battery usage. You are better off accepting its limitations and keeping your phone in motion periodically during testing. If you want to get logs to see what is happening, use ADB commands to disable charging when connected via USB or learn to use ADB over WiFi.

Android does not exit doze mode when moving with device

So some background, i am developing a location tracking application. I have a service running that gets gps updates using the fused location provider. When the app is closed or the device is locked my service is moved to the foreground. It also uses a partial wakelock and is whitelisted.
I am under the impression that a device should exit doze mode if it moves. As stated in this answer here. However i am seeing inconsistent results on different devices. I have three test devices. A sony experia, a moto e4 plus and a nexus 5x. The application works great on the sony and the nexus. I leave it idle, doze mode kicks in and then when i move the device it exits doze mode and gps updates start again. Which is great because i do not need gps updates when the device is stationary, it would just waste battery life. Just for clarity the different android version for each device are:
Sony Xperia: 7.0
Moto E4 plus: 7.1.1
Nexus 5X: 8.0
The issue is with the moto, i leave the device to enter doze mode. However upon picking it up and moving with it, it does not exit doze mode. It only exits doze mode with some user interaction. After looking through other answers and androids documentation am i correct in assuming that moving the device should always results in it existing from doze mode. If that is the case is this more likely to be a hardware issue, or can the OEM change how doze mode should work?
Thank you for any help.
So android needs to be able to detect motion in order to leave doze mode. The moto does not have a gyroscope. This stops it detecting the movement and exiting doze mode.
For having this feature working, the manufacturer should have been implemented the "Significant Motion sensor". You can check if this sensor is available by asking if this sensor exists: "android.sensor.significant_motion".
SensorManager mSensorManager = (SensorManager) mContext.getSystemService(Service.SENSOR_SERVICE);
boolean isSensorPresent = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION) != null;
Apart of this, there is also some manufacturers that bypass the Doze mode rules with their own power management system. It is the case for some of the phone manufactured by Huawei & Xiaomi for example.
In those cases, it is possible to stop the manufacturer power manager system to suspend your application. But it has to be done manually by the user.

White listed android app in doze mode

I am trying to make a GPS tracker app.
I don't intent to distribute this app in play store. So, I got full control of the device(and can do manually whitelist).
I would like to know if:
Would doze mode be activated if charger is always connected to
phone?
Would sudden jerk or vibration to phone disables doze mode
after a prolonged inactivity? For example,consider phone placed
inside a truck and would that vibration (from engine start or
travelling) be sufficient to get the device from doze to normal
mode?
Will job scheduler service work in doze mode with scheduled
app manually whitelisted (including GPS and network)?
Would phone call to doze mode device changes its state to normal mode?
Would sensors like gyro, accelerometer work in doze mode?
Question 1:
Would doze mode be activated if charger is always connected to phone?
No.
As soon as the user wakes the device by moving it, turning on the
screen, or connecting a charger, the system exits Doze and all apps
return to normal activity.
Question 2:
Would sudden jerk or vibration to phone disables doze mode after a
prolonged inactivity? For example,consider phone placed inside a truck and would that vibration (from engine start or travelling) be sufficient to get
the device from doze to normal mode?
Prior to Android N, Yes. From Android N, no.
Android 7.0 brings further enhancements to Doze by applying a subset
of CPU and network restrictions while the device is unplugged with the
screen turned off, but not necessarily stationary, for example, when a
handset is traveling in a user’s pocket.
Question 3
Will job scheduler service work in doze mode with scheduled app
manually whitelisted (including GPS and network)?
No.
An app that is whitelisted can use the network and hold partial wake
locks during Doze and App Standby. However, other restrictions still
apply to the whitelisted app, just as they do to other apps. For
example, the whitelisted app’s jobs and syncs are deferred (on API
level 23 and below), and its regular AlarmManager alarms do not fire.
Question 4
Would phone call to doze mode device changes its state to normal mode?
Yes. Due to same reason as Question 1.
Question 5
Would sensors like gyro, accelerometer work in doze mode?
Might not work in Doze mode
For more details on Optimizations in Doze mode, you can refer this document.

Bluetooth Low Energy scan result delivery is reduced when the screen is OFF

I am developing an android application that uses Bluetooth Low Energy to receive data from a peripheral device.
The device itself sends the data as part of it's advertising message and it could send the message at any time.
So the applications works by continuously scanning for BLE devices.
When the screen is ON the application receives scan results as intended.
When the screen is OFF it appears that it only receives scan results every 5 seconds.
I guess that the phone is trying to optimize for battery life but this app is mission-critical so we can not afford to lose messages.
The scanning is done by a Service which is set as a 'foreground service' so that the OS knows that it is important.
The service also acquires a PowerManager.PARTIAL_WAKE_LOCK but that does not seem to help.
The application is excluded from battery optimizations so that it does not enter Doze or deep sleep mode.
The application is using the following ScanSettings:
ScanSettings.SCAN_MODE_LOW_LATENCY
ScanSettings.CALLBACK_TYPE_ALL_MATCHES
ScanSettings.MATCH_MODE_AGGRESSIVE
ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT
I am working with a Galaxy S7 but I have been told the same behaviour happens with a Galaxy S7 Edge.
I have tested with other non-samsung devices and they work as intended (continuously receiving scan results when the screen is OFF).
The question: is there something that I, as a developer, can do to make the BT adapter deliver scan results continuously even when the screen is OFF?
Samsung automatically change scan mode to low_power if screen is turned off. It's their customisation on their Android OS, so unfortunately no way to change it.

Android Doze mode

Documentation says:
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.
What exactly stationary mean? Does phone go into Doze if it is stationary in a car that is moving? I'm developing an app that send location update to a server and if phone go into Doze mode while moving in car that can't be done.
Stationary means literally stationary, no gyro activity, screen off, not connected to charger. As far as the car is moving, the phone will not enter Doze mode.
Android developers: Optimizing for Doze and App Standby
It should be noted that Android N has extended doze mode further. Here the device doesn't need to be 'absolutely stationary' for a 'lighter' version of doze - that allows wake locks but restricts network access - to kick in.
The principal is that to truly take advantage of this, android needs to conserve battery if the phone is in a user's pocket where it is 'unused' but may not be stationary.
The original Marshmallow 'deep-doze' is still very much present in N so once the phone is stationary for a good amount of time [screen off + not being charged are already met criteria] it will go into the original doze where wifi scans and gps scans [location updates], wake locks, job schedulers, sync adapters, and network access is restricted.
To answer your question for location updates, android has a fine motion detecting sensor which ensures that the device is stationary w.r.t earth before diving into the deeper version of doze. So for this particular use case you should be good to go but you should still use the newly introduced Job scheduler api for batched updates. I am sure fetching location is one aspect, you want to upload it to your servers as well. Network access is restricted in both versions of doze modes.
references
https://plus.google.com/+AndroidDevelopers/posts/gwA68kaVJQ8
https://www.youtube.com/watch?v=VC2Hlb22mZM
In case of Android M doze mode will not get kicked in if the device is in moving car.
But in Android N, a light doze mode has been introduced that does not require the device to be completely stationary or wait as long to activate.It starts shortly after both the screen is off and the device is not charging, waiting only a couple minutes before applying the restrictions just to make sure the user has stopped using their phone.
In case,phone is Android N it may go into light doze mode.

Categories

Resources