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:am.project.x.utils.ContextUtils.java

/**
 * ?WIFI IP?/*from  w w  w  . j  a va2 s .  com*/
 *
 * @param context Context
 * @return IP?
 */
public static String getWifiIp(Context context) {
    final WifiManager manager = (WifiManager) context.getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    if (manager == null)
        return "0.0.0.0";
    final DhcpInfo info = manager.getDhcpInfo();
    if (info == null)
        return "0.0.0.0";
    final int ip = info.ipAddress;
    return (0xFF & ip) + "." + (0xFF & ip >> 8) + "." + (0xFF & ip >> 16) + "." + (0xFF & ip >> 24);
}

From source file:org.rm3l.ddwrt.utils.Utils.java

@Nullable
public static InetAddress getBroadcastAddress(@Nullable final WifiManager wifiManager) throws IOException {
    if (wifiManager == null) {
        return null;
    }/*from  w  w w  . j a v  a  2 s . c  om*/
    final DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    if (dhcpInfo == null) {
        return null;
    }
    final int broadcast = (dhcpInfo.ipAddress & dhcpInfo.netmask) | ~dhcpInfo.netmask;
    final 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:totalcross.android.ConnectionManager4A.java

public static String getLocalHost() {
    try {//from w w w.j  ava2 s .  c o m
        ConnectivityManager connMgr = (ConnectivityManager) Launcher4A.loader
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        int type = connMgr.getActiveNetworkInfo().getType();
        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiMgr = (WifiManager) Launcher4A.loader.getSystemService(Context.WIFI_SERVICE);
            return ipAddressToString(wifiMgr.getDhcpInfo().ipAddress);
        }
        String ip = getLocalIpAddress();
        return ip != null ? ip : InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
    }
    return "127.0.0.1";
}

From source file:org.basdroid.common.NetworkUtils.java

public static String getApIpAddr(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
    try {/*from w w w .  j  a v a2  s . c o m*/
        String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress();
        return apIpAddr;
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:us.nineworlds.serenity.core.services.GDMService.java

protected InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = (WifiManager) 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);
    return InetAddress.getByAddress(quads);
}

From source file:com.ionicsdk.discovery.Discovery.java

InetAddress getBroadcastAddress() throws Exception {
    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);
    InetAddress addr = InetAddress.getByAddress(quads);

    Log.d(TAG, "Got broadcast addr:" + addr);
    return addr;/* w  ww  . j a  v a2 s . c o  m*/
}

From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java

private String getWiFiBroadcastAddress(Context context) {
    String bcastaddr = null;/*from w ww .  j a v  a 2 s .  com*/
    WifiManager mWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = mWifi.getDhcpInfo();

    if (mWifi.isWifiEnabled() && dhcp != null) {
        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);

        try {
            bcastaddr = InetAddress.getByAddress(quads).getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return bcastaddr;
}

From source file:org.brandroid.openmanager.fragments.DialogHandler.java

public static String getNetworkInfo(Context c) {
    String ret = getNetworkInterfaces();

    ConnectivityManager conman = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conman == null)
        ret += "No Connectivity?\n";
    else {/*w  ww .  j a va  2 s. c o m*/
        ret += "Connectivity Info:\n" + conman.toString() + "\n";
        for (NetworkInfo ni : conman.getAllNetworkInfo()) {
            if (!ni.isAvailable())
                continue;
            ret += "Network [" + ni.getTypeName() + "]: " + getNetworkInfoInfo(ni) + "\n";
        }
    }
    try {
        WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
        if (wifi == null)
            ret += "No wifi\n";
        else {
            ret += "Wifi Info:\n" + wifi.toString() + "\n";
            ret += "Status: " + (wifi.isWifiEnabled() ? "ENABLED" : "DISABLED") + "\n";
            ret += "ip=" + getReadableIP(wifi.getConnectionInfo().getIpAddress()) + "/ " + "mac="
                    + wifi.getConnectionInfo().getMacAddress() + "/ " + "b="
                    + wifi.getConnectionInfo().getBSSID() + "/ " + "s=" + wifi.getConnectionInfo().getSSID();
            DhcpInfo dh = wifi.getDhcpInfo();
            if (dh == null)
                ret += "No DHCP\n";
            else {
                ret += "IP: " + getReadableIP(dh.ipAddress) + "\n";
                ret += "Gateway: " + getReadableIP(dh.gateway) + "\n";
                ret += "DNS: " + getReadableIP(dh.dns1) + " " + getReadableIP(dh.dns2) + "\n";
            }
        }
    } catch (SecurityException sec) {
        ret += "No Wifi permissions.\n";
    }
    return ret;
}

From source file:com.ifoer.util.NetPOSPrinter.java

public int printPic(Bitmap bmp) {
    WifiManager wifi_service = (WifiManager) this.mContext.getSystemService("wifi");
    DhcpInfo dhcpinfo = wifi_service.getDhcpInfo();
    WifiInfo wifi_info = wifi_service.getConnectionInfo();
    if (wifi_info != null && wifi_info.getSSID() != null && !wifi_info.getSSID().startsWith("X-431PRINTER")) {
        return PRINT_NOT_CONNECT;
    }/*from  w w w .j  a va 2 s. c o m*/
    this.serverAddress = dhcpinfo.serverAddress;
    try {
        this.wifiSocket = new Socket(Formatter.formatIpAddress(this.serverAddress), PRINT_PORT);
        this.dos = new DataOutputStream(this.wifiSocket.getOutputStream());
        this.in = new DataInputStream(this.wifiSocket.getInputStream());
    } catch (UnknownHostException e1) {
        e1.printStackTrace();
    } catch (IOException e12) {
        e12.printStackTrace();
    }
    if (this.dos == null || this.in == null) {
        return PRINT_NOT_CONNECT;
    }
    byte[] data = new byte[3];
    data[0] = (byte) 27;
    data[ERROR_PRINT_JAM] = (byte) 51;
    try {
        this.dos.write(data, 0, data.length);
        data[0] = (byte) 0;
        data[ERROR_PRINT_JAM] = (byte) 0;
        data[ERROR_PRINT_WILL_NO_PAPER] = (byte) 0;
    } catch (IOException e122) {
        e122.printStackTrace();
    }
    byte[] escj = new byte[3];
    escj[0] = (byte) 27;
    escj[ERROR_PRINT_JAM] = (byte) 74;
    int i = 3;
    byte[] esccheck = new byte[] { (byte) 29, (byte) 114, (byte) 73 };
    byte[] escBmp = new byte[5];
    escBmp[0] = (byte) 27;
    escBmp[ERROR_PRINT_JAM] = (byte) 42;
    escBmp[ERROR_PRINT_WILL_NO_PAPER] = SmileConstants.TOKEN_LITERAL_NULL;
    escBmp[3] = (byte) (bmp.getWidth() % KEYRecord.OWNER_ZONE);
    escBmp[ERROR_PRINT_NO_PAPER] = (byte) (bmp.getWidth() / KEYRecord.OWNER_ZONE);
    for (int i2 = 0; i2 < (bmp.getHeight() / 24) + ERROR_PRINT_JAM; i2 += ERROR_PRINT_JAM) {
        try {
            this.dos.write(escBmp, 0, escBmp.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int j = 0; j < bmp.getWidth(); j += ERROR_PRINT_JAM) {
            for (int k = 0; k < 24; k += ERROR_PRINT_JAM) {
                if ((i2 * 24) + k < bmp.getHeight()) {
                    if (Color.red(bmp.getPixel(j, (i2 * 24) + k)) == 0) {
                        int i3 = k / ERROR_PRINT_ACTUATOR_FAULT;
                        data[i3] = (byte) (data[i3]
                                + ((byte) (ERROR_PRINT_HEAD_OVERHEATING >> (k % ERROR_PRINT_ACTUATOR_FAULT))));
                    }
                }
            }
            try {
                this.dos.write(data, 0, data.length);
                data[0] = (byte) 0;
                data[ERROR_PRINT_JAM] = (byte) 0;
                data[ERROR_PRINT_WILL_NO_PAPER] = (byte) 0;
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        try {
            if (i2 % 10 == 0) {
                this.dos.write(esccheck);
                if (this.in.readByte() == null) {
                    this.dos.write(escj, 0, escj.length);
                }
            } else {
                this.dos.write(escj, 0, escj.length);
            }
        } catch (IOException e22) {
            e22.printStackTrace();
        }
    }
    i = 3;
    byte[] escf = new byte[] { (byte) 29, (byte) 122, (byte) 49 };
    i = 3;
    byte[] esck = new byte[] { (byte) 27, (byte) 74, (byte) 64 };
    try {
        this.dos.write(escf);
        this.dos.write(esck);
        this.result = this.in.readByte();
        try {
            this.dos.close();
            this.in.close();
        } catch (IOException e222) {
            e222.printStackTrace();
        }
        return this.result;
    } catch (IOException e3) {
        return PRINT_NOT_CONNECT;
    }
}

From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java

private void refreshData() {
    Context context = getActivity();
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    mNetworkInfo = connectivityManager.getActiveNetworkInfo();
    mNetworkInterfaceInfos = getNetworkInterfaceInfos();
    mDNSes = getActiveNetworkDnsResolvers(context);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    mWifiEnabled = wifiManager.isWifiEnabled();
    mWifiInfo = wifiManager.getConnectionInfo();
    mDhcpInfo = wifiManager.getDhcpInfo();
}