Android WiFi Scanning Frustrations

How wifi scanning has changed with the release of new versions of Android and how Google continues to limit developer opportunities

Elvina Sh
ProAndroidDev

--

Image by Gaspart from Dribbble

Hi there! In the Navigine team, we’ve been providing indoor and outdoor positioning mobile technologies that enable advanced indoor navigation and proximity solutions for eight years.

Today we want to tell you more about WiFi scanning on different versions of Android devices and the restrictions you could meet during the development process. In some of our previous articles, we mentioned a few important details of working with a WiFi scan and various restrictions concerning it, but now we decided to combine all this in one place and write a full article describing all the nuances.

Since restrictions and changes are strongly tied to Android versions, we will divide the article into subsections regarding these versions. However, it is also worth noting that we will only consider the versions of Android that are actively used now and will not consider those versions that have long been out of circulation. So let’s start!

Android Oreo and earlier

A couple of years ago, Google still did not pay much attention to working with wifi and did not introduce any restrictions and everything worked the same way as it did many years before. Therefore, the wifi scanning worked without any restrictions and it was possible to scan continuously. Here is the code snippet below which shows how wifi scanning works.

You create the Broadcast Receiver, override its onReceive method, register your receiver with related intent filter and then call startScan on your wifi manager. As you can see, very simple and straightforward code. And it worked just as easily as it looks and without any difficulties.

Let’s make a small digression by mentioning that around the same time Google began to severely limit developers in their capabilities. For example, they limited the ability of services to work in the background, and much more. However, at that moment there were no restrictions on working with WiFi, and people working with this functionality could keep calm. But the worst happened later…

Android Pie and Android 10

At the release of the version of Android 9, Google presented a surprise to all developers. The wifi scan was cropped as much as possible. Now you can start the scan only four times every 120 seconds. If you try to start the scan more times, then all your attempts will be throttled. And then everyone who did not have time to read the documentation about the new changes faced difficulties because their scan worked a couple of times and then disappeared for two minutes.

To put it mildly, this is very unpleasant from the point of view of developers.
Now we had to add if-else construction for scanning evenly during this time. Here is the code snippet above which shows, how we used it in our solution. In total, nothing smart, the scan process works the same way, just now I had to add more checks so that everything worked with a certain pause so as not to catch throttling.

But we did not mention here the main thing that happened with Android 10, Google made deprecated WiFi scanning. At the same time, no new solution appeared and there were no alternatives. We had to wait for a new version of Android.

Android 11

And this new version brought a new solution for WiFi scanning. However, it has not yet been documented, while it will only work in the new version of Android, which means you will have to add even more if-else constructions. Here is the code snippet which shows the methods for the new WiFi scanning process and the explanation of how it should work.

Now you need to register the scan results callback instead of start scan, pass there new ScanResultsCallback class element. You should override its onScanResultsAvailable() method and there you can call the get scan results method (provided in the code snippet). And also there is a method for unregistering scan callback.

Unfortunately, we were unable to test this solution in the absence of a new version of Android and cannot say whether it works as well, whether the old restrictions apply to it, and how much more convenient it is.

Summary

As you can see, Google more and more wants the developers’ code to be replenished with if-else constructions. It’s safe to say that they are trying to limit developers as much as possible from being able to scan wifi devices. But it’s worth noting, there are ways to bypass this, at least during testing, for example, on the Android 10, you can turn off wifi scan throttling. Well, let’s hope that the new scan will be much more convenient and less limited.

--

--

Marketing associate at Navigine.com — Integrated software platform for precise indoor and outdoor positioning.