没有广播接收器的 Wi-Fi 扫描?

发布于 2024-11-15 02:39:34 字数 1612 浏览 6 评论 0原文

我已经创建了无线扫描仪。它不断扫描可用的 Wi-Fi 网络。但我的问题是,如果我实际上可以运行扫描(每 x 秒使用计时器调用 startScan())并在不创建广播接收器的情况下接收相同的结果,为什么还需要广播接收器?

这是 onCreate() 方法中的广播接收器代码:

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
    }
};

在扫描方法中,按下扫描按钮后调用该方法我有:

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(receiver,i) 即可执行。但是,只有当 scanningInterval 小于 2 秒时,receiver 扫描结果和 startScan() 才会不同步。我的意思是 startScan() 结果不会改变,直到 receiver 获得新结果。同时,在 logCat 中,我收到 ERROR/wpa_supplicant(5837): Ongoing Scan action...。看起来 2s 是最低的扫描间隔。如果我的假设有误,请纠正我。

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?

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
    }
};

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 );

where WifiTimerTask is

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2024-11-22 02:39:34

当您调用 startScan() 时,您不知道实际扫描需要多长时间(一般来说可以是 1 毫秒或 5 小时)。因此,您无法可靠地调用 getScanResults(),因为您不知道扫描何时完成。

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文