如何将 Android 连接到无 SSID 公共且无需密码的情况

发布于 2025-01-01 03:58:14 字数 1341 浏览 6 评论 0原文

我无法在已建立的 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 技术交流群。

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

发布评论

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

评论(1

恍梦境° 2025-01-08 03:58:14

可能是因为 wifiManager.reconnect(); 启动了一个异步进程来连接。当您的代码到达最后一行时,还没有连接(这需要时间,甚至可能是一分钟左右),因此 null 是此时的正确答案。您将需要一个侦听器来对连接事件做出反应。有关详细信息,请参阅该问题: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

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