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);
} else {
}
此代码用于连接到WiFi。它在Android棒棒糖上工作正常,但我想以编程方式为棉花糖设置配置。我给了所有的运行时间权限和危险的权限,但仍然没有帮助,无法删除保存的WiFi密码。如果有人为棉花糖做了这个,这将是有帮助的。通过编程方式连接到Wifi不适用于Android Marshmallowow?
韦恩感谢您的答复。其实我的代码是不能工作在棉花糖。每当我试图通过给SSID,它仍然有连接到前一个密码才能连接到其他网络添加权限。您可以在这里分享您的POC,这将有所帮助。 –
也无论何时我试图连接到其他网络我的应用程序卡住了,我得到了这个问题“无法chmod(/ sdcard/appName):android.system.ErrnoException:chmod失败:EPERM(操作不允许)”.. –