1

m trying to understand the wifi part in the android by connecting to specific wifi and i did my device is connected to the wifi that i configured successfuly,,but the problem is its always gives "connection success" and move to the next activity regardless its connected to the my wifi or not ,, i want when connected he can access the next activity otherwise he cant

MainClass.java

public String networkSSID = "helloworld";
public String networkPass = "it123456";
ImageButton ConnectButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi);
    ConnectButton = (ImageButton) findViewById(R.id.enter);
    ConnectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            networkSSID = "helloworld";
            networkPass = "it123456";

            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes

            /* for wep*/
            //conf.wepKeys[0] = "\"" + networkPass + "\"";
            //conf.wepTxKeyIndex = 0;
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            //conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

            /* for wpa*/
            conf.preSharedKey = "\"" + networkPass + "\"";

            /* for open network*/
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

            Context context = getApplicationContext();
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            wifiManager.setWifiEnabled(true);

            wifiManager.addNetwork(conf);

            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration i : list)
                if (i.SSID != null && i.SSID.equals("helloworld" + networkSSID + "it123456")) {
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(i.networkId, true);
                    wifiManager.reconnect();
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    while (wifiInfo.getSSID() == null) {
                        Log.i("WifiStatus", "Here I am");
                        try {
                            Thread.sleep(Time.SECOND);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        wifiInfo = wifiManager.getConnectionInfo();
                    }
                    System.out.println("Connection established");
                    break;
                }
            if (networkSSID == "helloworld" && networkPass == "it123456") {
                Toast.makeText(context, "Connection success", 400).show();
            } else {
                Toast.makeText(context, "Connection fail", 400).show();
            }

            startActivity(new Intent(wifi.this, MainActivity.class));
        }
    });
}
Rakesh
  • 4,004
  • 2
  • 19
  • 31
  • Possible duplicate of [How do I connect to a specific Wi-Fi network in Android programmatically?](https://stackoverflow.com/questions/8818290/how-do-i-connect-to-a-specific-wi-fi-network-in-android-programmatically) – Tomin B Azhakathu Nov 09 '18 at 13:40
  • You cannot compare `String` objects using `==`. You must use `.equals()`. At `if (networkSSID == "helloworld"...` – KevinO Nov 09 '18 at 15:09

0 Answers0