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

2022-09-02 11:25:34

在 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“密码”。

我确信这是密码,并且所有其他参数都是正确的。我错过了什么?


答案 1

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

虽然这里的文档指出

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

这是正确的,但非常重要的一点是,它没有说的是,期望这里以太64字节的linux命令的哈希结果

wpa_passphrase <ssid> [passphrase] 

或接入点的密码用双引号括起来!

因此,如果ApcesPoint的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 );

答案 2

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

早些时候,我输入了错误密码,但后来我纠正了密码,然后它起作用了。


推荐