你好,我正在尝试使用wifimanager api将我的应用程序连接到特定的接入点。目前,我拥有我所在区域内的所有接入点列表,我将它们存储在一个数组中,并选择要连接的接入点。但是目前为止,它还没有连接成功。请问有人可以帮助我吗?
(我正在尝试连接的是一个开放网络。)以下是我的代码:
public void WifiConfiguration(){
try {
ScanResult networkData = getIntent().getParcelableExtra("networkData");
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String networkPassWord = "";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkData.SSID + "\"";
conf.BSSID = "\"" + networkData.BSSID + "\"";
conf.hiddenSSID = true;
conf.wepKeys[0] = "\"" + networkPassWord + "\"";
conf.wepTxKeyIndex = 0;
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.preSharedKey = "\""+ networkPassWord +"\"";
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Log.d(TAG, "Initialising WIFI Manager");
int id = wifiManager.addNetwork(conf);
Log.d(TAG, "conf.SSID: "+conf.SSID);
Log.d(TAG, "id: "+id);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
Log.d(TAG, "Should be connected....");
} catch (Exception e) {
Log.d(TAG, e.toString());
}
}