Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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");
        }
like image 411
Nils Avatar asked Dec 06 '22 02:12

Nils


1 Answers

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

like image 155
xenonite Avatar answered Dec 21 '22 09:12

xenonite