Example usage for android.net.wifi WifiInfo getIpAddress

List of usage examples for android.net.wifi WifiInfo getIpAddress

Introduction

In this page you can find the example usage for android.net.wifi WifiInfo getIpAddress.

Prototype

public int getIpAddress() 

Source Link

Usage

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//from   w ww.j a v  a 2s. c om
                    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));
}

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 {/*from   ww  w.  j  av a 2  s .co  m*/
        byte[] ipAddress = convertIpAddress(ip);
        return InetAddress.getByAddress(ipAddress);
    }
}

From source file:Main.java

public static String getCurrentWifiIP(Application app) {
    WifiManager wifiManager = (WifiManager) app.getSystemService(Application.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        return null;
    }//from   w w  w  . ja v a  2  s. c o m

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    int ipAddress = wifiInfo.getIpAddress();
    String strIPAddess = ((ipAddress >> 0) & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "."
            + ((ipAddress >> 16) & 0xFF) + "." + ((ipAddress >> 24) & 0xFF);

    return strIPAddess;
}

From source file:Main.java

/**
 * get the local ip address by Android System
 * /*www.j  av  a2 s.  c  o m*/
 * @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

public static boolean isWiFiActive(Context inContext) {
    WifiManager mWifiManager = (WifiManager) inContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
    if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
        System.out.println("**** WIFI is on");
        return true;
    } else {/*  w  w  w .j  av  a 2s .  co m*/
        System.out.println("**** WIFI is off");
        return false;
    }
}

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  a  va 2 s .c  o  m*/
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "."
                + (ipAddress >> 24 & 0xff));
    }
    return null;
}

From source file:Main.java

public static String getWifiIP(WifiManager wifiManager) {
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ip = intToIp(wifiInfo.getIpAddress());
    return ip != null ? ip : "";
}

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 {/*from  w ww . jav  a2 s. c o m*/
        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;
}

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   www .  j  av  a 2s .c o  m
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 getLocalIpByWifi(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null) {
        return "wifiManager not found";
    }//w  w w .j  a  v  a 2s.  c  o m
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null) {
        return "wifiInfo not found";
    }
    int ipAddress = wifiInfo.getIpAddress();
    return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
            (ipAddress >> 24 & 0xff));
}