2

I am working on an android application which involves connecting to a specific wifi when in range. Once I am done doing some stuff, I am trying to connect back to my original wifi. For some strange reason, it connects to my original wifi as I want it to, but after about 30 seconds or so, it drops the connection. When I check my wifi settings on the device it shows that wifi as disabled.

To sum up:
1. Connect to Wifi W1.
2. When Wifi W2 is in range, connect to that (using the SSID) but, remember the SSID of W1 for later.
3. Disconnect from W2 and look for W1 in wificonfiguration list (using SSID). When found, connect to that.

All three steps are working, but for some reason, a short while after step 3 succeeds (< 1 minute)the connection to W1 is dropped and disabled by the device. This only happens when I change wifi connections through code. Here's what my code looks like: the 'net' variable contains the SSID value of the original wifi connection's SSID (W1). Does anyone have any idea why I would be dropping connection shortly after reconnecting to the original Wifi?

if(net!=""){
    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.contains(net)) {            
             wifiManager.disconnect();                          

             wifiManager.enableNetwork(i.networkId, true);

             wifiManager.setWifiEnabled(true);                  
             break;
        }           
     }
    }
user_CC
  • 4,686
  • 3
  • 20
  • 15
user_rz_jaz
  • 213
  • 1
  • 3
  • 13
  • when you are performing your test when does w2 comes in range after your device is connected to w1? – user_CC Apr 06 '13 at 14:46
  • yes, w2 comes into range while connected to w1. I disconnect from w1 and connect to w2. Then I shut off w2 and re-establish connection to w1. It connects to w1, but then quickly drops off – user_rz_jaz Apr 06 '13 at 18:05
  • "When Does" means when in time after w1 is connected? – user_CC Apr 06 '13 at 18:13
  • There are a series of steps that are performed in the application. Throughout most of the application, I'm connected to W1. I initiate an action and then that tells the app to start looking for W2. It finds W2 within about 30 seconds or so. This is because W2 takes some time to establish a connection to the device. Keep in mind, W1 is the default connection of the device. So, for this app and everything else that is done on the device it is using W1. Does that answer your question? – user_rz_jaz Apr 06 '13 at 18:59
  • yes it does thank you. Check out my answer below and share any logs if you see any exception. – user_CC Apr 07 '13 at 11:22

1 Answers1

1

I have a suggestion which I am not sure if this is going to solve the problem or not but it would be worth trying

in the below code if you place

if(net!=""){
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.contains(net)) {            
         wifiManager.disconnect();      

         //place a sleep on thread so that the underlying hardware gets some time to disconnect
        Thread.sleep(10000);
        //Now trying connecting to the previous network. Also try increasing the time to wait if 5 seconds is not working.   

         wifiManager.enableNetwork(i.networkId, true);

         wifiManager.setWifiEnabled(true);                  
         break;
    }           
 }
}

EDIT:

It seems to work with the interval more than 10seconds. This time interval is i think dependent on the WiFi Chipset of the device, it needs some time to disconnect from the current network and to be connected to other. Also If the device has a low quality chipset then it might take longer Also I reckon if the device has a high quality chipset then it will be much quicker..

user_CC
  • 4,686
  • 3
  • 20
  • 15
  • This didn't work. I did extend the thread to sleep for 10 seconds and still nothing. This application needs to perform in a quick manner so if I extend it for longer than 10 seconds and it does work that still does not provide me with an acceptable solution. Thanks for the suggestion anyway. I do appreciate it. – user_rz_jaz Apr 08 '13 at 22:41
  • @RZulfekar Atleast it is good to hear that it worked with a timeframe longer than 10 seconds. Please can you let me know on how much time difference does it work? This time interval is dependent on the WiFi Chipset of the device, it needs some time to disconnect from the current network and to be connected to other. Also If the device has a low quality chipset then it might take longer, what device are you using for your testing purposes? Also I reckon if the device has a high quality chipset then it will be much quicker... – user_CC Apr 09 '13 at 08:12
  • Thanks again. I marked it as the answer anyway because it did – user_rz_jaz Apr 09 '13 at 12:21
  • Thank you! I have edited the answer to place a 10 seconds and some more information ..So that it can help someone else in the future. – user_CC Apr 09 '13 at 13:35