尝试以编程方式从Android连接到我的无线网络。
安全类型是WPA2,加密AES。
这不能按预期工作:
private WifiConfiguration saveWepConfig(String password, String networkSSID) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
// conf.wepKeys[0] = "\"" + password + "\"";
conf.preSharedKey = "\"" + password + "\"";
conf.wepTxKeyIndex = 0;
conf.hiddenSSID = true;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
return conf;
}
我必须在这里加密密码吗?它只保存连接,不连接。
答案 0 :(得分:6)
啊......我在发布问题后立即找到了解决方案:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\"" + password + "\"";
// conf.hiddenSSID = true;
// conf.wepTxKeyIndex = 0;
// conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
// conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
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);
它对我有用。 感谢。
答案 1 :(得分:5)
您只需要设置SSID
和preSharedKey
。其他一切都默认为WPA / WPA2(可能不适用于真正的旧版Android)。
public static void saveWpaConfig(Context context, String ssid, String passphrase)
{
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + ssid + "\"";
wifiConfiguration.preSharedKey = "\"" + passphrase + "\"";
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.addNetwork(wifiConfiguration);
if (networkId != -1)
{
wifiManager.enableNetwork(networkId, true);
// Use this to permanently save this network
// Otherwise, it will disappear after a reboot
wifiManager.saveConfiguration();
}
}