1

如何使用 SSID 连接到特定的 Wifi 网络?我试过使用下面的代码。

wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        WifiConfiguration config = new WifiConfiguration();

        if(wifi.startScan()){
            List<ScanResult> results = wifi.getScanResults();
            for(int i=0;i<results.size();i++)
            {   Log.e("VALUE"," "+results.get(i).toString());
                Log.e("",""+results.get(i).SSID);
                if(results.get(i).SSID.equalsIgnoreCase("\"MAC\""))
                {
                    Log.e("","mac.....");

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

            config.preSharedKey="0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";
            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;
                }
            }

        }

我在任何地方都没有找到有关如何执行此操作的任何信息?我哪里错了?

4

1 回答 1

0

您的问题可能与我遇到的问题有关。如果您直接从扫描结果中获取 SSID 值,则它不在双引号中,但由于某种原因 WifiConfiguration 要求 SSID 在双引号中。请看我的回答。

https://stackoverflow.com/a/12616521/178931

于 2012-09-27T07:51:34.883 回答