在我的Android应用程序中,我正在尝试将我的Android设备连接到“WPA2-PSK”安全连接,经过大量搜索,我编写了以下代码 -
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);
}
上面的代码执行正常(没有任何错误)但不知道为什么,连接没有建立。请帮忙。谢谢。!
答案 0 :(得分:7)
以下所有类型的wifi安全类型的id clear api:
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);
}
这是工作示例