19

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

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

4

3 回答 3

47

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

While documentation here states

"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."

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] 

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

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)

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 );
于 2010-01-26T15:03:14.530 回答
6

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

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

于 2012-12-14T08:55:39.730 回答
3

您必须添加波纹管才能:

wifi.saveConfiguration();
于 2012-04-01T05:53:26.230 回答