Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Open WiFi

I use the code below to connect to encrypted networks. However if a network is unsecured and I leave key empty ("") then it fails. Has anyone an idea how to solve this? Furthermore is it possible to detect if a network is open using the ssid / bssid ? Or do I have to scan with a filter?

public void connectToSSID(final String ssid, final String key) {
    Log.i("wifimaster", "connection to "+ssid);

    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\""+ssid+"\""; //IMPORTANT! This should be in Quotes!!  
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

    wc.preSharedKey = "\""+key+"\"";
    wc.wepKeys[0] = "\""+key+"\""; //This is the WEP Password
    wc.wepTxKeyIndex = 0;

    wc.preSharedKey = "\""+key+"\"";

    int res = wifiManager.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean es = wifiManager.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es );
    boolean b = wifiManager.enableNetwork(res, true);   
    Log.d("WifiPreference", "enableNetwork returned " + b );

    if(b)
        Toast.makeText(c, c.getString(R.string.connected), Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(c, c.getString(R.string.unconnected), Toast.LENGTH_SHORT).show();
}
like image 917
paulgavrikov Avatar asked Oct 08 '12 21:10

paulgavrikov


People also ask

What does connect to open Wi-Fi mean?

An open WiFi is a WiFi that is not password protected. Sometimes the open WiFi turns out to be a captive portal, and in that case, it will look like you are connected to the internet, when you are in fact only connected to the router of the WiFi, without being able to surf.

Is it safe to connect to open Wi-Fi?

Public Wi-Fi Isn't Secure If the network isn't secure, and you log into an unencrypted site — or a site that uses encryption only on the sign-in page — other users on the network can see what you see and send. They could hijack your session and log in as you.


1 Answers

Use

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

only and it will work.

remove all other assignments to wc, it is not necessary to use them for a simple task of connecting to a network. You are not creating a network, you are connecting to an existing one.

For full solution check this link: How do I connect to a specific Wi-Fi network in Android programmatically? It has everything you need to know.

like image 187
Vikram Gupta Avatar answered Oct 12 '22 23:10

Vikram Gupta