Java 如何以及如何设置 Android WifiConfiguration.preSharedKey 以连接到 WPA2 PSK WiFi 网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2140133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How and what to set to Android WifiConfiguration.preSharedKey to connect to the WPA2 PSK WiFi network
提问by Boris Daich
In Android 1.5 (also on 1.6)
在 Android 1.5(也在 1.6 上)
How to add an Access Point from code?
如何从代码添加接入点?
Given Access point that supports WPA2. Here is my code snippet.
给定支持 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 );
This code fails as in LogCat appear
此代码失败,因为在 LogCat 中出现
01-26 16:44:13.550: ERROR/wpa_supplicant(2032): Line 0: Invalid PSK 'password'.
01-26 16:44:13.550:错误/wpa_supplicant(2032):第 0 行:PSK“密码”无效。
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?
我确信这是密码并且所有其余参数都是正确的。我想念什么?
采纳答案by Boris Daich
The reason for the my sorrow is here in this Documentation issue
我悲伤的原因在这个文档问题中
While documentation herestates
虽然这里的文档指出
"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."
“与 WPA-PSK 一起使用的预共享密钥。读取此密钥的值时,不会返回实际密钥,如果密钥有值,则仅返回“*”,否则返回空字符串。”
It is correct, but very important what it does not sayis that expected here ether 64 byte hash result of the linux command
这是正确的,但非常重要的是它没有说的是这里预期的 linux 命令的 64 字节哈希结果
wpa_passphrase <ssid> [passphrase]
or Access Point's password IN DOUBLE QUOTES!
或接入点的密码双引号!
So in case that Access Point's PSK is "example"it has to be passed in java like this
因此,如果接入点的 PSK 是“示例”,则必须像这样在 java 中传递
WifiConfiguration myWiFiConfig = new WifiConfiguration();
...
myWiFiConfig.preSharedKey = "\"example\"";
...
OR
或者
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)
按原样复制并粘贴它,让您自己节省半天的痛苦,我们已经为此付出了半天的时间(特别感谢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 );
回答by sl96314
You will have to add bellow line in order to:
您必须添加波纹管才能:
wifi.saveConfiguration();
回答by poe
thanks, all i can user your code conncet to my wpa psk wifi.
谢谢,我可以将您的代码连接到我的 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 );
early, I input the error password, but later I correct password then it works.
早些时候,我输入了错误密码,但后来我更正了密码,然后它就可以工作了。