Serkan Özaydin
Serkan Özaydin

Reputation: 27

Android studio connect to wifi without password

I try connect to wifi and i can connect with password.But if wifi hasn't no password i can't connect,How can i?

this my code

public void connectWithoutPasswordToWifi(String networkSSID) {
    android.net.wifi.WifiConfiguration conf = new android.net.wifi.WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(conf);

    List<android.net.wifi.WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for (android.net.wifi.WifiConfiguration connect : list) {

        if (connect.SSID != null && connect.SSID.equals("\"" + networkSSID + "\"")) {
            wifiManager.disconnect();
            wifiManager.enableNetwork(connect.networkId, true);
            wifiManager.reconnect();

            break;
        }


    }

}

EDIT: I found this How connect Android to no SSID public and no Password requered

Upvotes: 0

Views: 1863

Answers (1)

Vinod Ranga
Vinod Ranga

Reputation: 531

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";  

For Open network you need to do this:

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

Upvotes: 3

Related Questions