Android Scan for Wi-Fi networks

I am trying to check wireless networks and find this useful source on the network. Unfortunately, it does not work, and I have no idea why. My problem is that I can’t wait 10 minutes for the result - I need them for a few seconds and thought about setting a boolean variable that expects false as soon as I get the result ... well, it works forever. .nothing seems to be received. Any ideas? Thank.

// -- 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");
        }
+5
source share
5 answers

BroadcastReceiver, , WifiManager.startScan(). onReceive(), . 1 onReceive()...

+10

? onCreate ?

, , , API Android API , , , onCreate , .

+4

, , , Wi-Fi- ( Wi-Fi ... -, ). , , , , - , startActivityForResult(). , "" , while.

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

, .

. , onReceive , . , , OnReceive...

Thanks for the help anyway. It helped me improve it a bit :)

+3
source

You should write in the BroadcastReceiverfollowing way:

  • Register it
  • Then run Scan and do this

    if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
    
    super.onReceive(context, intent);           
    //Scan is ok, just need few seconds!
    }
    
0
source

All Articles