如何将 Android 连接到无 SSID 公共且无需密码的情况
我无法在已建立的 SSID 上连接 Android,我尝试 WifiConfiguration 但不成功,我的 ssid 已打开(没有密码)。
在 Android UI 中我可以连接,但在代码上我不能。
有人帮助我吗?
坦克友。
编辑:
我的代码:
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());
i can't connect Android on SSID established, i try WifiConfiguration but unsucessfull, my ssid is open (without password).
In Android UI i can connect, but on code i can't.
Anybody help me?
Tankyou.
EDIT:
my code:
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是因为
wifiManager.reconnect();
启动了一个异步进程来连接。当您的代码到达最后一行时,还没有连接(这需要时间,甚至可能是一分钟左右),因此null
是此时的正确答案。您将需要一个侦听器来对连接事件做出反应。有关详细信息,请参阅该问题:https://stackoverflow.com/a/5890553/1503237Probably 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), sonull
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