如何和什么设置到Android WifiConfiguration。preSharedKey连接到WPA2 PSK WiFi网络连接到、网络、preSharedKey、WifiConfiguratio

2023-09-12 10:49:22 作者:做个废材继续可爱

在Android 1.5的(也1.6)

In Android 1.5 (also on 1.6)

如何从code添加一个接入点?

How to add an Access Point from code?

由于支持WPA2接入点。 这是我的code段。

Given Access point that supports WPA2. Here is my code snippet.

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

这code未能在LogCat中出现

This code fails as in LogCat appear

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

01-26 16:44:13.550: ERROR/wpa_supplicant(2032): Line 0: Invalid PSK 'password'.

我相信,这是密码,所有参数的其余部分是正确的。 我该怎么办我错过?

I am sure that this is the password and that all of the rest of the parameters are right. What do I do I miss?

推荐答案

究其原因,我的悲哀就在这里的在这个文档问题

The reason for the my sorrow is here in this Documentation issue

在documentation这里状态

pre-共享密钥与WPA-PSK使用。   按这个键的值被读取时,不会返回实际的键,只需一   *如果该键的值,或空字符串,否则。

"Pre-shared key for use with WPA-PSK. When the value of this key is read, the actual key is not returned, just a "*" if the key has a value, or the null string otherwise."

这是正确的,但很重要的是它的不说什么的是,预计这里的linux醚64字节的散列结果 命令

It is correct, but very important what it does not say is that expected here ether 64 byte hash result of the linux command

wpa_passphrase <ssid> [passphrase] 

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

所以,如果该接入点的PSK是 榜样的它在Java中传递 像这样

So in case that Access Point's PSK is "example" it has to be passed in java like this

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

myWiFiConfig.preSharedKey = "0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";

有关你所有的休息,将栽倒在这个正确的方法是:

For all the rest of you that will stumble on this the right way is:

复制和,贴吧的是与痛苦半天,我们已经在它(花保存你自己特别感谢引用日志)

Copy&Paste it as is and save your self half a day of pain we already spent on it (Special Thanks to 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 );