Java Code Examples for android.net.wifi.WifiManager#getDhcpInfo()

The following examples show how to use android.net.wifi.WifiManager#getDhcpInfo() . 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: 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 2
Source File: AppContext.java    From Lanmitm with GNU General Public License v2.0 6 votes vote down vote up
public static void initWifiInfo() {
	WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
	int_ip = wifiManager.getDhcpInfo().ipAddress;
	int_net_mask = wifiManager.getDhcpInfo().netmask;
	/**获取不到子网掩码,nexus5实测,偶尔拿不到**/
	if (int_net_mask == 0) {
		int_net_mask = (0 << 24) + (0xff << 16) + (0xff << 8) + 0xff ;
	}
	int_gateway = wifiManager.getDhcpInfo().gateway;
	try {
		mInetAddress = InetAddress.getByName(NetworkUtils.netfromInt(int_ip));
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}

	gatewayMac = wifiManager.getConnectionInfo().getBSSID().replace('-', ':');
}
 
Example 3
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 4
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 5
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 6
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 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: 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 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: 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 12
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 13
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 14
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 15
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 16
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 17
Source File: LANScanModule.java    From react-native-lanscan with MIT License 4 votes vote down vote up
private boolean getInfo() {
    sendEvent(getReactApplicationContext(), EVENT_STARTFETCH, null);

    WifiManager wifi_manager= (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE);
    dhcp_info=wifi_manager.getDhcpInfo();

    try {
        String s_dns1 = intToIp(dhcp_info.dns1);
        String s_dns2 = intToIp(dhcp_info.dns2);
        String s_gateway = intToIp(dhcp_info.gateway);
        String s_ipAddress = intToIp(dhcp_info.ipAddress);
        int s_leaseDuration = dhcp_info.leaseDuration;
        String s_netmask = intToIp(dhcp_info.netmask);
        String s_serverAddress = intToIp(dhcp_info.serverAddress);

        WritableMap device_info = new WritableNativeMap();
        device_info.putString(KEY_WIFISTATE_DNS1, s_dns1);
        device_info.putString(KEY_WIFISTATE_DNS2, s_dns2);
        device_info.putString(KEY_WIFISTATE_GATEWAY, s_gateway);
        device_info.putString(KEY_WIFISTATE_IPADDRESS, s_ipAddress);
        device_info.putInt(KEY_WIFISTATE_LEASEDURATION, s_leaseDuration);
        device_info.putString(KEY_WIFISTATE_NETMASK, s_netmask);
        device_info.putString(KEY_WIFISTATE_SERVERADDRESS, s_serverAddress);

        ipv4_wifi = new IPv4(s_ipAddress, s_netmask);
        hosts_list = ipv4_wifi.getHostAddressList();

        device_info.putInt(KEY_IPv4_HOSTSNUMBER, ipv4_wifi.getNumberOfHosts().intValue());

        sendEvent(getReactApplicationContext(), EVENT_INFOFETCHED, device_info);

        return true;
    } catch (UnknownHostException e) {

        sendEvent(getReactApplicationContext(), EVENT_FETCHERROR, e.getMessage());
        sendEvent(getReactApplicationContext(), EVENT_ERROR, e.getMessage());
    }

    return false;

}