1

I know how to check if wifi is enabled or not.

Code:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
    //Code execution comes here
}

But how to find out if the user is actually connected to a nearby wifi network (or any wifi network for that matter)?

EDIT: I mean to ask, say if a user has logged in to a wifi network after typing in a password, then only would he be able to use that wifi. So is there anyway to check if has connected (logged in) to any wifi network?

SoulRayder
  • 4,699
  • 5
  • 39
  • 86
  • 1
    please check for a signal strength correspondingly with respect to each enabled wifi – Jitesh Upadhyay Apr 08 '14 at 10:10
  • I mean to say there will be a need to type a password to actually connect to some wifi networks. Is there a way to check if the user is already *logged in* to a wifi network or not? – SoulRayder Apr 08 '14 at 10:13
  • check my ans for this :) may this will help u – Bhanu Sharma Apr 08 '14 at 10:27
  • possible duplicate of [how to see if wifi is connected in android](http://stackoverflow.com/questions/3841317/how-to-see-if-wifi-is-connected-in-android) – Yogesh D Apr 08 '14 at 10:27

5 Answers5

1

You should be able to use the ConnectivityManager to get the state of the Wifi adapter. By this you can check if it is connected...

Method to check whether wifi is conected or not :-

public static Boolean checkWIFI(Activity activity) {
    Log.d("checkWIFI", "checkWIFI");

    ConnectivityManager cm = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    Log.d("NetworkInfo", "NetworkInfo" + netInfo);

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else if (netInfo != null
            && (netInfo.getState() == NetworkInfo.State.DISCONNECTED
                    || netInfo.getState() == NetworkInfo.State.DISCONNECTING
                    || netInfo.getState() == NetworkInfo.State.SUSPENDED || netInfo
                    .getState() == NetworkInfo.State.UNKNOWN)) {
        return false;

    } else {
        return false;
    }
}

You need to add permission in your manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

Good Luck!!

Namrata
  • 1,624
  • 1
  • 15
  • 28
0

Try this:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}

Hope this helps.. :)

Rashad
  • 10,519
  • 4
  • 40
  • 67
  • This will go into the if condition if the user is connectd to the wifi.. can I check if he has logged into the wifi network? – SoulRayder Apr 08 '14 at 10:53
0

Try this

ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

    //check Internet connection
    if(wifi)
    {
        //do what you want
    }else{
    }

also add require permissions

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Hope this will do the trick

Systematix Infotech
  • 2,271
  • 1
  • 12
  • 30
0
    /**
     * 
     * Method to check internet connection is available
     */

    public static boolean isInternetAvailable(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;
        boolean connectionavailable = false;

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            try {

                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected())
                        haveConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected())
                        haveConnectedMobile = true;
                if (informationabtnet.isAvailable()
                        && informationabtnet.isConnected())
                    connectionavailable = true;

            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("Inside utils catch clause , exception is"
                        + e.toString());
                e.printStackTrace();

            }
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

I have tried the similar code in my project and is running.Hope this would help you...:)

Pihu
  • 1,025
  • 11
  • 32
0

Just call this method for wifi network may this will help u :)

call like this checkWiFi(this); // pass context with this method here i pass this for current activity

  public static final boolean checkWiFi(Context cn)
    {
        ConnectivityManager connManager = (ConnectivityManager) cn.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected())
            return true;
        else
            return false;
    }

also take permission

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Bhanu Sharma
  • 4,946
  • 1
  • 21
  • 46