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 getWifiName(Context ctx) {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    Log.d("wifiInfo", wifiInfo.toString());
    Log.d("SSID", wifiInfo.getSSID());
    return wifiInfo.getSSID();
}

From source file:Main.java

public static String getMacAddress(Context context) {
    try {//from www . j  a v  a 2 s  .c o m
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String mac = info.getMacAddress();
        return mac == null ? "" : mac;
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

private static String getImei(Context context) {
    String imei = "";
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    if (info != null) {
        imei = info.getMacAddress();/*w  ww. j  ava  2 s  .c om*/
        if (imei != null) {
            return imei;
        }
    }
    return imei;
}

From source file:Main.java

public static String getMacAddress(Context context) {
    WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String macAddress = wimanager.getConnectionInfo().getMacAddress();
    if (macAddress == null) {
        //Device doesn't have mac address or wi-fi is disabled
        macAddress = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }//from w  w w.j a v a  2s . c  om
    return macAddress;
}

From source file:Main.java

public static String getCurrentSSID(Context context) {
    WifiManager wifiMan = (WifiManager) (context.getSystemService(Context.WIFI_SERVICE));
    WifiInfo wifiInfo = wifiMan.getConnectionInfo();

    if (wifiInfo != null)
        return wifiInfo.getSSID();
    else//  ww w . j  av a2s  .c  o  m
        return null;
}

From source file:Main.java

public static long getIPAdress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    long ipAddress = wifiInfo.getIpAddress();
    if (ipAddress == 0)
        ipAddress = 1001;//from  w  ww .  j  av  a2s . c  o m
    return ipAddress;
}

From source file:Main.java

public static String getMacAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo.getMacAddress() != null) {
        return wifiInfo.getMacAddress();
    } else {//w  w  w .  j  a  v  a  2 s .c  o  m
        return "";
    }
}

From source file:Main.java

public static String getCurrentWifiSSID(Context context) {
    if (context == null) {
        return "";
    }/*  ww w .ja  v  a  2  s  .  co m*/
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    if (info != null) {
        return info.getSSID() == null ? "" : info.getSSID();
    } else {
        return "";
    }
}

From source file:Main.java

public static String getMacAddress(Context context) {
    String mac = "";
    try {/*from   w w  w  .j av  a  2s  .com*/
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();
        if (mac == null || mac.equalsIgnoreCase("null")) {
            mac = "";
        }
        return mac;
    } catch (Exception e) {
        return mac;
    }
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
 * and {@link android.Manifest.permission#CHANGE_WIFI_STATE} permissions.
 *
 * @param context//from  ww  w  . j a v a2s.co  m
 * @return
 */
@SuppressLint("MissingPermission")
public static int disconnectWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null && ni.isConnectedOrConnecting()) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int id = wm.getConnectionInfo().getNetworkId();
        return wm.disconnect() ? id : -1;
    }

    return -1;
}