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

Asked

Viewed 1,000 times

3

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

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

I wonder if it is possible that my application make my Android connect to a specific network when the user click on it.

It is possible to do this from my application?

  • Allef, I think this question might help you: http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically

1 answer

1


Just call this function by passing the login and password in String more this function works only 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);
    }
}
  • Only lacked a function to list the networks to answer be "perfect", because from what I understand the author wants to list the networks and click on the desired.

  • Thanks! I had already managed using the content of some posts of this site http://www.tutorialspoint.com/

  • Guilherme, if he wants to list can use the Android manager itself, it does not make much sense to recreate it just to select the network. If you want to open the manager just use an Intent contexto.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.