Android 连接 WiFi 无需人工交互

发布于 2024-11-10 05:17:01 字数 145 浏览 3 评论 0原文

我想知道是否有一些代码片段可用于将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演出会有结束 2024-11-17 05:17:01

谢谢你们。在您的帮助下,我现在能够轻松连接到 WPA/PSK 加密网络。这是我的代码片段:

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        // setup a wifi configuration
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"YOUR_SSID\"";
        wc.preSharedKey = "\"YOUR_PASSWORD\"";
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        // connect to and enable the connection
        int netId = wifiManager.addNetwork(wc);
        wifiManager.enableNetwork(netId, true);
        wifiManager.setWifiEnabled(true);

技巧是:

  • SSID 字符串应该用“括起来,用 \” 表示
  • addNetwork() 方法默认禁用添加的网络,因此您应该使用 enableNetwork() 方法启用它。

Thanks guys. With your help, I'm now able to connect to a WPA/PSK encrypted network without pain. Here is my code snippet:

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        // setup a wifi configuration
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"YOUR_SSID\"";
        wc.preSharedKey = "\"YOUR_PASSWORD\"";
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        // connect to and enable the connection
        int netId = wifiManager.addNetwork(wc);
        wifiManager.enableNetwork(netId, true);
        wifiManager.setWifiEnabled(true);

The tricks are :

  • SSID string should be surrounded with ", which is denoted by \"
  • addNetwork() method DISABLES the added network by default, so you should enable it with the enableNetwork() method.
姐不稀罕 2024-11-17 05:17:01

为了使 OP 示例代码正常工作,我必须再添加一行:

wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

如果没有这一行,它将无法连接到网络。配置已被接受并添加,但未进行任何连接尝试。实际上,我在 logcat 窗口中收到了以下消息:

Event [WPA: Failed to select WPA/RSN] android

这使我找到了最终的解决方案,并找出了为什么它对我不起作用。

To make OPs sample code work, I had to add one more line:

wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

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:

Event [WPA: Failed to select WPA/RSN] android

which put me to the final solution, figuring out why it didn't work for me.

后知后觉 2024-11-17 05:17:01

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.

余生共白头 2024-11-17 05:17:01

查看“WifiManager”的文档

它可以用来启用 wifi:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

并且它可以用来做许多其他事情。

编辑:在监视和更改 wifi 状态时,不要忘记更新您的权限,例如:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

等...

Checkout the documentation for "WifiManager"

It can be used to enable wifi:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

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:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文