3

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

   }
D. Wonnink
  • 306
  • 4
  • 19

3 Answers3

1

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.

Community
  • 1
  • 1
Arun C
  • 9,035
  • 2
  • 28
  • 42
  • don't know about old APIs, but SupplicantState will rarely provide enough transitions when a bad password is input to ensure authentication is the "disableReason" in the now hidden APIs - i.e. we can never know if authentication failed in recent APIs – leRobot May 09 '16 at 10:36
  • Not useful for me, my device wi-fi is not authenticated but this code returns me COMPLETED every time. – Manish Jain Sep 01 '16 at 12:18
1

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?

Community
  • 1
  • 1
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • 1
    These answers only check effective connection, and don't provide enough detail to assert authentication is the issue. My comment elsewhere in this post: "don't know about old APIs, but SupplicantState will rarely provide enough transitions when a bad password is input to ensure authentication is the "disableReason" in the now hidden APIs - i.e. we can never know if authentication failed in recent APIs" – leRobot May 09 '16 at 10:37
  • Agreed! For api Above 21 its possible to check using ConnectivityManager.NetworkCallback() However, there is no solution check for api below 21, if anyone finds that solution please help. Thank you. – Junia Montana Mar 08 '20 at 20:36
0

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.

Naskov
  • 4,121
  • 5
  • 38
  • 62