简体   繁体   English

Android扫描Wifi网络

[英]Android Scan for Wifi networks

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. 我的问题是,我不能等待10分钟的结果 - 我需要它们在几秒钟内,并考虑设置布尔变量等待假一旦得到结果....好吧,它运行永远..看起来没什么收到的。 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");
        }

you need to implement a BroadcastReceiver listening for the scan results returned from WifiManager.startScan(). onReceive() 您需要实现BroadcastReceiver侦听从WifiManager.startScan(). onReceive()返回的扫描结果WifiManager.startScan(). onReceive() WifiManager.startScan(). onReceive() allows you to access the scan resuls directly. WifiManager.startScan(). onReceive()允许您直接访问扫描结果。 it takes about 1 second for the scan to complete and trigger onReceive() ... 扫描完成需要大约1秒钟并触发onReceive() ...

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 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. 问题是你正在注册一个回调函数,当你收到扫描结果时会调用它, 根据Android API文档是在一个单独的线程中完成的,所以你的繁忙等待循环在这种情况下什么也没有实现,除了不必要的停止你的活动,如果是在onCreate期间,这意味着它永远不会退出该方法。

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). 好吧,我不知道有关加快这个过程的任何事情,它可能只是需要一段时间才能找到wifi信号(或者你的wifi没有开启......这是你的程序在开始之前应该检查的东西)。 However, one thing you can do to improve your workflow would be to do all of this in a different activity using startActivityForResult(). 但是,您可以做的一件事就是使用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. 这样,你的“主要”活动将能够在完成后对该数据进行操作,并且你不必在while循环中吃掉cpu。

public void onActivityResult(....){
   switch(retCode){
   case SCAN_ACTIVITY:{
         //do stuff
      }
   }
}

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. 看起来onReceive函数永远不会被调用,因为活动只运行此循环。 Looks like the program has to reach the end of the function to execute other function like OnReceive ... 看起来程序必须到达函数的末尾才能执行OnReceive等其他函数...

Thanks for the help any way. 非常感谢您的帮助。 It helped me to improve it a bit :) 它帮助我改善了一点:)

You should write in BroadcastReceiver like this: 你应该在BroadcastReceiver写这样的:

  1. Register it 注册它
  2. 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! } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM