如何以及如何设置为Android WifiConfiguration.preSharedKey连接到WPA2 PSK WiFinetworking

在Android 1.5(也在1.6)

如何从代码添加访问点?

给定接入点,支持WPA2。 这是我的代码片段。

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); // This is must be quoted according to the documentation // http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID wc.SSID = "\"SSIDName\""; wc.preSharedKey = "password"; wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); int res = wifi.addNetwork(wc); Log.d("WifiPreference", "add Network returned " + res ); boolean b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b ); 

在LogCat出现时,此代码失败

01-26 16:44:13.550:错误/ wpa_supplicant(2032):第0行:PSK密码无效。

我相信这是密码,其余的参数都是正确的。 我错过了什么?

我的悲伤的原因是在这个文档问题

虽然文件在这里说明

“用于WPA-PSK的预共享密钥,当读取此密钥的值时,不返回实际密钥,如果密钥有值,则返回”*“,否则返回空string。

这是正确的,但非常重要的是它不说的是在这里预期的以太网64位字节散列结果的Linux命令

 wpa_passphrase <ssid> [passphrase] 

或访问点的密码在双引号!

所以如果接入点的PSK是“示例” ,就必须像这样在java中传递

 WifiConfiguration myWiFiConfig = new WifiConfiguration(); ... myWiFiConfig.preSharedKey = "\"example\""; ... 

要么

 myWiFiConfig.preSharedKey = "0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f"; 

对于所有其他你会绊倒这个正确的方法是:

复制并粘贴它,并保存你的自己半天的痛苦,我们已经花了它(特别感谢Reflog )

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\"SSIDName\""; wc.preSharedKey = "\"password\""; wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); int res = wifi.addNetwork(wc); Log.d("WifiPreference", "add Network returned " + res ); boolean b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b ); 

谢谢,所有我可以用你的代码conncet我的wpa psk wifi。

  WifiConfiguration wc = new WifiConfiguration(); // This is must be quoted according to the documentation // http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID wc.SSID = "\"zpoint\""; wc.preSharedKey = "\"sipisP@ssw0rd!\""; wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); int res = wifi.addNetwork(wc); Log.d("WifiPreference", "add Network returned " + res ); boolean b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b ); 

早期,我input错误密码,但后来我正确的密码,然后它的工作。

您将不得不添加波纹pipe线以便:

 wifi.saveConfiguration();