How do I connect to a specific Wi-Fi network from my application?

3

Now, I'm following the tutorial below to list the Wi-Fi networks available in my Android application.

  

link

I wonder if it's possible for my app to have my Android connect to a specific network when the user clicks on it.

Is it possible to do this from my application?

    
asked by anonymous 30.03.2015 / 15:40

1 answer

1

Just call this function by passing the login and password into String plus 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);
    }
}
    
06.04.2015 / 04:26