How To Connect Android Device To "wpa2 Psk" Secured Wifi Hotspot Network Programmatically?
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
Solution 1:
Below id clear api for all type of wifi security-type :
publicvoidaddWifiConfig(String ssid,String password,String securityParam,String securityDetailParam) {
Log.d(TAG, "Inside addWifiConfig...");
if (ssid == null) {
thrownew 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 quotesif (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);
} elseif (security
.equalsIgnoreCase("NONE")) {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} elseif ("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);
} elseif (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_AES)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
} elseif (securityDetails
.equalsIgnoreCase(Constants.NETWROK_ADDITIONAL_SECURITY_WEP)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} elseif (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
Post a Comment for "How To Connect Android Device To "wpa2 Psk" Secured Wifi Hotspot Network Programmatically?"