Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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");
                            }
                        }
                    }

                }
            }

        });      
    }
}
like image 936
Tody Kwok Avatar asked May 31 '12 13:05

Tody Kwok


People also ask

How do I automatically connect to a specific Wi-Fi?

Open Settings. Click on Network & Internet and choose WiFi. Scroll down and tap WiFi Preferences. Select Turn on WiFi automatically.

How do I connect to a specific SSID?

Select Settings > Wi-Fi > Add Wi-Fi network. Enter the network name (SSID), security type, and password. Tap Connect. Your device connects to the network.

Can I automatically connect to the strongest Wi-Fi network under Windows 10?

Windows should automatically always connect to the strongest signal at the time it is turned on and will not switch until the connection is lost/very weak.

How do I prioritize my Wi-Fi SSID?

The quickest way to make a Wi-Fi connection a priority is to use the Network flyout available in the taskbar. Click the wireless icon on the bottom-right corner of the taskbar. Select the wireless network you want to prioritize. Check the Connect automatically option.


1 Answers

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);

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.

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

like image 166
QED Avatar answered Oct 18 '22 08:10

QED