4

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");
        }
Nils
  • 1,705
  • 6
  • 23
  • 32

5 Answers5

9

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

ngrashia
  • 9,869
  • 5
  • 43
  • 58
xenonite
  • 1,671
  • 4
  • 28
  • 43
  • 1
    This is the right answer. It's the android way, and allows you to almost instantly get fresh data. – Eagle Aug 02 '12 at 15:31
3

Where are you putting this code? In the onCreate of an activity?

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.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • Yes, it was in the onCreate Method ... I moved it now to OnStart, but it's still not working ... it seems the onReceive function is never called ... is there maybe somewhere an error ? I runs fine, but it never exits this loop ... – Nils Jun 06 '10 at 00:51
  • 2
    you wont want to put it in any of the onCreate, onRestart, onStart methods because they all pose the same issue: you will never exit those methods, therefore the callback will never happen. – mtmurdock Jun 06 '10 at 04:31
2

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.

public void onActivityResult(....){
   switch(retCode){
   case SCAN_ACTIVITY:{
         //do stuff
      }
   }
}
mtmurdock
  • 12,756
  • 21
  • 65
  • 108
2

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

Thanks for the help any way. It helped me to improve it a bit :)

Nils
  • 1,705
  • 6
  • 23
  • 32
0

You should write in BroadcastReceiver like this:

  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!
    }
    
ngrashia
  • 9,869
  • 5
  • 43
  • 58
Fang
  • 3,652
  • 4
  • 16
  • 30