I suppose the OP has found some solutions or workaround. If not and for anyone else (like me) looking for an easy solution, here's what I did:
Declare a global variable: Boolean singleScanMode = false;
Create a method and call it whenever I need to scan for available APs:
public void scanAps() {
WifiManager tempWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// set singleScanMode = true so you can collect scan data
singleScanMode = true;
// add intent filters to enable auto scan
IntentFilter i = new IntentFilter();
i.addAction(tempWifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//ignore scan results unless singleScanMode is set to
if (singleScanMode) {
List<ScanResult> results = tempWifiManager.getScanResults();
Iterator<ScanResult> iterator = results.iterator();
Toast.makeText(context, "Total APs found: "
+ results.size(), Toast.LENGTH_SHORT).show();
while (iterator.hasNext()) {
ScanResult next = iterator.next();
final String bssid = next.BSSID; // MAC address
final String ssid = next.SSID; // AP name
final int rssi = next.level; // Received Signal Strength Indicator
// do anything else you want
} // end while
//set it back to false
singleScanMode = false;
} //end if
}, i);
tempWifiManager.startScan();
}
Note that, Android will report to you Activity whenever wifi is scanned, as long as the Activity is running. But this way you just ignore the scan results. And if anyone else has a better idea please share. Hope it helps!