如何自动连接具有指定SSID的WiFi? [英] How to auto connect a WiFi with specified SSID?

查看:507
本文介绍了如何自动连接具有指定SSID的WiFi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个问题吗?

Can some body help me to solve this issue?

这是我的代码,在mWifi.enableNetwork(netID, true)处,它无法启用网络,也无法自动连接到指定的网络.所以我想知道我在哪里犯了错误?

Here is my code, and at mWifi.enableNetwork(netID, true) it's cannot enable network and cannot auto connect to specified network. So I want to know where I had made a mistake?

    public class WifiConnActivity extends Activity {
    /** Called when the activity is first created. */
    final String tag = "WifiConn:...";
    EditText txt;
    WifiManager mWifi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        txt = (EditText)findViewById(R.id.editText1);

        Button b1 = (Button)findViewById(R.id.B1);        
        b1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {

                if (mWifi.startScan())  //scan now
                {
                    Log.d(tag, "startScan()");

                    List<ScanResult> sRet = mWifi.getScanResults();  //scan results.

                    for (int i=0; i<sRet.size(); i++)
                    {
                        ScanResult retS = sRet.get(i); 
                        txt.append("resT: " + retS.SSID +" " + retS.BSSID + "\n");
                        Log.d(tag, "resT: " + retS.SSID +" " + retS.BSSID);

                        if (retS.SSID.equalsIgnoreCase("TEST"))
                        {
                            txt.append("Found: " + retS.SSID +" " + retS.BSSID + "\n");

                            WifiConfiguration wc = new WifiConfiguration();

                            wc.SSID = "\""+retS.SSID+"\"";
                            wc.BSSID = retS.BSSID;
                            wc.status = WifiConfiguration.Status.ENABLED;
                            wc.hiddenSSID = true;

                            int netID = mWifi.addNetwork(wc); // add network
                            txt.append("addNetwork: "+ Integer.toString(netID) +"\n");

                            if(mWifi.enableNetwork(netID, true)) // enable network, but cannot work???????????
                            {
                                txt.append("enableNetwork: true\n");
                            }
                        }
                    }

                }
            }

        });      
    }
}

推荐答案

我认为您需要在WifiConfiguration对象中添加WifiConfiguration.KeyMgmt.假设它是一个开放的网络:

I think you need to add a WifiConfiguration.KeyMgmt to your WifiConfiguration object. Assuming it's an open network:

wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

此外,请谨慎地假设在退出startScan()呼叫后立即可以获取扫描结果.在这种情况下,最好的选择是在 ,并从mWifi.getScanResults()开始将所有代码添加到其中.成功获得enableNetwork()之后,您将需要向mWifi.reconnect()添加呼叫.

Also, be cautious in assuming that scan results are available immediately upon exit of your call to startScan(). The best bet in this case is to add a BroadcastReceiver on WifiManager.SCAN_RESULTS_AVAILABLE_ACTION and add to it all of your code from mWifi.getScanResults() forward. You will need to add a call to mWifi.reconnect() once you get enableNetwork() to succeed.

关于初始化您的WifiConfiguration wc,如果您愿意在此处看到我的帖子,我会很乐意.最后,另一个很好的答案是此处.

As for initializing your WifiConfiguration wc, I'd love it if you'd consider my post here. Finally, another good answer is here.

这篇关于如何自动连接具有指定SSID的WiFi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆