Example usage for android.net ConnectivityManager TYPE_WIFI

List of usage examples for android.net ConnectivityManager TYPE_WIFI

Introduction

In this page you can find the example usage for android.net ConnectivityManager TYPE_WIFI.

Prototype

int TYPE_WIFI

To view the source code for android.net ConnectivityManager TYPE_WIFI.

Click Source Link

Document

A WIFI data connection.

Usage

From source file:Main.java

public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NETWORK_WIFI;/*from  w  w w  . j  a v a  2s .c o m*/
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

            case NETWORK_TYPE_GSM:
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netType = NETWORK_2G;
                break;

            case NETWORK_TYPE_TD_SCDMA:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                netType = NETWORK_3G;
                break;

            case NETWORK_TYPE_IWLAN:
            case TelephonyManager.NETWORK_TYPE_LTE:
                netType = NETWORK_4G;
                break;
            default:

                String subtypeName = info.getSubtypeName();
                if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA")
                        || subtypeName.equalsIgnoreCase("CDMA2000")) {
                    netType = NETWORK_3G;
                } else {
                    netType = NETWORK_UNKNOWN;
                }
                break;
            }
        } else {
            netType = NETWORK_UNKNOWN;
        }
    }
    return netType;
}

From source file:Main.java

public static final boolean isNetworkAvailable(Context paramContext) {
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService(Activity.CONNECTIVITY_SERVICE);
    final NetworkInfo localNetworkInfo1 = localConnectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final NetworkInfo localNetworkInfo2 = localConnectivityManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return (localNetworkInfo1.isConnected() && localNetworkInfo2.isConnected());
}

From source file:fr.pingtimeout.ConnectionUtils.java

public static boolean isOnWifi(Context context) {
    Log.d(loggerName, "Checking if connection is Wifi");

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifiNetworkInfo.isConnected()) {
        Log.d(loggerName, "Wifi is connected");
        return true;
    } else {/*from w  ww . ja  va  2 s. c o  m*/
        Log.d(loggerName, "Wifi is not connected");
        return false;
    }
}

From source file:Main.java

/**
 * Checks for network connectivity via WiFi or Mobile.
 *
 * @param context Context of application
 * @return A concatenated string with two fields separated by a colon.  The first field is Wifi
 * and second field is Mobile.  If wifi is up, there will be a 'w', otherwise no character.
 * If mobile is up, there will be a 'm', otherwise no character.  If neither is up, then the
 * string will only be the colon.//from   w w w . ja v  a 2  s .c o  m
 */
public static String getNetworkStatus(Context context) {

    String result = "";

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    boolean wifiConnected;
    boolean mobileConnected;
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {
        wifiConnected = false;
        mobileConnected = false;
    }

    if (wifiConnected)
        result += "w";
    result += ":";
    if (mobileConnected)
        result += "m";
    return result;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//  w w w .j  a v  a2  s . c  om
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;
    final boolean onlyOnWifi = true;//PreferenceUtils.getInstace(context).onlyOnWifi();

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        if (!onlyOnWifi) {
            state = mbobileNetwork.isConnectedOrConnecting();
        }
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (!onlyOnWifi) {
            state = activeNetwork.isConnectedOrConnecting();
        }
    }

    return state;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from   w  w w  .jav a  2s.  c  om
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        state = mbobileNetwork.isConnectedOrConnecting();
    }

    /* Other networks */
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        state = activeNetwork.isConnectedOrConnecting();
    }

    return state;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    // showLog("getNetWorkType");
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == networkInfo || !networkInfo.isAvailable()) {
        return 1;
    }/*from  w w w  .  j  a va 2 s.  c  o m*/
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
        return 2;
    }
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
        return 3;
    }
    return 4;
}

From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java

/**
 * Check if the device is connected to the Internet via Wi-Fi.
 * /*from   ww  w  .j  a  v  a2  s .  co m*/
 * @param context
 * @return
 */
public static boolean isWifiOnline(Context context) {
    return isOnline(context, ConnectivityManager.TYPE_WIFI);
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static boolean isConnectedToWifi(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (info == null) {
        return false;
    }/*from  w  ww  .  jav a2s .  com*/

    return info.isConnected();
}

From source file:edu.mecc.race2ged.helpers.Utils.java

/**
 * Are we connected to WiFi, Mobile Data, or nothing?
 * @return The devices current network state.
 *//*  ww  w .  j av  a  2s.  c o m*/
public static int getNetworkStatus(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi != null && wifi.isAvailable() && wifi.isConnected())
        return WIFI;
    else if (mobile != null && mobile.isAvailable() && mobile.isConnected())
        return MOBILE_DATA;
    return NO_CONNECTION;
}