android – How to connect to a specific Wi-Fi network from my app?

Question:

Guys, I'm following the tutorial below to list the Wi-Fi networks available on my Android app.

http://www.tutorialspoint.com/android/android_wi_fi.htm

I would like to know if it is possible for my app to make my Android connect to a specific network when the user clicks on it.

Is it possible to do this from my app?

Answer:

Just call this function passing the login and password in String but this function only works for Wifi WPA_PSK and WPA2_psk

  public void Connection(String ssid, String password){

    WifiConfiguration wfc = new WifiConfiguration();

    wfc.SSID = "\"".concat(ssid).concat("\"");
    wfc.status = WifiConfiguration.Status.DISABLED;
    wfc.priority = 40;

    wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);


    wfc.preSharedKey = "\"".concat(password).concat("\"");

    WifiManager wfMgr = (WifiManager)   mGap.getSystemService(Context.WIFI_SERVICE);
    int networkId = wfMgr.addNetwork(wfc);

    if (networkId != -1) {
        // success, can call wfMgr.enableNetwork(networkId, true) to connect
        wfMgr.enableNetwork(networkId, true);
    }
}
Scroll to Top