android.net.DhcpInfo Java Examples

The following examples show how to use android.net.DhcpInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LFXNetworkUtils.java    From lifx-sdk-android with MIT License 6 votes vote down vote up
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 {
        return InetAddress.getByAddress(quads).getHostAddress();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return "255.255.255.255";
}
 
Example #2
Source File: Broadcaster.java    From Android with Apache License 2.0 6 votes vote down vote up
public static InetAddress getBroadcastAddress(Context context) throws UnknownHostException {
    if(isWifiApEnabled(context)) {
        return InetAddress.getByName("192.168.43.255");            
    }
    WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    if(dhcp==null) {
        return InetAddress.getByName("255.255.255.255");
    }
    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);
}
 
Example #3
Source File: Broadcast.java    From slide-android with GNU General Public License v2.0 6 votes vote down vote up
private InetAddress getBroadcastAddress()
    throws UnknownHostException
{
    final WifiManager wifi = (WifiManager) SettingsActivity.getActivity().getSystemService(
        Context.WIFI_SERVICE);
    final DhcpInfo dhcp = wifi.getDhcpInfo();
    if (dhcp == null)
    {
        return InetAddress.getByName("0.0.0.0");
    }
    final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.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);
}
 
Example #4
Source File: NetworkHelper.java    From orWall with GNU General Public License v3.0 6 votes vote down vote up
private static String getNetwork(DhcpInfo dhcp){
    int ip = dhcp.ipAddress;
    int mask = dhcp.netmask;
    if (ip == 0 || mask == 0) return null;

    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ip = Integer.reverseBytes(ip);
        mask = Integer.reverseBytes(mask);
    }

    ip &= mask;
    mask = netmaskToCIDR(mask);
    if (mask == 0) return null;

    int a = (ip >> 24) & 0xFF;
    int b = (ip >> 16) & 0xFF;
    int c = (ip >>  8) & 0xFF;
    int d = ip & 0xFF;

    return String.format(Locale.US, "%d.%d.%d.%d/%d", a, b, c, d, mask);
}
 
Example #5
Source File: Dns.java    From HttpInfo with Apache License 2.0 6 votes vote down vote up
private static String[] readDnsServersFromWifiManager(Context context) {
    LinkedList<String> dnsServers = new LinkedList<>();
    try {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager == null || !wifiManager.isWifiEnabled()) {
            return new String[0];
        }
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        if (dhcpInfo != null) {
            if (dhcpInfo.dns1 != 0) {
                dnsServers.add(intToIp(dhcpInfo.dns1));
            }
            if (dhcpInfo.dns2 != 0) {
                dnsServers.add(intToIp(dhcpInfo.dns2));
            }
        }
    } catch (Exception e) {
    }
    return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);
}
 
Example #6
Source File: LDNetUtil.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
/**
 * wifi状态下获取网关
 */
public static String pingGateWayInWifi(Context context) {
  String gateWay = null;
  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;
}
 
Example #7
Source File: NetworkUtils.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
public static InetAddress getBroadcastAddress(Context c) throws UnknownHostException {
    WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();

    if (dhcp == null) {
        return InetAddress.getByAddress(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);
    }

    return InetAddress.getByAddress(quads);
}
 
Example #8
Source File: NetworkUtil.java    From ShareBox with Apache License 2.0 6 votes vote down vote up
/**
 * result[0] is self ip,result[1] is host ip,result[2] is isWifiEnable,true or false.
 */
public static ArrayList<String> getWifiHostAndSelfIP(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String isWifiEnable;
    if (!wifiManager.isWifiEnabled()) {
        isWifiEnable = "false";
    } else
        isWifiEnable = "true";
    ArrayList<String> result = new ArrayList<>();
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String IPAddress = intToIp(wifiInfo.getIpAddress());
    result.add(IPAddress);

    DhcpInfo dhcpinfo = wifiManager.getDhcpInfo();
    String serverAddress = intToIp(dhcpinfo.serverAddress);
    result.add(serverAddress);

    result.add(isWifiEnable);
    return result;
}
 
Example #9
Source File: JsonSerializer.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
private JSONObject serializeDhcpInfo(DhcpInfo data) throws JSONException {
    JSONObject result = new JSONObject(mGson.toJson(data));
    int ipAddress = data.ipAddress;
    byte[] addressBytes = {
        (byte) (0xff & ipAddress),
        (byte) (0xff & (ipAddress >> 8)),
        (byte) (0xff & (ipAddress >> 16)),
        (byte) (0xff & (ipAddress >> 24))
    };
    try {
        String addressString = InetAddress.getByAddress(addressBytes).toString();
        result.put("IpAddress", addressString);
    } catch (UnknownHostException e) {
        result.put("IpAddress", ipAddress);
    }
    return result;
}
 
Example #10
Source File: NetworkUtils.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public static void getGatewayIpAddress(Context c) {
    // get wifi ip

    final WifiManager manager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    final DhcpInfo dhcp = manager.getDhcpInfo();
    final String address = Formatter.formatIpAddress(dhcp.gateway);

    StringBuilder IFCONFIG = new StringBuilder();
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                        && inetAddress.isSiteLocalAddress()) {
                    IFCONFIG.append(inetAddress.getHostAddress() + "\n");
                }

            }
        }
    } catch (SocketException ex) {
        Log.e("LOG_TAG", ex.toString());
    }
    MLog.d(TAG, "ifconfig " + IFCONFIG.toString());

    MLog.d(TAG, "hotspot address is " + address);

}
 
Example #11
Source File: Util.java    From ssj with GNU General Public License v3.0 5 votes vote down vote up
public static InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = (WifiManager)SSJApplication.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    if(dhcp == null)
        throw new IOException("dhcp is 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);
    return InetAddress.getByAddress(quads);
}
 
Example #12
Source File: LFXSocketGeneric.java    From lifx-sdk-android with MIT License 5 votes vote down vote up
public final static byte[] getBroadcastAddress(Context context) {
    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 quads;
}
 
Example #13
Source File: JsonSerializer.java    From mobly-bundled-snippets with Apache License 2.0 5 votes vote down vote up
public JSONObject toJson(Object object) throws JSONException {
    if (object instanceof DhcpInfo) {
        return serializeDhcpInfo((DhcpInfo) object);
    } else if (object instanceof WifiConfiguration) {
        return serializeWifiConfiguration((WifiConfiguration) object);
    } else if (object instanceof WifiInfo) {
        return serializeWifiInfo((WifiInfo) object);
    }
    return defaultSerialization(object);
}
 
Example #14
Source File: MainActivity.java    From Chorus-RF-Laptimer with MIT License 5 votes vote down vote up
private String getGatewayIP() {
    if (!checkIsWifiOnAndConnected()) return "0.0.0.0";

    WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    int ip = dhcp.gateway;
    return String.format("%d.%d.%d.%d",
            (ip & 0xff),
            (ip >> 8 & 0xff),
            (ip >> 16 & 0xff),
            (ip >> 24 & 0xff)
    );
}
 
Example #15
Source File: EnvironmentUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
public static String getRouterIpAddress(@NonNull Context context){
    try{
        WifiManager wifiManager=(WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcpInfo=wifiManager.getDhcpInfo();
        return Formatter.formatIpAddress(dhcpInfo.gateway);
    }catch (Exception e){
        e.printStackTrace();
    }
    return "192.168.1.1";
}
 
Example #16
Source File: WifiUtils.java    From tilt-game-android with MIT License 5 votes vote down vote up
public static byte[] getBroadcastIPAddressRaw(final Context pContext) throws WifiUtilsException {
	final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
	final DhcpInfo dhcp = wifiManager.getDhcpInfo();
	// TODO handle null somehow...

	final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
	final byte[] broadcastIP = new byte[IPUtils.IPV4_LENGTH];
	for (int k = 0; k < IPUtils.IPV4_LENGTH; k++) {
		broadcastIP[k] = (byte) ((broadcast >> (k * 8)) & 0xFF);
	}
	return broadcastIP;
}
 
Example #17
Source File: WifiHotUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取热点主机 IP 地址
 * @return 热点主机 IP 地址
 */
@SuppressLint("MissingPermission")
public String getHotspotServiceIp() {
    try {
        // 获取网关信息
        DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
        // 获取服务器地址
        return intToString(dhcpInfo.serverAddress);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getHotspotServiceIp");
    }
    return null;
}
 
Example #18
Source File: ContextUtils.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
/**
 * 判断WIFI是否连接
 *
 * @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;
}
 
Example #19
Source File: ContextUtils.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
/**
 * 获取WIFI IP地址
 *
 * @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);
}
 
Example #20
Source File: OneKeyWifi.java    From zhangshangwuda with Apache License 2.0 4 votes vote down vote up
public String getgateway() {
	DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
	String str1 = FormatIP(dhcpInfo.gateway);
	return str1;
}
 
Example #21
Source File: WifiFragmentSupport.java    From zhangshangwuda with Apache License 2.0 4 votes vote down vote up
public String getgateway() {
	DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
	String str1 = FormatIP(dhcpInfo.gateway);
	return str1;
}
 
Example #22
Source File: WifiUtils.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
public static String getServerIPAddress() {
    DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
    return intToIp(mDhcpInfo.gateway);
}
 
Example #23
Source File: WifiUtils.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
public static String getServerIPAddress() {
    DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
    return intToIp(mDhcpInfo.gateway);
}
 
Example #24
Source File: FragmentRouter.java    From Helepolis with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected String doInBackground(Void... ip) {
	DhcpInfo dhcpInfo = ((WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
	return intToIp(dhcpInfo.gateway);
}
 
Example #25
Source File: AndroidWifiModule.java    From react-native-android-wifi with ISC License 4 votes vote down vote up
@ReactMethod
public void getDhcpServerAddress(Callback callback) {
	DhcpInfo dhcpInfo = wifi.getDhcpInfo();
	String ip = longToIP(dhcpInfo.serverAddress);
	callback.invoke(ip);
}
 
Example #26
Source File: WifiWizard2.java    From WifiWizard2 with Apache License 2.0 4 votes vote down vote up
/**
 * Get WiFi Router IP from DHCP
 * @return
 */
private String getWiFiRouterIP() {
  DhcpInfo dhcp = wifiManager.getDhcpInfo();
  int ip = dhcp.gateway;
  return formatIP(ip);
}