Saturday, April 14, 2012

How Check Wifi connection with it's state in Android ?


To check wifi we were only using ConnectivityManager and Networkinfo. By this we can only check that wifi is available and wifi is connected.
But what about when wifi is connected but not in range?
So to check the wifi state we can use this given code snippet.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isAvailable() && mWifi.isConnected()) {
                SupplicantState supState;
                WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                supState = wifiInfo.getSupplicantState();
                if(String.valueOf(supState).equalsIgnoreCase("COMPLETED") || String.valueOf(supState).equalsIgnoreCase("ASSOCIATED")){
                     //wifi is connected
                }
        }
Please refer to this given link for more information.
Reference Link:- http://stackoverflow.com/questions/3841317/how-to-see-if-wifi-is-connected-in-android

No comments:

Post a Comment