Sunday, March 25, 2012

[android-developers] Wifi manager woes

Based on some events, I'm trying to switch on or off wifi.
 
 
Switching wifi off works well. But while switching on wifi, 95% of the time, it can't reconnect to my previous wifi network.
If I goto the settings -> wifi, then I see that it's trying to connect, and after a while it'll disconnect.
But wifi service is on.
 
Sometimes 'reconnect' call returns false.
 
 
I went to the extent of waiting until the wifi service is switched on, before reconnecting.
 
Any idea as to how I can reconnect to my previous (or any) wifi network.
 
 
onEvent_SwitchOn()
{
 WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
android.text.format.Time cTime = new android.text.format.Time();
     android.text.format.Time nTime = new android.text.format.Time();
           
 if(!wifiManager.setWifiEnabled(true)) {
  Log.e("Err", "setWifiEnabled - true, failed");
 }
 
// wait for about 5 seconds until wifi service is actually switched on
cTime.setToNow();
     nTime.setToNow();
     while(true)
     {
      long diff = nTime.toMillis(true) - cTime.toMillis(true);
      if(diff > 5000)
      {
       break;
      }
      int  state = wifiManager.getWifiState();
      
      if(wifiManager.WIFI_STATE_ENABLED == state)
      {
       break;
      }
      
      try {
       Thread.currentThread().sleep(100);
      } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      nTime.setToNow(); // update to the latest time....
     }
 

if(!wifiManager.reconnect()) {
      Log.e("Err", "Wifi manager (reconnect) failed");
     }

}

 
}
 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate