You need to create an instance of WifiConfiguration as follows:
String networkSSID = "test"; String networkPass = "pass"; WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\"";
Then, for the WEP network, you need to do the following:
conf.wepKeys[0] = "\"" + networkPass + "\""; conf.wepTxKeyIndex = 0; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
For a WPA network, you need to add this phrase:
conf.preSharedKey = "\""+ networkPass +"\"";
For an open network, you need to do this:
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Then you need to add it to the settings of the Android Wi-Fi manager:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.add(conf);
And finally, you may need to enable it, so Android connects to it:
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) { wm.disconnect(); wm.enableNetwork(i.networkId, true); wm.reconnect(); break; } }
In the case of WEP, if your password is in hexadecimal, you do not need to surround it with quotation marks.
sachin10
source share