Sayaki
Sayaki

Reputation: 789

WiFi scan on Android

I have an app that is scanning Wi-Fi networks. When one Wi-Fi scan is finished I’m starting new one. I’m testing this on Pixel3 and SamsungS10 phones. On both phones I disabled Wi-Fi scan throttling from developer options. I do scans in the office where is a lot of different Wi-Fi networks. The problem that I’m observing, is that on Pixel3 Wi-Fi scans are working just fine for some time, then they start to returning scan errors for like a dozen consecutive scans and after that scans are working just fine again. This is happening periodically. The issue does not occur on SamsungS10 phones at all. Here is how I’m doing the scan:

wifiManager.startScan();

BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context c, Intent intent) {
    boolean success = intent.getBooleanExtra(
                       WifiManager.EXTRA_RESULTS_UPDATED, false);
    if (success) {
      scanSuccess();
    } else {
      scanFailure();
    }

    wifiManager.startScan();
  }
}; 

Any idea why Wi-Fi scan are working much worse on Pixel3 phones? Is there any way to get exact Wi-Fi scan error code / error message instead of simple boolean from boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false); ?

Upvotes: 2

Views: 2816

Answers (1)

BierDav
BierDav

Reputation: 1404

This Page (https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-process) contains this peace of code:

private void scanFailure() {
  // handle failure: new scan did NOT succeed
  // consider using old scan results: these are the OLD results!
  List<ScanResult> results = wifiManager.getScanResults();
  //... potentially use older scan results ...
}

I believe that Google's OS developers, in order to get a better battery life, simply prevent an app from doing a "wifi scan" all the time. And as the comments in the code describes, in case of an error you should simply use the old results. BierDav

Upvotes: 5

Related Questions