Android 扫描 Wifi 网络
我正在尝试扫描无线网络并在网上找到了这个有用的资源。 不幸的是它不起作用,我不知道为什么。我的问题是,我不能等待 10 分钟才能得到结果 - 我在几秒钟内需要它们,并考虑在得到结果后立即将布尔变量设置为 false......好吧,它会永远运行......看起来什么也没收到。有什么想法吗?谢谢。
// -- Sample WiFi implementation - http://groups.google.com/group/android-developers/browse_thread/thread/f722d5f90cfae69
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context c, Intent i){
// Code to execute when SCAN_RESULTS_AVAILABLE_ACTION event occurs
mWifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
wireless = mWifiManager.getScanResults(); // Returns a <list> of scanResults
waiting = false;
}
}
,i);
// -- End Wifi Sample
mWifiManager.startScan();
while (waiting) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("PROJECT1","Wifi WAITING");
}
I'm trying to scan for wireless networks and found this helpful source on the net.
Unfortunately it's not working and I have no idea why. My problem is that I can't wait 10 minutes for the result - I need them within a few seconds and thought about setting the boolean variable waiting on false as soon as I get a result.... well, it runs forever ... looks like nothing is received. Any idea ? Thanks.
// -- Sample WiFi implementation - http://groups.google.com/group/android-developers/browse_thread/thread/f722d5f90cfae69
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context c, Intent i){
// Code to execute when SCAN_RESULTS_AVAILABLE_ACTION event occurs
mWifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
wireless = mWifiManager.getScanResults(); // Returns a <list> of scanResults
waiting = false;
}
}
,i);
// -- End Wifi Sample
mWifiManager.startScan();
while (waiting) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("PROJECT1","Wifi WAITING");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要实现一个 BroadcastReceiver 来监听从 WifiManager.startScan() 返回的扫描结果。 onReceive() 允许您直接访问扫描结果。
扫描完成并触发
onReceive()
大约需要 1 秒...you need to implement a BroadcastReceiver listening for the scan results returned from
WifiManager.startScan(). onReceive()
allows you to access the scan resuls directly.it takes about 1 second for the scan to complete and trigger
onReceive()
...你把这段代码放在哪里?在 Activity 的
onCreate
中?问题是您正在注册一个回调,当您收到扫描结果时将调用该回调,该回调 根据 Android API 文档 是在单独的线程中完成的,因此在这种情况下,除了不必要地停止您的活动之外,您的忙等待循环不会实现任何目标,并且如果它是在
onCreate 期间
这意味着它永远不会退出该方法。Where are you putting this code? In the
onCreate
of an activity?The problem is that you're registering a callback which will get called when you receive the scan results, which according to the Android API docs is done in a separate thread, so your busy-waiting loop is achieving nothing in this circumstance except needlessly halting your activity, and if it's during the
onCreate
that means it never exits the method.好吧,我不知道有关加快该过程的任何信息,可能只是需要一段时间才能找到 wifi 信号(或者您的 wifi 未打开......这是您的程序在启动之前应该检查的内容) )。但是,为了改进工作流程,您可以做的一件事是使用 startActivityForResult() 在不同的活动中完成所有这些操作。这样,您的“主要”活动将能够在完成后对这些数据采取行动,并且您不必在 while 循环中耗尽 CPU。
Well i dont know anything about speeding up the process, it could just be that it takes a while to find the wifi signals (that, or your wifi is not turned on... which is something that your program should check for before it starts). However, one thing you can do to improve your workflow would be to do all of this in a different activity using startActivityForResult(). That way your "main" activity will be able to act on that data after it's done and you wont have to eat up the cpu on a while loop.
好吧,我发现错误了。
这是循环。看起来 onReceive 函数永远不会被调用,因为活动仅运行此循环。看起来程序必须到达函数末尾才能执行其他函数,例如 OnReceive ...
感谢您的帮助。它帮助我改进了一点:)
Ok, I found the mistake.
It was the loop. It looks like the onReceive function is never called as the activity run this loop only. Looks like the program has to reach the end of the function to execute other function like OnReceive ...
Thanks for the help any way. It helped me to improve it a bit :)
你应该在
BroadcastReceiver
中这样写:然后开始扫描并这样做
You should write in
BroadcastReceiver
like this:Then startScan and do like this