Android 连接 WiFi 无需人工交互
我想知道是否有一些代码片段可用于将 Android 设备连接到 WiFi 网络。网络应该是开放的或 WEP/WPA 加密的,并且对该设备可见。通常,我们使用GUI界面输入WiFi密码并点击连接按钮。我想将密码存储在一个地方,并使用密码无缝连接到网络,而无需人工交互。这可能吗?
I'm wondering if there are some code snippets that can be used to connect an Android device to a WiFi network. The network should be either open or WEP/WPA encrypted, and visible to that device. Normally, we use GUI interface to input WiFi passwords and tap the connect button. I want to store the password in a place, and use the password to connect to the network seamlessly without human interaction. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢你们。在您的帮助下,我现在能够轻松连接到 WPA/PSK 加密网络。这是我的代码片段:
技巧是:
Thanks guys. With your help, I'm now able to connect to a WPA/PSK encrypted network without pain. Here is my code snippet:
The tricks are :
为了使 OP 示例代码正常工作,我必须再添加一行:
如果没有这一行,它将无法连接到网络。配置已被接受并添加,但未进行任何连接尝试。实际上,我在 logcat 窗口中收到了以下消息:
这使我找到了最终的解决方案,并找出了为什么它对我不起作用。
To make OPs sample code work, I had to add one more line:
Without that line it just won't connect to the network. The configuration is accepted and added, but no connection attempts are made. I actually got the following message in the logcat window:
which put me to the final solution, figuring out why it didn't work for me.
WifiManager - 您是否尝试过在这里查看。 addNetwork()< /a> 方法看起来可以做你想做的事情。您所要做的就是将信息放入 WifiConfiguration 类中然后添加网络,然后启用该连接。文档就在那里。
WifiManager - Have you tried looking here. The addNetwork() method looks like it can do what you want it to do. All you have to do is put the information in a WifiConfiguration class and then add the network, then enable that connection. The Documentation is all there.
查看“WifiManager”的文档
它可以用来启用 wifi:
并且它可以用来做许多其他事情。
编辑:在监视和更改 wifi 状态时,不要忘记更新您的权限,例如:
等...
Checkout the documentation for "WifiManager"
It can be used to enable wifi:
And it can be used to do many other things.
Edit: Don't forget to update your permissions when monitoring and changing wifi state, example:
etc...