String BACKSLASH = "\"";
String NETWROK_SECURITY_WEP = "WEP";
String NETWROK_SECURITY_NONE = "NONE";
String NETWROK_SECURITY_WPA = "WPA";
String NETWROK_SECURITY_WPA2 = "WPA2";
String NETWROK_SECURITY_WPA_WPA2 = "WPA/WPA2 PSK";
String NETWROK_ADDITIONAL_SECURITY_TKIP = "TKIP";
String NETWROK_ADDITIONAL_SECURITY_AES = "AES";
String NETWROK_ADDITIONAL_SECURITY_WEP = "WEP";
String NETWROK_ADDITIONAL_SECURITY_NONE = "NONE";
int FAILED_TO_ADD_NETWORK = -1;
WifiConfiguration conf = new WifiConfiguration();
String wifiName = sSid;
conf.SSID = BACKSLASH + wifiName + BACKSLASH;
String securityType = NETWROK_SECURITY_WPA_WPA2;
if (NETWROK_SECURITY_WEP.equalsIgnoreCase(securityType)) {
conf.wepKeys[0] = pass;
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (NETWROK_SECURITY_NONE.equalsIgnoreCase(securityType)) {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if (NETWROK_SECURITY_WPA.equalsIgnoreCase(securityType)
|| NETWROK_SECURITY_WPA2.equalsIgnoreCase(securityType)
|| NETWROK_SECURITY_WPA_WPA2.equalsIgnoreCase(securityType)) {
conf.preSharedKey = BACKSLASH + pass + 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 wlanAdditionalSecurity = "";
if(NETWROK_ADDITIONAL_SECURITY_TKIP.equalsIgnoreCase(wlanAdditionalSecurity)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
} else if
(NETWROK_ADDITIONAL_SECURITY_AES.equalsIgnoreCase(wlanAdditionalSecurity)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
} else if
(NETWROK_ADDITIONAL_SECURITY_WEP.equalsIgnoreCase(wlanAdditionalSecurity)) {
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (NETWROK_ADDITIONAL_SECURITY_NONE.equalsIgnoreCase(wlanAdditionalSecurity)) {
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
}
WifiManager wifiManager = (WifiManager)
mContext.getSystemService(Context.WIFI_SERVICE);
int res = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.reconnect();
if (true) {
wifiManager.enableNetwork(res, true);
wifiManager.saveConfiguration();
wifiManager.setWifiEnabled(true);
new AppPreferences(mContext).setPrefrenceLong("connectTime",
Calendar.getInstance().getTimeInMillis());
}
if (res != -1) {
setFalseOther(mm);
notifyDataSetChanged();
Intent i = new Intent(mContext, Connect.class);
i.putExtra("networkName", mm.getName());
i.putExtra("networkId", mm.getId());
i.putExtra("AdminID", mm.getUserId());
i.putExtra("networkConnection", "true");
mContext.startActivity(i);
}
This code works in android lollipop but not for marshmallow .unable to remove saved passwords before connecting to next Wifi network in marshmallow
在 android M 中以编程方式更改 wifi 配置的开发?删除保存的 wifiaswords 不工作
最佳答案
如果您尝试根据此文档使用任何危险权限 https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous在 android-M 上。您需要在运行时请求权限才能在 android-M 上使用它们。使用此链接执行此操作:https://developer.android.com/training/permissions/requesting.html
关于android - 在 android M 中以编程方式更改 wifi 配置的开发?删除保存的 wifiaswords 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344673/