Android的扫描WiFi网络 [英] Android Scan for Wifi networks

查看:131
本文介绍了Android的扫描WiFi网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扫描无线网络,发现网络上的这个有用的来源。 遗憾的是它不工作,我不知道为什么。我的问题是,我不能等待10分钟的结果 - 我需要他们在几秒钟内,想着,只要我得到的结果设置虚假的布尔变量等待....嗯,这将永远运行下去.. 。貌似收到什么。任何想法 ?谢谢你。

  //  - 样品无线网络实现 -  http://groups.google.com/group/android-developers/browse_thread/thread/f722d5f90cfae69
        IntentFilter的I =新的IntentFilter();
        i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
            registerReceiver(新BroadcastReceiver的(){
                @覆盖
                公共无效的onReceive(上下文C,意图我){
                    // code时SCAN_RESULTS_AVAILABLE_ACTION事件发生时执行
                    mWifiManager =(WifiManager)c.getSystemService(Context.WIFI_SERVICE);
                    无线= mWifiManager.getScanResults(); //返回一个<列表> scanResults的
                    等待= FALSE;
                }
            }
        ,一世);
        //  - 完无线样品


        mWifiManager.startScan();


        同时,(等待){
            尝试 {
                视频下载(200);
            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            Log.d(PROJECT1,无线WAITING);
        }
 

解决方案

您需要实现一个BroadcastReceiver监听来自 WifiManager.startScan扫描结果返回()。的onReceive(),您可以直接访问扫描resuls。 约需要1秒扫描完成并引发的onReceive() ...

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

解决方案

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()...

这篇关于Android的扫描WiFi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆