I'm trying to set up a wifi scan every 1 second, since the current 6 second delay between the scan results is too long for me. I think that somehow it works, but I barely understand how it works, or the result. Here is the basic code telling the timer to run wifiMgr.startScan();every 1000 ms.
private void startNetworkScan() {
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
private void TimerMethod() {
this.runOnUiThread(rTimer);
}
private Runnable rTimer = new Runnable() {
public void run() {
wifiMgr.startScan();
Bundle bb = wifiScanReceiver.getResultExtras(true);
txtList.append("Result " + bb.getString("scanresult") + "\n");
}
};
Below is the code for the BroadcastReceiver class. I just show the timestamp and related information from the access point.
private void handleScanResultsAvailable() {
List<ScanResult> results = wifiMgr.getScanResults();
String currentTimeStr = new SimpleDateFormat("HH:mm:ss").format(new Date());
Bundle b = new Bundle();
StringBuilder sb = new StringBuilder();
sb.append(i + ": " + currentTimeStr);
for (ScanResult result : results) {
sb.append(String.format(" SSID: %s, RSSI: %s dBm ", result.SSID, result.level));
}
b.putString("scanresult", sb.toString());
setResultExtras(b);
i++;
}
And here is a snippet of the result:
Result 1: 10:04:12 SSID: XXXXX, RSSI: -88 dBm
Result 1: 10:04:12 SSID: XXXXX, RSSI: -88 dBm
Result 2: 10:04:14 SSID: XXXXX, RSSI: -87 dBm
Result 2: 10:04:14 SSID: XXXXX, RSSI: -87 dBm
1-2 , . , . , , wifiMgr.startScan() , :
// there are ca. 6-7 lines for every record
// as the wifi scanresult delay is 6 secs
Result 1: 10:03:40 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:41 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:42 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:43 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:44 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:45 SSID: XXXXX, RSSI: -85 dBm
Result 2: 10:03:45 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:46 SSID: XXXXX, RSSI: -85 dBm
Result 2: 10:03:46 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:47 SSID: XXXXX, RSSI: -85 dBm
Result 2: 10:03:47 SSID: XXXXX, RSSI: -85 dBm
Result 1: 10:03:48 SSID: XXXXX, RSSI: -85 dBm
Result 2: 10:03:48 SSID: XXXXX, RSSI: -85 dBm
- ? - , . , .
. BroadcastReceiver.
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
handleScanResultsAvailable();
}
}