In my Android app I'm trying to connect my Android device to "WPA2-PSK" secured connection and after lot of search I have written following code--
if (securityMode.equalsIgnoreCase("WPA2")) // WPA2
{
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiManager.setWifiEnabled(true);
wifiManager.saveConfiguration();
int added = wifiManager.addNetwork(wifiConfiguration);
System.out.println("added network: " + added);
boolean enabled = wifiManager.enableNetwork(added, true);
System.out.println("enableNetwork returned: " + enabled);
}
Above code is executing fine (without any error) but don't know why, connection is not getting establish. Please help. Thank you.!
Below id clear api for all type of wifi security-type :
public void addWifiConfig(String ssid,String password,String securityParam,String securityDetailParam) {
Log.d(TAG, "Inside addWifiConfig...");
if (ssid == null) {
throw new IllegalArgumentException(
"Required parameters can not be NULL #");
}
String wifiName = ssid;
WifiConfiguration conf = new WifiConfiguration();
// On devices with version Kitkat and below, We need to send SSID name
// with double quotes. On devices with version Lollipop, We need to send
// SSID name without double quotes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
conf.SSID = wifiName;
} else {
conf.SSID = Constants.BACKSLASH + wifiName + Constants.BACKSLASH;
}
String security = securityParam;
Log.d(TAG, "Security Type :: " + security);
if (security.equalsIgnoreCase("WEP")) {
conf.wepKeys[0] = password;
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (security
.equalsIgnoreCase("NONE")) {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if ("WPA"
.equalsIgnoreCase(security)
|| "WPA2"
.equalsIgnoreCase(security)
|| "WPA/WPA2 PSK"
.equalsIgnoreCase(security)) {
// appropriate ciper is need to set according to security type used,
// ifcase of not added it will not be able to connect
conf.preSharedKey = Constants.BACKSLASH
+ password + Constants.BACKSLASH;
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
}
String securityDetails = securityDetailParam;
if (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_TKIP)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
} else if (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_AES)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
} else if (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_WEP)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_NONE)) {
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.NONE);
}
WifiManager wifiManager = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
int newNetworkId = wifiManager.addNetwork(conf);
wifiManager.enableNetwork(newNetworkId, true);
wifiManager.saveConfiguration();
wifiManager.setWifiEnabled(true);
}
This is working example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With