Example usage for android.net.wifi WifiManager getConnectionInfo

List of usage examples for android.net.wifi WifiManager getConnectionInfo

Introduction

In this page you can find the example usage for android.net.wifi WifiManager getConnectionInfo.

Prototype

public WifiInfo getConnectionInfo() 

Source Link

Document

Return dynamic information about the current Wi-Fi connection, if any is active.

Usage

From source file:Main.java

public static String GetMacAddress(Context context) {
    String macAddress = null;//w w w. ja v a  2s  .c om
    WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
    if (info != null) {
        macAddress = info.getMacAddress();
    }
    return macAddress;
}

From source file:Main.java

public static String getDeviceInfo(Context context) {
    try {//from w  w  w  .j av a  2  s .co  m
        org.json.JSONObject json = new org.json.JSONObject();
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String device_id = tm.getDeviceId();
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        String mac = wifi.getConnectionInfo().getMacAddress();
        json.put("mac", mac);
        if (TextUtils.isEmpty(device_id)) {
            device_id = mac;
        }
        if (TextUtils.isEmpty(device_id)) {
            device_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        json.put("device_id", device_id);
        return json.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Returns true if WiFi connection was established
 * @param context - used to get connectivity manger
 * @return true if WiFi connection established, otherwise - false.
 *//*from  w w  w. ja  v a  2s .  com*/
public static boolean isWifiConnected(Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null)
        return false;

    return wifiInfo.getIpAddress() != 0;
}

From source file:Main.java

public static String getSSid(Context ctx) {
    WifiManager wm = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    if (wm != null) {
        WifiInfo wi = wm.getConnectionInfo();
        if (wi != null) {
            String s = wi.getSSID();
            if (s.length() > 2 && s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
                return s.substring(1, s.length() - 1);
            }//w  w w  .  j a  v  a2 s . c o m
        }
    }
    return "";
}

From source file:Main.java

/**
 * Returns MAC address of WiFi module.//from  w  ww.j  av  a2  s.co m
 *
 * @param context - used to get WiFi manager
 */
public static String getMacAddress(Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null)
        return null;

    return wifiInfo.getMacAddress().toUpperCase();
}

From source file:Main.java

public static String getMyDeviceId(Context context) {
    try {//from w  ww.j  a  va  2  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

public static String getMacInfo(Context context) {
    String macAddress = null, ip = null;
    WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
    if (null != info) {
        macAddress = info.getMacAddress();

    }//from   w  w  w  . j a v a 2s  . com
    return macAddress;
}

From source file:Main.java

public static String getDeviceId(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String uuid = tm.getDeviceId();
    if (uuid == null) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        String mac = wifiManager.getConnectionInfo().getMacAddress();
        if (mac == null)
            throw new IllegalStateException();
        return mac;
    }//ww w.j ava  2 s .  c  om
    return uuid;
}

From source file:Main.java

public static String getWifiMac1(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifi != null) {
        WifiInfo info = wifi.getConnectionInfo();
        String mac = info.getMacAddress();
        return mac;
    }// ww w  .j a va 2  s .com

    return "";
}

From source file:Main.java

public static String checkWifiConnection(final Activity activity) throws Exception {
    ConnectivityManager connManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (!mWifi.isConnected()) {
        new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
                .setMessage("Wifi not connected. Application will exit.")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override/* w  w w.j  a  v a2 s .  co m*/
                    public void onClick(DialogInterface dialog, int which) {
                        //activity.finish();
                    }
                }).show();
        throw new Exception("Wifi");
    }
    WifiManager wifiMgr = (WifiManager) activity.getSystemService(Activity.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
}