简体   繁体   English

使用计时器进行wifi扫描android的意外结果

[英]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. 我试图每隔1秒安排一次wifi扫描,因为每次扫描结果之间的当前6秒延迟对我来说太长了。 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(); 这是告诉计时器启动wifiMgr.startScan();的基本代码wifiMgr.startScan(); every 1000 ms. 每1000毫秒。

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. 下面是BroadcastReceiver类的代码。 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. 大多数情况下,每1-2秒显示一次结果,这非常好。 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: 我实际上期望应该有一些重叠的结果,因为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 

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. 更新 :这是BroadcastReceiver代码的其余部分。

@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: 你的sb在你的for循环之外初始化,所以如果结果包含多个结果,它将产生如下输出:

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. 在你的rTimer运行代码中,你调用wifiMgr.startScan()来启动扫描但是会立即返回,所以下一行不会获得你期望的数据。

Presumably your receiver receives android.net.wifi.SCAN_RESULTS. 大概你的接收器收到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(): 在wifiMgr.startScan()之前:

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

and at the start of handleScanResultsAvailable: 并且在handleScanResultsAvailable的开头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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