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; }
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); }
private String getBroadcastAddress(Context ctx) /* */ { /* 109 */ WifiManager cm = (WifiManager)ctx /* 110 */ .getSystemService("wifi"); /* 111 */ DhcpInfo myDhcpInfo = cm.getDhcpInfo(); /* 112 */ if (myDhcpInfo == null) { /* 113 */ return "255.255.255.255"; /* */ } /* */ /* */ /* 117 */ int broadcast = myDhcpInfo.ipAddress & myDhcpInfo.netmask | /* 118 */ myDhcpInfo.netmask ^ 0xFFFFFFFF; /* 119 */ byte[] quads = new byte[4]; /* 120 */ for (int k = 0; k < 4; k++) /* 121 */ quads[k] = ((byte)(broadcast >> k * 8 & 0xFF)); /* */ try { /* 123 */ return InetAddress.getByAddress(quads).getHostAddress(); /* */ } catch (Exception e) {} /* 125 */ return "255.255.255.255"; /* */ }
@Override public InetAddress getBroadcastAddress() { try { WifiManager wifi = (WifiManager) Global.getContext().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) & 0xFF); } return InetAddress.getByAddress(quads); } catch (UnknownHostException ex) { Log.e(AndroidSystemServiceImpl.class.getName(), "Could not getBroadcastAddress!", ex); } return null; }
/** * Calculate the broadcast IP we need to send the packet along. If we send it * to 255.255.255.255, it never gets sent. I guess this has something to do * with the mobile network not wanting to do broadcast. */ private InetAddress getBroadcastAddress() throws IOException { Log.i(TAG, "Entree getBroadcastAddress"); WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE); DhcpInfo dhcp = wm.getDhcpInfo(); if (dhcp == null) { Log.d(TAG, "Could not get dhcp info"); return null; } Log.d(TAG, "ipAddress::" + BigInteger.valueOf(wm.getDhcpInfo().netmask).toString()); 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); Log.d(TAG, "BroadcastAddress:" + getByAddress(quads).getHostAddress()); return getByAddress(quads); }
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"; }
public static InetAddress getDeviceSubnetMask(Context ctx) { WifiManager wifii = (WifiManager) ctx.getApplicationContext().getSystemService(Context.WIFI_SERVICE); DhcpInfo d = wifii.getDhcpInfo(); //A volte non funziona e torna 0. if (d.netmask == 0) { InetAddress localHost = null; try { NetworkInterface networkInterface = NetworkInterface.getByInetAddress(NetUtils.intToInet(d.ipAddress)); //get(1) e` IP4 byte[] subDario = subnetLenghtToSubnetAddressDario(networkInterface.getInterfaceAddresses().get(1).getNetworkPrefixLength()); Log.w(Constants.TAG, "Emergency subnet recovery from IP Address mask lenght:" + networkInterface.getInterfaceAddresses().get(1).getNetworkPrefixLength()); return Inet4Address.getByAddress(subDario); } catch (Exception e) { Log.e(Constants.TAG, "Errore nel recupero subnet mask per: " + d.ipAddress); e.printStackTrace(); } } return intToInet(d.netmask); }
private int wifiDhcpGatewayAddress() { if (mConnectionType != ConnectionType.WIFI) { return 0; } if (null == mApplicationContext) { return 0; } try { WifiManager mgr = (WifiManager) mApplicationContext.getSystemService(Context.WIFI_SERVICE); DhcpInfo d = mgr.getDhcpInfo(); if (d == null) { return 0; } return d.gateway; } catch (Exception ex) { // getDhcpInfo() is not documented to require any permissions, but on some devices // requires android.permission.ACCESS_WIFI_STATE. Just catch the generic exception // here and returning 0. Not logging because this could be noisy. return 0; } }
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); }
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; }
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"; }
private String getWiFiBroadcastAddress (Context context) { String bcastaddr = null; 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; }
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); }
private InetAddress getBroadcastAddress() { DhcpInfo dhcp = wifiManager.getDhcpInfo(); if (dhcp == null) { Log.d(TAG, "Could not get dhcp info"); return 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 { return InetAddress.getByAddress(quads); } catch (UnknownHostException e) { e.printStackTrace(); throw new RuntimeException(e); } }
public ConnectionInfo getConnectionInfo() { ConnectionInfo info = new ConnectionInfo(); WifiInfo winf = mWifiMgr.getConnectionInfo(); info.winf = winf; if (winf != null) { if (winf.getSSID() != null) { String winfSsid = unQuote(winf.getSSID()); if (winfSsid.equals(mNetssid)) { DhcpInfo dhinf = mWifiMgr.getDhcpInfo(); info.dhinf = dhinf; } } } return info; }
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) ); }
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); }
public static String getRouterIp(Context context){ WifiManager wifiService = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if(wifiService == null){ return null; } DhcpInfo dhcpInfo = wifiService.getDhcpInfo(); return Formatter.formatIpAddress(dhcpInfo.gateway); }
private static JSONObject jsonifyDhcpInfo(DhcpInfo info) throws JSONException { JSONObject obj = new JSONObject(); obj.put("dns1", ipToString(info.dns1) ); obj.put("dns2", ipToString(info.dns2) ); obj.put("gateway", ipToString(info.gateway) ); obj.put("ip", ipToString(info.ipAddress) ); obj.put("lease", info.leaseDuration ); obj.put("netmask", ipToString(info.netmask) ); obj.put("server", ipToString(info.serverAddress) ); return obj; }
private DhcpInfo createDhcpInfo(IPInfo ip) { DhcpInfo i = new DhcpInfo(); i.ipAddress = ip.ip_hex; i.netmask = ip.netmask_hex; i.dns1 = 0x04040404; i.dns2 = 0x08080808; return i; }
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().toString() + "\n"); } } } } catch (SocketException ex) { Log.e("LOG_TAG", ex.toString()); } MLog.d(TAG, "ifconfig " + IFCONFIG.toString()); MLog.d(TAG, "hotspot address is " + address); }
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); }
/** * Calculate the broadcast IP we need to send the packet along. If we send it * to 255.255.255.255, it never gets sent. I guess this has something to do * with the mobile network not wanting to do broadcast. */ private InetAddress getBroadcastAddress() throws IOException { DhcpInfo dhcp = mWifi.getDhcpInfo(); if (dhcp == null) { //Log.d("InetAddress getBroadcastAddress()", "Could not get dhcp info"); return 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); }
public 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; }
public static String getBroadcastAddress(Context context) { if (sBroadcastAddress != null && sBroadcastAddressExpires > System.currentTimeMillis()) { return sBroadcastAddress; } WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifi != null) { DhcpInfo dhcp = wifi.getDhcpInfo(); if (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)); try { sBroadcastAddress = InetAddress.getByAddress(quads).getHostAddress(); sBroadcastAddressExpires = System.currentTimeMillis() + EXPIRE_TIME; return sBroadcastAddress; } catch (UnknownHostException e) { e.printStackTrace(); } } } sBroadcastAddress = "255.255.255.255"; sBroadcastAddressExpires = System.currentTimeMillis() + EXPIRE_TIME; return sBroadcastAddress; }
/** * Returns the Gateway IP of current network. */ public static String refreshGateway(Context context) { WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); return Utils.parseIp(dhcpInfo.gateway); }
/** * 获取设备IP地址 * * @param context 上下文 * @return IP地址 */ public static String getDeviceIpAddress(Context context) { String ipAddress = "UNKNOWN"; if (isNetworkAvailable(context)) { WifiManager wifiInfo = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo d = wifiInfo.getDhcpInfo(); if (d != null) { ipAddress = String.valueOf(d.ipAddress); } } return ipAddress; }
private String getIPAddress() throws IOException { WifiManager wifiManager = (WifiManager) FAIMSApplication.getInstance().getApplication().getSystemService(Application.WIFI_SERVICE); DhcpInfo myDhcpInfo = wifiManager.getDhcpInfo(); if (myDhcpInfo == null) { FLog.d("could not determine device ip"); return null; } int broadcast = myDhcpInfo.ipAddress; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); return InetAddress.getByAddress(quads).getHostAddress(); }
@SuppressWarnings({ "unused", "deprecation" }) public String getGatewayIPAddress(Context ctx) { WifiManager wifi_service = (WifiManager) ctx .getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifi_service.getDhcpInfo(); WifiInfo wifiinfo = wifi_service.getConnectionInfo(); return Formatter.formatIpAddress(dhcpInfo.gateway); }
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; }
private InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) this.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); }
/** * Returns the IP broadcast address for the current wifi connection * @param mContext * @return * @throws IOException */ private static InetAddress getBroadcastAddress(final Context mContext) throws IOException { WifiManager wifi = (WifiManager) mContext.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) & 0xFF); } return InetAddress.getByAddress(quads); }
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; }
@Override public String getGatewayAddr(Context context) { WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); DhcpInfo d = wm.getDhcpInfo(); return __formatString(d.gateway); }
InetAddress getBroadcastAddress() throws IOException { Context mContext=getApplicationContext(); 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); }
private InetAddress getBroadcastAddress() throws IOException { DhcpInfo dhcp = wifiManager.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); }