没有广播接收器的 Wi-Fi 扫描?
我已经创建了无线扫描仪。它不断扫描可用的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
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 callgetScanResults()
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 toSCAN_RESULTS_AVAILABLE_ACTION
.