Code in my current project periodically calls the WifiManager.startScan method and fetches the result via 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 gets called every 2-3 seconds when scanning 2,5Ghz and 5Ghz bands and every ~800ms when scanning the 2,5Ghz band only - this is fine. However, I'm facing an annoying problem. In a nutshell:
- As long as there are APs in range, WifiManager.getScanResults returns up-to-date data
- After leaving that range (e.g. leaving an urban area), the BroadcastReceiver still gets called, but from now on the getScanResults method will always return the last APs that were fetched - even if I am 20 miles away
- As soon as there is at least one new AP in range, I suddenly get valid data again
In other words: getScanResults never returns an emtpy list (except perhabs for some seconds after the app was started). Either there are APs in range and the returned list is up-to-date, or there are no APs in range and the list is out-of-date (and contains the last seen APs).
I'm testing on a Nexus 5 with stock Android (4.4.4), but I'm quite sure the same code worked on a Galaxy Nexus six months ago.
I've got an idea regarding 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 not valid anymore (at least the signal strength of an AP should change between multiple scans). But maybe I'm doing something wrong and there is a simple solution. Any help is appreciated, thanks.