简体   繁体   中英

unexpected result using timer for wifi scan android

I'm trying to schedule a wifi scan every 1 second as the current 6 secs delay between each scan result is too long for me. I think somehow it works, but I barely understand either the way it works or the result. Here is the basic code telling the timer to start 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 on the BroadcastReceiver class. I just display the timestamp and related info 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 

Mostly each result is displayed every 1-2 seconds, which is quite good. What I don't understand is, for each result, I always get two lines with the same value. I actually expect that there should be some overlapping results because wifiMgr.startScan() is not stopped at every execution by the timer, like this:

// 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 

Can anyone shed some light? - is the code right, is the result fine. Would appreciate any explanation on the logic behind it.

Update : Here is the rest of the BroadcastReceiver code.

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equalsIgnoreCase(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
        handleScanResultsAvailable();
    }
}

Your sb is initialised outside your for loop, so that if results contained more than one result, it would produce output like:

Result 1: 10:04:12 SSID: XXXXX, RSSI: -88 dBm  SSID: XXXXX, RSSI: -85 dBm 

Therefore the duplicate line output is probably in the code which outputs the results.

In your rTimer run code, you call wifiMgr.startScan() to start the scan but that will return immediately so the next line won't get the data you expect.

Presumably your receiver receives android.net.wifi.SCAN_RESULTS. It should check for this and output the results there.

Basically your data requests and data returns are not synchronised. You ask for the data but try to display it before it is returned

Can you put some logging in to see what's happening? Before wifiMgr.startScan():

/*DEBUG*/Log.d(this.getClass().getName(), "run: Started");

and at the start of handleScanResultsAvailable:

/*DEBUG*/Log.d(this.getClass().getName(), "handleScanResultsAvailable: Started");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM