2
votes

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;
        }           
     }
    }
1
when you are performing your test when does w2 comes in range after your device is connected to w1?user_CC
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 offuser_rz_jaz
"When Does" means when in time after w1 is connected?user_CC
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
yes it does thank you. Check out my answer below and share any logs if you see any exception.user_CC

1 Answers

1
votes

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