Android 扫描 Wifi 网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2981751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Scan for Wifi networks
提问by Nils
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.
我正在尝试扫描无线网络并在网上找到了这个有用的资源。不幸的是它不起作用,我不知道为什么。我的问题是我不能等待 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");
}
回答by xenonite
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()
...
您需要实现一个 BroadcastReceiver 来监听返回的扫描结果,WifiManager.startScan(). onReceive()
允许您直接访问扫描结果。扫描完成并触发大约需要 1 秒onReceive()
...
回答by JRL
Where are you putting this code? In the onCreate
of an activity?
你把这段代码放在哪里?在onCreate
活动中?
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 docsis 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.
问题是您正在注册一个回调,该回调将在您收到扫描结果时被调用,根据 Android API 文档,这是在一个单独的线程中完成的,因此您的忙等待循环在这种情况下除了不必要地停止之外什么也没有实现您的活动,如果在 期间onCreate
,则意味着它永远不会退出该方法。
回答by Nils
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 ...
这是循环。看起来 onReceive 函数从未被调用,因为活动仅运行此循环。看起来程序必须到达函数的末尾才能执行其他函数,例如 OnReceive ...
Thanks for the help any way. It helped me to improve it a bit :)
感谢您的帮助。它帮助我改进了一点:)
回答by mtmurdock
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.
好吧,我对加快这个过程一无所知,可能只是需要一段时间才能找到 wifi 信号(那个,或者你的 wifi 没有打开......这是你的程序应该在启动之前检查的东西)。但是,您可以做的一件事是使用 startActivityForResult() 在不同的活动中完成所有这些工作以改进您的工作流程。这样,您的“主要”活动将能够在完成后对该数据采取行动,并且您不必在 while 循环中消耗 CPU。
public void onActivityResult(....){
switch(retCode){
case SCAN_ACTIVITY:{
//do stuff
}
}
}
回答by Fang
You should write in BroadcastReceiver
like this:
你应该这样写BroadcastReceiver
:
- Register it
Then startScan and do like this
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { super.onReceive(context, intent); //Scan is ok, just need few seconds! }
- 注册
然后开始扫描并这样做
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { super.onReceive(context, intent); //Scan is ok, just need few seconds! }