List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:com.ihelpoo.app.common.DeviceUtil.java
/** * Returns MAC address of the given interface name. * * @param interfaceName eth0, wlan0 or NULL=use first interface * @return mac address or empty string// w ww . ja v a2 s .com */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static String getMACAddress(String interfaceName) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } byte[] mac = intf.getHardwareAddress(); if (mac == null) return ""; StringBuilder buf = new StringBuilder(); for (int idx = 0; idx < mac.length; idx++) buf.append(String.format("%02X:", mac[idx])); if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); return buf.toString(); } } catch (Exception ex) { } // for now eat exceptions return ""; /*try { // this is so Linux hack return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim(); } catch (IOException ex) { return null; }*/ }
From source file:de.uni_bonn.bit.IPAddressHelper.java
/** * Returns a list of IP addresses that can be used to communicate with a peer on a * different machine. The IP addresses are in CIDR notation (e.g. 192.168.2.1/24). * @return/*from w w w.j a v a 2 s .com*/ */ public static List<String> getAllUsableIPAddresses() { List<String> output = new ArrayList<String>(); try { for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InterfaceAddress address : ni.getInterfaceAddresses()) { if (!address.getAddress().isMulticastAddress() && !address.getAddress().isLinkLocalAddress() && !address.getAddress().isLoopbackAddress()) { output.add(address.getAddress().getHostAddress() + "/" + address.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return output; }
From source file:Main.java
public static String getGPRSIP() { String ip = null;/*from w w w . ja v a 2 s.c o m*/ try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { for (Enumeration<InetAddress> enumIpAddr = en.nextElement().getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { ip = inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); ip = null; } return ip; }
From source file:com.seadee.library.receiver.NetworkStateReceiver.java
public static String getLocalIpAddress(Context context) { final String IPTAG = "getLocalIpAddress"; String nullAddress = "0.0.0.0"; try {/*w w w . j a va 2 s . c o m*/ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String ip_address = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address)) return ip_address; } } } catch (SocketException ex) { Log.e(IPTAG, ex.toString()); } return nullAddress; }
From source file:org.openhab.binding.network.service.NetworkUtils.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet/*w w w.j av a 2s.c om*/ * * @return The collected IPv4 Addresses */ public static TreeSet<String> getInterfaceIPs() { TreeSet<String> interfaceIPs = new TreeSet<String>(); try { // For each interface ... for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); if (!networkInterface.isLoopback()) { // .. and for each address ... for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { // ... get IP and Subnet InterfaceAddress interfaceAddress = it.next(); interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return interfaceIPs; }
From source file:Main.java
public static void getLocalIP() { String ipaddress = ""; try {//w w w .j a va 2s. com 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()) { ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { } }
From source file:Main.java
public static String getIpWithoutWifi() { try {//from w ww . jav a2s. c o m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumeration = intf.getInetAddresses(); en.hasMoreElements();) { InetAddress inetAddress = enumeration.nextElement(); if (inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { Log.w(TAG, "getIpWithoutWifi error:" + e.getMessage()); } return null; }
From source file:Main.java
public static String getIfIpAddress() { String result = ""; try {// w w w .j a va 2s. c o m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); // Log.v("","ip="+inetAddress.getHostAddress()); if (!inetAddress.isLoopbackAddress() && (inetAddress.getHostAddress().startsWith("0") || inetAddress.getHostAddress().startsWith("1") || inetAddress.getHostAddress().startsWith("2"))) { result = inetAddress.getHostAddress(); break; } } } } catch (SocketException ex) { Log.e(DEBUG_TAG, ex.toString()); result = "192.168.0.1"; } // Log.v("","getIfIpAddress result="+result); if (result.equals("")) result = "192.168.0.1"; return result; }
From source file:com.anyi.gp.license.RegisterTools.java
public static Map getInetAddresses() { Map result = new HashMap(); Enumeration netInterfaces = null; try {/*from w ww.jav a 2 s . co m*/ netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); String displayName = ni.getDisplayName(); String ipString = ""; Enumeration ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ipString += ((InetAddress) ips.nextElement()).getHostAddress() + ","; } if (ipString != null && ipString.length() > 0) ipString = ipString.substring(0, ipString.length() - 1); result.put(displayName, ipString); } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String getLocalIpAddress() { String result = ""; boolean exit = false; try {/* w w w. java 2 s. c o m*/ 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.toString().indexOf(":")>=0)) { // return inetAddress.getHostAddress().toString(); // } // Log.v("","ip="+inetAddress.getHostAddress()+ // ", name="+intf.getName()); if (inetAddress.isSiteLocalAddress()) { result = inetAddress.getHostAddress(); // Log.v("","result="+result+", name="+intf.getName()+"-"); if (intf.getName().equals("wlan0")) { exit = true; break; } } } if (exit) break; } } catch (SocketException ex) { Log.e(DEBUG_TAG, ex.toString()); result = "192.168.0.1"; } // Log.v("","getLocalIpAddress result="+result); if (result.equals("")) result = "192.168.0.1"; return result; }