Example usage for android.content Context WIFI_SERVICE

List of usage examples for android.content Context WIFI_SERVICE

Introduction

In this page you can find the example usage for android.content Context WIFI_SERVICE.

Prototype

String WIFI_SERVICE

To view the source code for android.content Context WIFI_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.net.wifi.WifiManager for handling management of Wi-Fi access.

Usage

From source file:Main.java

public static String getConnectWifiSsid(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        return wifiInfo.getSSID();
    }/*from w  ww  .ja  va2 s.c  om*/
    return null;
}

From source file:Main.java

static public String getMacAddress(Context context, ConnectivityManager connMananger) {
    Log.d(TAG, "getMacAddress");

    String macAddress = "";

    NetworkInfo info = connMananger.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        int type = info.getType();

        Log.d(TAG, "connected type = " + type + " name " + info.getTypeName());

        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            macAddress = wifiInfo.getMacAddress();
        } else if (type == ConnectivityManager.TYPE_ETHERNET || type == ConnectivityManager.TYPE_MOBILE) {
            String addressFileName = "/sys/class/net/eth0/address";
            File addressFile = new File(addressFileName);
            Log.d(TAG, "1");

            if (addressFile.exists()) {
                Log.d(TAG, "2");
                macAddress = readString(addressFile);
                Log.d(TAG, macAddress);/*w  w w  . ja va2  s.c  o m*/
            } else {
                addressFileName = "/sys/class/net/eth1/address";
                addressFile = new File(addressFileName);
                if (addressFile.exists()) {
                    macAddress = readString(addressFile);
                    Log.d(TAG, macAddress);
                }
            }
        } else {
            //other type;
        }
    }
    return macAddress;
}

From source file:Main.java

public static String getConnectWifiIp(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0) {
            return null;
        }/*from  w w w . ja  v  a  2 s  .com*/
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "."
                + (ipAddress >> 24 & 0xff));
    }
    return null;
}

From source file:Main.java

public static String getIP(Context ctx) {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.isWifiEnabled() ? getWifiIP(wifiManager) : getGPRSIP();
}

From source file:Main.java

public static InetAddress getIpAddress(Context context) throws UnknownHostException {
    WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();

    if (ip == 0) {
        return null;
    } else {//w  w  w.  j  a v a  2 s.  co m
        byte[] ipAddress = convertIpAddress(ip);
        return InetAddress.getByAddress(ipAddress);
    }
}

From source file:Main.java

public static String getMyDeviceId(Context context) {
    try {//from w  w  w  .j  av  a2 s.  c om
        WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMan.getConnectionInfo();

        return Integer.toString(Math.abs(wifiInfo.getMacAddress().hashCode()));
    } catch (Exception e) {
        return "000000000000000000000";
    }

}

From source file:Main.java

private static String getIpUnderWifi() {
    WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
    Log.w(TAG, "getIp  isWifiConnected  ipaddress=" + ipAddress);
    return String.format("%d.%d.%d.%d", ipAddress & 0xff, ipAddress >> 8 & 0xff, ipAddress >> 16 & 0xff,
            ipAddress >> 24 & 0xff);
}

From source file:Main.java

/** get IP of the gateway this device is connected to (useful if gateway has server running) */
public static String getHostIpAddress(Context mContext) {

    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wifi.getDhcpInfo().gateway);

    return ip;//from   www  . j a  v a  2s  .  co m

}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {// w w  w.  ja va2s.  c om

        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null) {

                isNetworkAvailable = netInfo.isConnected();
                //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
            }
        }
    } catch (Exception ex) {
        //Log.e("Network Avail Error", ex.getMessage());

    }
    //        check for wifi also
    if (!isNetworkAvailable) {

        WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        State wifi = cm.getNetworkInfo(1).getState();
        if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
            isNetworkAvailable = true;
            //Log.d("WIFI NETWORK<<","WIFI connected");
        } else {

            isNetworkAvailable = false;
            // Log.d("WIFI Network<<","WIFI not connected");
        }

    }
    return isNetworkAvailable;

}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    if (context != null) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        try {// ww  w .  j ava  2 s.  com

            if (cm != null) {
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null) {

                    isNetworkAvailable = netInfo.isConnected();
                    //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                    //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
                }
            }
        } catch (Exception ex) {
            //Log.e("Network Avail Error", ex.getMessage());

        }
        //        check for wifi also
        if (!isNetworkAvailable) {

            WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            State wifi = cm.getNetworkInfo(1).getState();
            if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
                isNetworkAvailable = true;
                //Log.d("WIFI NETWORK<<","WIFI connected");
            } else {

                isNetworkAvailable = false;
                // Log.d("WIFI Network<<","WIFI not connected");
            }

        }
    }

    return isNetworkAvailable;

}