List of usage examples for java.net NetworkInterface getName
public String getName()
From source file:com.common.utils.NetworkUtils.java
public static String getLocalHostIp() { String ips = "";//getString(R.string.ipaddr); try {/*from www .ja v a 2s. c o m*/ Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip.getHostAddress())) { ips = ips + nif.getName() + ":" + ip.getHostAddress() + " "; } } } } catch (SocketException e) { e.printStackTrace(); } return ips; }
From source file:license.ExtraTestWakeLicense.java
/** * ?mac?/*from www.ja v a 2 s . com*/ * @param sb * @throws Exception */ private static void mac(StringBuilder sb) throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); int i = 12; Base64 base64 = new Base64(); for (NetworkInterface ni : Collections.list(interfaces)) { if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) { sb.append(String.valueOf(i)).append('=').append(ni.getName()).append('\n'); i++; byte[] mac = ni.getHardwareAddress(); sb.append(String.valueOf(i)).append('=').append(base64.encodeAsString(mac)).append('\n'); i++; } } }
From source file:org.basdroid.common.NetworkUtils.java
/** * Get IP address from first non-localhost interface * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string/*from ww w . j av a 2s . co m*/ */ public static String getIPAddress(String interfaceName, boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } List<InetAddress> inetAddresses = Collections.list(intf.getInetAddresses()); for (InetAddress inetAddr : inetAddresses) { if (!inetAddr.isLoopbackAddress()) { String address = inetAddr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(address); if (useIPv4) { if (isIPv4) { return address; } } else { if (!isIPv4) { int delim = address.indexOf('%'); // drop ip6 port suffix return delim < 0 ? address : address.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:Main.java
public static NetworkInterface getActiveNetworkInterface() { Enumeration<NetworkInterface> interfaces = null; try {//w w w . java 2 s .c o m interfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); /* Check if we have a non-local address. If so, this is the active * interface. * * This isn't a perfect heuristic: I have devices which this will * still detect the wrong interface on, but it will handle the * common cases of wifi-only and Ethernet-only. */ if (iface.getName().startsWith("w")) { //this is a perfect hack for getting wifi alone while (inetAddresses.hasMoreElements()) { InetAddress addr = inetAddresses.nextElement(); if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) { Log.d("LSSDP", "DisplayName" + iface.getDisplayName() + " Name " + iface.getName()); return iface; } } } } return null; }
From source file:org.basdroid.common.NetworkUtils.java
/** * Returns MAC address of the given interface name. * @param interfaceName eth0, wlan0 or NULL=use first interface * @return mac address or empty string/*from w w w . j av a 2 s. c om*/ */ public static String getMACAddress(String interfaceName) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { Log.d(TAG, "intf.getName() = " + intf.getName()); 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 ""; }
From source file:org.basdroid.common.NetworkUtils.java
public static String getMacAddressFromNetworkInterface(final Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); // Convert little-endian to big-endianif needed if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { ipAddress = Integer.reverseBytes(ipAddress); }/*from ww w. j a va 2 s . c om*/ byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray(); String result; try { InetAddress addr = InetAddress.getByAddress(bytes); NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr); Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName()); byte[] mac = netInterface.getHardwareAddress(); if (mac == null || mac.length == 0) 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 (UnknownHostException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex); result = null; } catch (SocketException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex); result = null; } catch (Exception ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex); result = null; } return result; }
From source file:com.landenlabs.all_devtool.NetFragment.java
public static String getMacAddr() { try {/*from w w w .j a va2 s . c om*/ List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(Integer.toHexString(b & 0xFF)).append(":"); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; }
From source file:net.ftb.util.OSUtils.java
/** * Grabs the mac address of computer and makes it 10 times longer * @return a byte array containing mac address *///from w ww .j av a 2 s .c o m public static byte[] getMacAddress() { if (cachedMacAddress != null && cachedMacAddress.length >= 10) { return cachedMacAddress; } try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface network = networkInterfaces.nextElement(); byte[] mac = network.getHardwareAddress(); if (mac != null && mac.length > 0 && !network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getName().substring(0, 3) != "ham") { Logger.logDebug("Interface: " + network.getDisplayName() + " : " + network.getName()); cachedMacAddress = new byte[mac.length * 10]; for (int i = 0; i < cachedMacAddress.length; i++) { cachedMacAddress[i] = mac[i - (Math.round(i / mac.length) * mac.length)]; } return cachedMacAddress; } } } catch (SocketException e) { Logger.logWarn("Exception getting MAC address", e); } Logger.logWarn("Failed to get MAC address, using default logindata key"); return new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; }
From source file:Main.java
public static String getLocalIpAddress() { String result = ""; boolean exit = false; try {/* ww w . ja v a2 s . co 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; }
From source file:license.mac.MacTest.java
/** * mac?//from w ww. java2s. c o m * @return * @throws SocketException */ public boolean checkMac() throws SocketException { StringBuffer sb = new StringBuffer(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); int i = 12; Base64 base64 = new Base64(); for (NetworkInterface ni : Collections.list(interfaces)) { if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) { sb.append(String.valueOf(i)).append('=').append(ni.getName()).append('\n'); i++; byte[] mac = ni.getHardwareAddress(); sb.append(String.valueOf(i)).append('=').append(base64.encodeAsString(mac)).append('\n'); i++; } } System.out.println(sb.toString()); return true; }