Example usage for android.net.wifi WifiManager getDhcpInfo

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

Introduction

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

Prototype

public DhcpInfo getDhcpInfo() 

Source Link

Document

Return the DHCP-assigned addresses from the last successful DHCP request, if any.

Usage

From source file:Main.java

public static String getBroadcastAddress(Context mContext) throws IOException {
    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads).getHostAddress().toString();
}

From source file:Main.java

public static InetAddress getBroadcastAddress(Context context) throws UnknownHostException {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    if (dhcp == null) {
        return InetAddress.getByName("255.255.255.255");
    }/*  www  .ja v a 2  s  .com*/
    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}

From source file:Main.java

public static String getBroadcastAddress(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) (broadcast >> (k * 8));
    try {/*www.  j a v  a 2 s .  c o m*/
        return InetAddress.getByAddress(quads).getHostAddress();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return "255.255.255.255";
}

From source file:Main.java

/**
 * Returns the wi-fi broadcast address// w  w  w.  jav  a  2s .c  o m
 *
 * @param context the app context
 * @return returns an the broadcast address
 * @throws IOException
 */
public static InetAddress getBroadcastAddress(Context context) throws IOException {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}

From source file:Main.java

public static String getBroadcastAddress(Context ctx) {
    WifiManager cm = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo myDhcpInfo = cm.getDhcpInfo();
    if (myDhcpInfo == null) {
        return "255.255.255.255";
    }/* w  ww .  j  ava  2 s. c  o  m*/
    int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask) | ~myDhcpInfo.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    try {
        return InetAddress.getByAddress(quads).getHostAddress();
    } catch (Exception e) {
        return "255.255.255.255";
    }
}

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 ww w.j  a v a 2s  . com

}

From source file:Main.java

public static String pingGateWayInWifi(Context context) {
    String gateWay = null;//from w w  w.  ja  va  2  s.c  o  m
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null) {
        return "wifiManager not found";
    }
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    if (dhcpInfo != null) {
        int tmp = dhcpInfo.gateway;
        gateWay = String.format("%d.%d.%d.%d", (tmp & 0xff), (tmp >> 8 & 0xff), (tmp >> 16 & 0xff),
                (tmp >> 24 & 0xff));
    }
    return gateWay;
}

From source file:Main.java

public static String getGateWayAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    boolean enable = wifiManager.isWifiEnabled();
    if (!enable) {
        return null;
    }//from   w w  w.j a  v  a  2  s  . c  o m
    DhcpInfo info = wifiManager.getDhcpInfo();
    return intToIp(info.gateway);
}

From source file:Main.java

public static String getDhcpIpString(Context mContext) {
    WifiManager mWifiManager;
    String broadcastIp = null;//w  w w .  j a va2 s. co m
    mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (mWifiManager.isWifiEnabled()) {
        DhcpInfo myDhcpInfo = mWifiManager.getDhcpInfo();
        if (myDhcpInfo == null) {
            Toast.makeText(mContext, "can not get dhcp info", Toast.LENGTH_SHORT).show();
            return null;
        } else {
            try {
                broadcastIp = getBroadcastAddress(myDhcpInfo).getHostAddress();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        return broadcastIp;
    }
    return null;
}

From source file:am.project.x.utils.ContextUtils.java

/**
 * WIFI?/*from   w  ww.  j  a va  2  s.c o  m*/
 *
 * @param context Context
 * @return true: false:
 */
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isWifiConnected(Context context) {
    final WifiManager manager = (WifiManager) context.getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    if (manager == null)
        return false;
    final DhcpInfo info = manager.getDhcpInfo();
    return info != null && info.ipAddress != 0;
}