I have been following https://developer.android.com/guide/topics/connectivity/wifi-scan guide in order to create a Wi-Fi scanner. I am using a foreground service with a handler to call wifiMananger.startScan();
every 30 seconds (I tried with 15 minutes).
Everything works great for about 2 hours then suddenly WifiManager.EXTRA_RESULTS_UPDATED
boolean returns false and the wifiManager.getScanResults()
are not getting updated. Then as soon as the phone is plugged in it starts sending results again. (No, it is not low on battery)
I have battery optimization turned off. I have all of the required permissions allowed. Locations is turned on with Wi-Fi scanning enabled. The device I'm testing on is a Samsung S7 Edge running Android 8. So I know it's not the new OS. (I also tested with a Nokia 5.1 running Android 10 with pretty much the same results).
Does anyone know why this is happening or has anyone encountered this issue before?
Thanks in advance.
Restrict Scanning Always Option. Keeping the WiFi scanning active on the Android won't hold it stable. This may be the reason why the problem of network connection is happening. To find it, navigate to mobile settings >> WiFi >> Scanning Always Available.
Turning off Wi-Fi scanning You'll find it at "Security & Location". Under the "Privacy" subhead, tap "Location" then tap "Scanning". You'll have the option to turn off both Wi-Fi and Bluetooth scanning if you like. For all other Android phones, it should be in a similar place.
Should I Have Wi-Fi Scanning On? You do not need wifi scanning if you live at home, because your router stays connected to you, so it is not necessary. You may want to turn the WiFi on if you still think there is a wireless network, locate a public signal, connect, turn it off.
Each background app can scan one time in a 30-minute period. Android 9: Each foreground app can scan four times in a 2-minute period. This allows for a burst of scans in a short time.
Updated: In case of your problem, since you are using exactly 30 seconds due to some problem there might be more than 4 time in a 2 minutes period, make it like 35 seconds and test the result.
Original Answer: From this WifiManager startScan throttled in P Beta (Developer Preview 2) :
"Call Limitations - Throttling
We are further limiting the number of scans apps can request to improve network performance and improve battery life.
The WifiManager.startScan() usage is limited to:
It is said that this restriction is due to the battery drain, so it is normal to remove the restriction while charging. Read more about throttling in official documentation.
This is because Android OS enters the infamous "Doze mode". I recommend you to look through the official documentation/explanation.
Unfortunately, there is no way around this. It will also affect any kind of a foreground service and pause it for longer periods. Doze mode will only trigger if the screen is locked and the device is not plugged in.
I encountered the same issue on my project and we implemented a periodic check. If the screen is locked and device not plugged in for a longer period of time, we show a notification asking the user to either plug the device in or open the app so the service could keep working. The implementation is a bit lengthy, but if it is what you decide to go with, I can update the answer with some of the code (using the AlarmManager within a BroadcastReceiver to achieve this).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With