原文地址:
http://stackoverflow.com/questions/2140133/how-and-what-to-set-to-android-wificonfiguration-presharedkey-to-connect-to-the-wWifiManager 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#SSIDwc.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 );
每次wifi.addNetwork(wc);
之后,返回的都是-1
改成wc.preSharedKey = "\"password\"";