Question

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;
        }           
     }
    }
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top