2

I am trying to write some code that will disconnect current wifi network (if any) and reconnect to a specific wifi network with known SSID.

I have been following the code used here; How do I connect to a specific Wi-Fi network in Android programmatically?

which works but the connection takes several seconds, upto about 10 seconds.

Specifically, I use the code as follows;

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config;

I obtain the config, whether by creating a new one and setting the SSID and KeyMgmt to NONE and then adding it;

wifiManager.add(config);

or by getting a config that already exists;

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         config = i;
         break;
    }           
 }

Then I call;

wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();

I have a broadcast received checking the wifi state and when i get a connected for my correct SSID i can continue, however, this process takes upto 10 seconds, how can i set up the config or wifimanager to connect to this much quicker?

Thanks

Community
  • 1
  • 1
Hamid
  • 4,410
  • 10
  • 43
  • 72
  • Did you find the answer for your question? Have you tried turning the dhcp off to improve the time that it takes to connect? I want to do the same thing to quickly consume a service hosted in a different network, I'm thinking mDNS. – Rinaldi Segecin Jan 30 '17 at 06:28
  • @Hamid Does this code work for you, to switch from one network to another ? – Rohit Singh Apr 09 '17 at 07:07
  • Using this code, reconnects to same network again – Rohit Singh Apr 09 '17 at 07:08

2 Answers2

0

I think this code is help to you..

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.network_test);
    context = this;
    mUpdate = new UpdateTimeTask();
    mHandler = new Handler();
    mHandler.post(mUpdate);
    }
public Boolean isNetAvailable(Context con) {
    try{
        connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if(wifiInfo.isConnected() || mobileInfo.isConnected()) {
            return true;
            }
        }catch(Exception e){
            e.printStackTrace();
            }
    return false;
    }
private class UpdateTimeTask implements Runnable{

    public void run() {
        boolean net = isNetAvailable(context);


        if(net != false) {
        Toast.makeText(getBaseContext(), "network Available", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getBaseContext(), "network Not Available", Toast.LENGTH_SHORT).show();
        }
        mHandler.postDelayed(mUpdate, 30000);
    }
}

}

  • Sorry, I don't see how this is relevant to my question. It's not whether or not the internet is connected that I need to know, I can do that already, which is what this code appears to do. I need a way of fast switching the wifi, faster than the current method I'm using which takes a good while to associate. – Hamid Apr 05 '12 at 16:13
-1

Have you tried adding startScan() to your routine to force an immediate rescan for available networks at the time you wish to connect? I imagine forcing that command repeatedly with an alarmManager or something similar is possible, but I would think that has the potential to have an expensive performance/battery impact. If you have a specific trigger, it could be a solution.

See here: http://developer.android.com/reference/android/net/wifi/WifiManager.html#startScan()

mike47
  • 2,217
  • 1
  • 27
  • 50
  • The scanning was not the issue. The question is a little old now, but I believe, though I can't recall, that it was solved by statically setting the IP so that it didn't rely on DHCP to assign one, which took time. – Hamid Feb 24 '14 at 12:00