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 getCurrentWifiLink(Context context) {
    if (context == null)
        return null;

    final WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    final WifiInfo connectedWifi = wifiMgr.getConnectionInfo();
    final String connectedSSID = connectedWifi == null ? null : connectedWifi.getSSID().replace("\"", "");
    return connectedSSID;
}

From source file:Main.java

/**
 * get the local ip address by Android System
 * /*  ww  w.j  av  a2 s  .c om*/
 * @param context
 *            the context
 * @return the local ip addr allocated by Ap
 */
public static InetAddress getLocalInetAddress(Context context) {
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wm.getConnectionInfo();
    int localAddrInt = wifiInfo.getIpAddress();
    String localAddrStr = __formatString(localAddrInt);
    InetAddress localInetAddr = null;
    try {
        localInetAddr = InetAddress.getByName(localAddrStr);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return localInetAddr;
}

From source file:Main.java

/**
 * Returns SSID of Wifi hotspot device connected with.
 * @param context/* www .j  a  v a 2s. c  o m*/
 * @return
 */
public static String getCurrentWifiConnectionSSID(Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null)
        return null;

    String ssid = wifiInfo.getSSID();
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
            ssid = ssid.substring(1, ssid.length() - 1);
        }
    }
    return ssid;
}

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  ww.  ja  v  a2 s  . c o  m
        byte[] ipAddress = convertIpAddress(ip);
        return InetAddress.getByAddress(ipAddress);
    }
}

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 .jav a2s. co  m
    return null;
}

From source file:Main.java

public static String getCurrentSsid(Context context) {
    String ssid = null;/*from w  ww  .j a v a2s  .  co  m*/
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && connectionInfo.getSSID() != null
                && !connectionInfo.getSSID().equals("")) {
            ssid = connectionInfo.getSSID();
        }
    }
    return ssid == null ? ssid : ssid.replace("\"", "");
}

From source file:Main.java

public static String getMacAddress(Context context) {
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wm == null)
        return null;

    WifiInfo info = wm.getConnectionInfo();
    return info == null ? null : info.getMacAddress();
}

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;
        }/* ww  w.  j av a2  s.c o m*/
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "."
                + (ipAddress >> 24 & 0xff));
    }
    return null;
}

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

public static String getLocalIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null && wifiInfo.getIpAddress() != 0) {
        return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress());
    } else {/* ww  w  .ja  v  a2 s .  c  om*/
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) {
                        String ipAddress = inetAddress.getHostAddress();
                        if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) {
                            return ipAddress;
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return null;
}