1

I'm getting SSID and Password from user as an input. I want to create a function connectWifi(String SSID, String password) that returns connection successful or invalid credentials. What is the best way to connect Wi-Fi using SSID and Password?

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • 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) – TDG Oct 17 '19 at 09:52
  • No it is not. I'm not directly interested in connecting. I want to check the Wi-Fi password if it is correct or not. – androiddeveloper Oct 17 '19 at 10:07

3 Answers3

1

You can try the below code snippet to check for available wifi networks and get connected to a specified wifi network.

List<ScanResult> wifiScanList = wifi.getScanResults();
         wifis = new String[wifiScanList.size()];

         for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).SSID);                

            if(wifis[i].equals("WiredSSID")) {

                 WifiConfiguration wifiConfig = new WifiConfiguration();
                 wifiConfig.SSID = String.format("\"%s\"", wifis[i]);
                 wifiConfig.preSharedKey = String.format("\"%s\"", "password");

                 WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
                 //remember id
                 int netId = wifiManager.addNetwork(wifiConfig);
                 wifiManager.disconnect();
                 wifiManager.enableNetwork(netId, true);
                 wifiManager.reconnect();
             }
         }

And You must check the correct permission as well !!

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Rajan
  • 369
  • 3
  • 18
  • If I write wrong password it is still connecting to the Wi-Fi. This implementation does not work if user is connecting to the Wi-Fi on android phone first time. I want to verify the password if it is correct or not. Is there a way to do this job ? – androiddeveloper Oct 17 '19 at 10:05
  • 1
    You must go to this link, I hope you will get your answer: `https://medium.com/@josiassena/android-manipulating-wifi-using-the-wifimanager-9af77cb04c6a` – Rajan Oct 17 '19 at 10:27
1

We can use following method to connect wifi with provided password

String connectWifi(String ssid, String password){
   WifiConfiguration wifiConfig = new WifiConfiguration();
   wifiConfig.SSID = String.format("\"%s\"", ssid);
   wifiConfig.preSharedKey = String.format("\"%s\"", password);
   WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
   //remember id
   int netId = wifiManager.addNetwork(wifiConfig);
   wifiManager.disconnect();
   wifiManager.enableNetwork(netId, true);

   boolean isConnectionSuccessful = wifiManager.reconnect();

   if(isConnectionSuccessful){
       return "connection successful";
   }else{
       return "invalid credential";
   }
}
0

You can use this function for android 10 and above

private fun connectToYourWifi(ssid: String, password:String) {

        val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build()

        val networkRequest = NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(wifiNetworkSpecifier)
            .build()

        val connectivityManager =
            applicationContext!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connectivityManager.requestNetwork(networkRequest, ConnectivityManager.NetworkCallback())
    }

This code open up default dialog to connect with your specific wifi that you want to connect.

Prashant Sharma
  • 1,357
  • 1
  • 21
  • 31