WifiManager.getScanResults returns old results if APs are not available

The code in my current project periodically calls the WifiManager.startScan method and retrieves the result via the BroadcastReceiver:

void setupWifiScanner() {
    final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    final IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    registerReceiver(new BroadcastReceiver(){
        public void onReceive(Context c, Intent i){
            scanResultHandler(wifiManager.getScanResults());
            wifiManager.startScan();
        }
    }, filter);
    wifiManager.startScan();
}

      

The BroadcastReceiver is called every 2-3 seconds when scanning the 2.5GHz and 5GHz bands, and every ~ 800ms when scanning only the 2.5GHz band - that's fine. However, I ran into an annoying problem. In a nutshell:

  • As long as there are APs in range, WifiManager.getScanResults returns updated data
  • After going out of this range (for example, leaving a city area) the BroadcastReceiver is still called, but from now on, the getScanResults method will always return the last APs that were selected, even if I am 20 miles away.
  • As soon as there is at least one new access point in the range, I get valid data again

In other words: getScanResults never returns an emtpy list (except for perhabs for a few seconds after starting the application). Either there are APs in range and the returned list is updated, or there are no APs in range and the list is out of date (and contains the last APs seen).

I'm testing a Nexus 5 with Android Android (4.4.4), but I'm pretty sure the same code worked on a Galaxy Nexus six months ago.

I have an idea for a workaround - just hashing the result, and if the same hash occurs x times in a row, I think it's safe to say that the data is no longer valid (at least the signal strength of the access point should change between multiple scans). But maybe I am doing something wrong and there is a simple solution. Any help is appreciated, thanks.

+3


source to share





All Articles