Search code examples
androidandroid-wifi

Android:How can I check if wifi authenticated or not


My application is using wifi manager in android, but I am facing an error in a situation the situation is::WIFI is turned on but it is not authenticated(with our company wifi user name and password)at that time my application is force closing,

code I am using is

try{
    if (connectivityManager != null && 
       (connectivityManager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||
       (connectivityManager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED))
    { 
        //You are connected, do something online.
        return true;
    }
    else if (connectivityManager.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  
             connectivityManager.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) 
    {             
        //Not connected.        
        //Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();
        loginTask.myPublishProgress("You must be connected to the internet");
        return false; 
    }   
    }
catch(NullPointerException ae)
   {
        ae.printStackTrace();
        loginTask.myPublishProgress("You must be connected to the internet");

   }

Solution

  • Hi check the below answers:

    How to detect when WIFI Connection has been established in Android?

    How do I see if Wi-Fi is connected on Android?