如何以及如何设置Android WifiConfiguration.preSharedKey以连接到WPA2 PSK WiFi网络

时间:2010-01-26 14:52:18

标签: java android wifi

在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:ERROR / wpa_supplicant(2032):第0行:PSK'密码'无效。

我确信这是密码,所有其他参数都是正确的。 我想念我怎么办?

3 个答案:

答案 0 :(得分:45)

我悲伤的原因在于in this Documentation issue

虽然documentation here

  

“与WPA-PSK一起使用的预共享密钥。   读取此键的值时,不返回实际键,只返回a   如果键有值,则为“*”,否则为空字符串。“

这是正确的,但非常重要的是不说是这里预期的linux 64字节哈希结果的linux 命令

wpa_passphrase <ssid> [passphrase] 

或接入点的密码在双引号中

因此,如果Access Point的PSK是 “example” ,则必须在java中传递 像这样

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

OR

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 );

答案 1 :(得分:4)

谢谢,我可以用你的代码连接到我的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 );

很早,我输入了错误密码,但后来我更正了密码,然后就可以了。

答案 2 :(得分:3)

您必须添加以下行才能:

wifi.saveConfiguration();