类android.net.DhcpInfo源码实例Demo

下面列出了怎么用android.net.DhcpInfo的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: HttpInfo   文件: Dns.java
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()]);
}
 
源代码2 项目: mobly-bundled-snippets   文件: JsonSerializer.java
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;
}
 
源代码3 项目: ShareBox   文件: NetworkUtil.java
/**
 * 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;
}
 
源代码4 项目: PHONK   文件: NetworkUtils.java
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);
}
 
源代码5 项目: AndroidHttpCapture   文件: LDNetUtil.java
/**
 * 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;
}
 
源代码6 项目: slide-android   文件: Broadcast.java
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);
}
 
源代码7 项目: Android   文件: Broadcaster.java
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);
}
 
源代码8 项目: orWall   文件: NetworkHelper.java
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);
}
 
源代码9 项目: lifx-sdk-android   文件: LFXNetworkUtils.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 {
        return InetAddress.getByAddress(quads).getHostAddress();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return "255.255.255.255";
}
 
源代码10 项目: DevUtils   文件: WifiHotUtils.java
/**
 * 获取热点主机 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;
}
 
源代码11 项目: apkextractor   文件: EnvironmentUtil.java
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";
}
 
源代码12 项目: Chorus-RF-Laptimer   文件: MainActivity.java
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)
    );
}
 
源代码13 项目: mobly-bundled-snippets   文件: JsonSerializer.java
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);
}
 
源代码14 项目: PHONK   文件: NetworkUtils.java
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);

}
 
源代码15 项目: ssj   文件: Util.java
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);
}
 
源代码16 项目: tilt-game-android   文件: WifiUtils.java
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;
}
 
源代码17 项目: ProjectX   文件: ContextUtils.java
/**
 * 判断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;
}
 
源代码18 项目: ProjectX   文件: ContextUtils.java
/**
 * 获取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);
}
 
源代码19 项目: lifx-sdk-android   文件: LFXSocketGeneric.java
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;
}
 
源代码20 项目: WifiWizard2   文件: WifiWizard2.java
/**
 * Get WiFi Router IP from DHCP
 * @return
 */
private String getWiFiRouterIP() {
  DhcpInfo dhcp = wifiManager.getDhcpInfo();
  int ip = dhcp.gateway;
  return formatIP(ip);
}
 
@ReactMethod
public void getDhcpServerAddress(Callback callback) {
	DhcpInfo dhcpInfo = wifi.getDhcpInfo();
	String ip = longToIP(dhcpInfo.serverAddress);
	callback.invoke(ip);
}
 
源代码22 项目: FlyWoo   文件: WifiUtils.java
public static String getServerIPAddress() {
    DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
    return intToIp(mDhcpInfo.gateway);
}
 
源代码23 项目: Helepolis   文件: FragmentRouter.java
@Override
protected String doInBackground(Void... ip) {
	DhcpInfo dhcpInfo = ((WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
	return intToIp(dhcpInfo.gateway);
}
 
源代码24 项目: zhangshangwuda   文件: WifiFragmentSupport.java
public String getgateway() {
	DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
	String str1 = FormatIP(dhcpInfo.gateway);
	return str1;
}
 
源代码25 项目: zhangshangwuda   文件: OneKeyWifi.java
public String getgateway() {
	DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
	String str1 = FormatIP(dhcpInfo.gateway);
	return str1;
}
 
源代码26 项目: WifiChat   文件: WifiUtils.java
public static String getServerIPAddress() {
    DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
    return intToIp(mDhcpInfo.gateway);
}
 
 类所在包
 同包方法