• How to Avoid WiFi Throttling on Android Devices
  • Elvina Sh
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: regon – cao
  • Proofread by zenblo NieZhuZhu

Today I want to talk about the WiFi throttling in the new version of Android and how to avoid it. I wrote an article about the scanning mechanism in the recently released version of Android R. If you read this article, you’ll know that Google has limited how often developers can invoke WiFi scanning on the new version of Android compared to previous versions.

We’ll start with a look at how WiFi scanning works in the past, and then explain how it works on the Android R version.

What we did before

The previous approach was simple and had no throttling restrictions, including Android Oreo, which could be scanned every second without limitation. The following simple code explains how it worked before:

mWifiBroadcastReceiver = new BroadcastReceiver()
{
  @Override public void onReceive(Context c, Intent intent)
  {
    try
    {
      // Write your code
    }
    catch (Throwable e)
    {
      Logger.d(TAG, 1, Log.getStackTraceString(e)); }}}; mContext.registerReceiver(mWifiBroadcastReceiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Copy the code

All you need to do is register a broadcast receiver in the context to get and parse the scan results, then call the Start Scan method on the WiFi manager, and you’re done. Android at this point allows 10 WiFi scans per second. You can imagine the potential that opens up for high-precision navigation.

After Google introduced throttling for WiFi scanning, things went awry.

Android 9 and 10

With the release of Android 9, Google surprised all developers. WiFi scanning is limited as much as possible, now only 4 WiFi scans per 120 seconds.

In a previous article, I described a solution for equally spaced scanning and WiFi RTT work, but it’s worth noting that it doesn’t completely avoid throttling.

if(! mWifiScanning && timeNow > mWifiLastTime + WIFI_SLEEP_TIMEOUT) {if(mWifiBroadcastReceiver ! =null && !mWifiRegistered)
  {
    mContext.registerReceiver(mWifiBroadcastReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    mWifiRegistered = true;
  }

  if (mWifiManager.startScan())
  {
    mWifiTime = timeNow;

    try
    {
      mContext.unregisterReceiver(mWifiBroadcastReceiver);
      mWifiRegistered = false;
    } catch (Throwable e) { }
  }
}
Copy the code

In Android 10, by the way, Google offers a solution. In developer mode they provide a button to turn off WiFi throttling, so you can turn it off and not be restricted by throttling. But if you write a universal solution for all devices, you don’t know in advance whether the device has throttling turned off. The main problem is that some custom operating systems (huawei, Xiaomi, etc.) don’t offer the option to turn throttling off.

Android R solution

In Android R, released a few weeks ago, Google introduced a new scanning mechanism that was deprecated in Android 9. Now we just need to add a method that checks if throttling is turned off:

.if(! mWifiManager.isScanThrottleEnabled()) {// Scanning is no longer restricted}..Copy the code

Once throttling is determined to be off we can call the scan without restriction. In the meantime, this approach only works if you test your application locally. For apps released through the app marketplace or other platforms, the user experience is terrible because they need to turn on the device’s debug mode and find out where the switch is.

conclusion

Google limits the frequency of WiFi scanning, but gives us the option to turn it off. They seem to be aware of the decision and are trying to fix it. However, the move still caused a lot of trouble for developers and users.

On the other hand, you can’t call WiFi scanning on iOS, so we should be grateful for what we have.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.