所以我在这里和谷歌会带我去的任何其他地方搜索了真正的所有线程。但我仍然在连接到 WPA PSK 网络时遇到问题。这是我的代码,我有 2 个编辑文本字段,我从中读取 SSID 和 PSK,然后选择一个复选框来选择 SSID 是否隐藏。
EditText mSSID = (EditText) findViewById(R.id.wifiTVssidcurrent);
String networkSSID = mSSID.getText().toString();
EditText mWPA = (EditText) findViewById(R.id.wifiTVwpacurrent);
String networkWPA = mWPA.getText().toString();
// Update text to show that connection is pending
TextView wifiStatus = (TextView) findViewById(R.id.wifiTVconnectionstatus);
wifiStatus.setText("Connecting to " + networkSSID);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"".concat(networkSSID).concat("\"");
wc.preSharedKey = "\"".concat(networkWPA).concat("\"");
CheckBox mSSIDHidden = (CheckBox) findViewById(R.id.wifiCBhiddenssid);
wc.hiddenSSID = false;
if (mSSIDHidden.isChecked()) {
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 );
boolean c = wifi.reconnect();
Log.d("WifiPreference", "reconnect returned " + c );
运行后我在手机中看到的是在设置中创建了一个 AP,但它没有连接。如果我之后尝试从设置中手动使用创建的 AP,我也无法连接。但是,如果我从设置中创建 AP,我应该得到连接。
至于将SSID和WPA PSK放入我已经尝试过 "\"".concat(networkSSID).concat("\""); 和 "\""+ 网络SSID +"\"";结果相同。
任何提示都将受到欢迎。最好的问候拉斯