连接到指定的可用 wifi 网络

发布于 2024-11-03 10:59:19 字数 1849 浏览 0 评论 0原文

从我的应用程序中,我能够打开/关闭设备中的 WIFI 并能够扫描可用网络,但我无法连接到指定的可用网络。这是我正在使用的代码:

 if(wifi.getWifiState()==wifi.WIFI_STATE_DISABLED)
        { 
            wifi.setWifiEnabled(true);

        }
        if(wifi.startScan())
        {
            //ls=(ArrayAdapter<ScanResult>) wifi.getScanResults();
            ls=wifi.getScanResults();

            Log.e("",ls.get(0).toString());
            for(int i=0;i<ls.size();i++)
            {   Log.e("VALUE"," "+ls.get(i).toString());
                Log.e("",""+ls.get(i).SSID);
                if(ls.get(i).SSID.equalsIgnoreCase("SPECTRUM-GREEN"))
                {
                    Log.e("","SPectrum GREEN FOUND.....");

            try{ 
                String ssid="\""+ls.get(i).SSID+"\"";
                Log.e("SSId"," "+ssid);
                config.SSID=ssid;
            }catch(Exception e){Log.e("","Error : "+e.toString());}

            config.preSharedKey="\"password\"";
            config.status=WifiConfiguration.Status.ENABLED;

            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

            int res=wifi.addNetwork(config);
                    Log.e("ENABLE ",""+wifi.enableNetwork(res, false));

                    break;
                }
            }

From my application, I am able to switch on/off WIFI in my device and able to scan the available networks, but i am unable to connect to specified available network. This is the code I am using:

 if(wifi.getWifiState()==wifi.WIFI_STATE_DISABLED)
        { 
            wifi.setWifiEnabled(true);

        }
        if(wifi.startScan())
        {
            //ls=(ArrayAdapter<ScanResult>) wifi.getScanResults();
            ls=wifi.getScanResults();

            Log.e("",ls.get(0).toString());
            for(int i=0;i<ls.size();i++)
            {   Log.e("VALUE"," "+ls.get(i).toString());
                Log.e("",""+ls.get(i).SSID);
                if(ls.get(i).SSID.equalsIgnoreCase("SPECTRUM-GREEN"))
                {
                    Log.e("","SPectrum GREEN FOUND.....");

            try{ 
                String ssid="\""+ls.get(i).SSID+"\"";
                Log.e("SSId"," "+ssid);
                config.SSID=ssid;
            }catch(Exception e){Log.e("","Error : "+e.toString());}

            config.preSharedKey="\"password\"";
            config.status=WifiConfiguration.Status.ENABLED;

            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

            int res=wifi.addNetwork(config);
                    Log.e("ENABLE ",""+wifi.enableNetwork(res, false));

                    break;
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鲜血染红嫁衣 2024-11-10 10:59:19

虽然这是一个老问题,但万一有人遇到这个问题,以下内容对我有帮助:

void connect (String ssidName) {
    boolean result = false;
    List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration wifiConfiguration : arraylist) {
        String wifiConfigSSID = wifiConfiguration.SSID.replace("\"", "");
        if (wifiConfigSSID.equals(ssidName)) {
            result = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
            break;
        }
    }
}

如果网络已经可用,则无需提供各种参数,包括密码。

Although this is an old question, in case someone comes across this, the following helped me:

void connect (String ssidName) {
    boolean result = false;
    List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration wifiConfiguration : arraylist) {
        String wifiConfigSSID = wifiConfiguration.SSID.replace("\"", "");
        if (wifiConfigSSID.equals(ssidName)) {
            result = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
            break;
        }
    }
}

If the network is already available, then there is no need for giving the various parameters, including the password.

﹏半生如梦愿梦如真 2024-11-10 10:59:19

只是一个评论。您是否正在尝试临时连接?我读过的“普通”Android 手机不可能做到这一点......

Just a comment. Are you trying to connect ad hoc? This is not possible with "normal" Android phones I read...

扭转时空 2024-11-10 10:59:19

您必须禁用其他网络:

wifi.enableNetwork(res, true);

You have to disable other networks:

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