0

I want to connect to specific wifi by SSID using this code.

It is not connecting to wifi's SSID in String. I am new in Android and it will be good if someone shows me the problem. (Sure, not fix the code)!

public class DiscoverActTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String networkSSID = "DIRECT";
        String networkPass = "123456789";

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
        conf.wepKeys[0] = "\"" + networkPass + "\"";
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        conf.preSharedKey = "\""+ networkPass +"\"";
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
        wifiManager.addNetwork(conf);

         wifiManager.setWifiEnabled(true);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();
                break;
            }
        }
    }
}
Keale
  • 3,924
  • 3
  • 29
  • 46
muzafako
  • 188
  • 2
  • 11

1 Answers1

0

Change

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

To

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
Koustuv Ganguly
  • 897
  • 7
  • 21