的Wi-Fi扫描没有广播接收器? [英] Wi-Fi scanning without broadcast receiver?

查看:266
本文介绍了的Wi-Fi扫描没有广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的Wi-Fi扫描仪。它会不断扫描可用的Wi-Fi网络。但我的问题是,如果我可以实际运行的扫描需要为什么会正是广播接收机(调用 startScan()每x秒计时器),并不会产生广播接收器收到相同的结果?

I have created wi-fi scanner. It continually scans for available wi-fi networks. But my question is why would exactly broadcast receiver is needed if i can actually run scanning (invoke startScan() with timer every x seconds) and receive the same results without creating broadcast receiver?

这是广播接收器code在的onCreate()方法:

This is broadcast receiver code in onCreate() method:

i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
receiver = new BroadcastReceiver(){
    public void onReceive(Context c, Intent i){
      WifiManager w = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
      List<ScanResult> l = w.getScanResults();
      for (ScanResult r : l) {
         // do smth with results
      }
      // log results
    }
};

在扫描方法,它扫描按钮后调用pressed我有:

In scanning method, which is invoked after scan button is pressed I have:

timer = new Timer(true);
timer.schedule(new WifiTimerTask(), 0, scanningInterval);
registerReceiver(receiver, i );

其中, WifiTimerTask

publlic class WifiTimerTask extends TimerTask{
    @Override
     public void run(){
         wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
         if (wifi.isWifiEnabled()) {
            wifi.startScan();
            List<ScanResult> sc = wifi.getScanResults(); 
            for (ScanResult r : l) {
               // do smth with results
            }
           // log results
          }
     }
}

问题关键是,可以在不 registerReceiver进行了扫描(接收器,I)。但是,只有当 scanningInterval 大于2秒时,那么接收 scanresults和 startScan()不同步。我的意思是 startScan()结果不会改变,直到接收将得到新的结果。与此同时,在logcat中,我得到错误/参考wpa_supplicant(5837)进行的扫描动作... 。好像是2秒,虽然最低扫描间隔。请纠正我,如果我的假设是错误的。

And the point is that scanning can be performed without registerReceiver(receiver,i). However only if scanningInterval is lower than 2s, then receiver scanresults and startScan() are not synchronized. By that I mean startScan() results do not change until receiver will get new results. Meanwhile in logCat I get ERROR/wpa_supplicant(5837): Ongoing Scan action.... Seems like 2s is the lowest scanning interval though. Please correct me if my assumptions are wrong.

推荐答案

当你调用 startScan()你不知道实际的扫描需要多长时间(一般来说也可以是1毫秒或5小时)。所以,你不能可靠地调用 getScanResults()为完成时,扫描的时候,你不知道。

When you call startScan() you don't know how long the actual scan will take (generally speaking it can be 1 ms or 5 hours). So you cannot reliably call getScanResults() as you don't know when the scan is completed.

要跟踪时,当 getScanResults()将返回更新扫描的事件结果,你需要订阅 SCAN_RESULTS_AVAILABLE_ACTION

To track the event when when getScanResults() will return update scan results you need to subscribe to SCAN_RESULTS_AVAILABLE_ACTION.

这篇关于的Wi-Fi扫描没有广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆