简体   繁体   English

如何使用指定的SSID自动连接WiFi?

[英]How to auto connect a WiFi with specified SSID?

Can some body help me to solve this issue? 有人可以帮我解决这个问题吗?

Here is my code, and at mWifi.enableNetwork(netID, true) it's cannot enable network and cannot auto connect to specified network. 这是我的代码,在mWifi.enableNetwork(netID, true)它无法启用网络,无法自动连接到指定的网络。 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");
                            }
                        }
                    }

                }
            }

        });      
    }
}

I think you need to add a WifiConfiguration.KeyMgmt to your WifiConfiguration object. 我认为你需要一个添加WifiConfiguration.KeyMgmt您WifiConfiguration对象。 Assuming it's an open network: 假设它是一个开放的网络:

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

Also, be cautious in assuming that scan results are available immediately upon exit of your call to startScan() . 另外,在您退出调用startScan()立即假设扫描结果可用时要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. 在这种情况下,最好的办法是在WifiManager.SCAN_RESULTS_AVAILABLE_ACTION上添加一个BroadcastReceiver, WifiManager.SCAN_RESULTS_AVAILABLE_ACTIONmWifi.getScanResults()转发中添加所有代码。 You will need to add a call to mWifi.reconnect() once you get enableNetwork() to succeed. 一旦enableNetwork()成功,您将需要添加对mWifi.reconnect()的调用。

As for initializing your WifiConfiguration wc , I'd love it if you'd consider my post here . 至于初始化你的WifiConfiguration wc ,如果你在这里考虑我的帖子,我会很喜欢它。 Finally, another good answer is here . 最后,另一个好的答案就在这里

try adding remaining information about the network such as allowed ciphers, key Mangement scheme and the PSK (if you are using one) 尝试添加有关网络的剩余信息,例如允许的密码,密钥管理方案和PSK(如果您使用的话)

Secondly check in the configured networks list first to make sure that you don't try to add another network with the same configuration as there is not point doing that. 其次,首先检查已配置的网络列表,以确保您不会尝试添加具有相同配置的另一个网络,因为没有点这样做。

As a recommendation don't startScan() and wait for it finish. 作为建议不要startScan()并等待它完成。 Register for SCAN_RESULTS_AVAILABLE_ACTION and use a callback from the broadcast receiver to your activity to process those results. 注册SCAN_RESULTS_AVAILABLE_ACTION并使用从广播接收器到您的活动的回调来处理这些结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM