我无法在建立的SSID上连接Android,我尝试WifiConfiguration但是不成功,我的ssid是开放的(没有密码)。
在Android UI中我可以连接,但在代码上我不能。
有人帮帮我吗?
Tankyou。
编辑:
我的代码:
String networkSSID = "DL63.0.0.0.1";
String networkPass = "";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
// conf.wepKeys[0] = "\"" + networkPass + "\"";
// conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
// conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
Toast.makeText(this, "SSID: "+wifiManager.getConnectionInfo().getSSID(), Toast.LENGTH_SHORT).show();
break;
}
}
status.setText("SSID: "+wifiManager.getConnectionInfo().getSSID());
答案 0 :(得分:0)
可能是因为wifiManager.reconnect();
启动了异步进程来连接。当您的代码到达最后一行时,仍然没有连接(这需要时间,甚至可能是一分钟左右),因此null
是当时的正确答案。您将需要一个侦听器来对connect事件做出反应。有关详细信息,请参阅该问题:https://stackoverflow.com/a/5890553/1503237