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");

   }

Upvotes: 3

Views: 6482

Answers (3)

Naskov
Naskov

Reputation: 4169

Don't use

conectivityManager.getNetworkInfo(0) and getNetworkInfo(1).

Instead of that use

For WiFi

NetworkInfo conectivityManager = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

For mobile networks(3g)

NetworkInfo conectivityManager = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

Cause on Jelly Beans will make trouble hardcoding values like that.

Upvotes: 0

Arun C
Arun C

Reputation: 9035

Use SupplicantState

SupplicantState supState; 
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();

See this answer in stackoverflow

If COMPLETED - All authentication completed.

Upvotes: 1

Related Questions