问题描述
我无法在已建立的SSID上连接Android,我尝试了WifiConfiguration,但未成功,我的ssid已打开(没有密码).
i can't connect Android on SSID established, i try WifiConfiguration but unsucessfull, my ssid is open (without password).
在Android UI中,我可以连接,但是在代码上,我无法连接.
In Android UI i can connect, but on code i can't.
有人帮我吗?
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());
推荐答案
可能是因为wifiManager.reconnect();
启动了一个异步进程进行连接.当您的代码到达最后一行时,仍然没有连接(这需要时间,甚至可能需要一分钟左右),因此null
是此时的正确答案.您将需要一个侦听器以对connect事件做出反应.有关详情,请参见该问题: https://stackoverflow.com/a/5890553/1503237
Probably because wifiManager.reconnect();
starts an asynchronous process to connect. When your code reaches the last line, there is yet no connection (that takes time, may even be a minute or so), so null
is the correct answer at that moment. You will need a listener to react on the connect event. See that question for details: https://stackoverflow.com/a/5890553/1503237
这篇关于如何将Android连接到没有公共的SSID并且不需要输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!