Back to project page CopresenceDataCollector.
The source code is released under:
Copyright (c) 2014, Xiang Gao All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...
If you think the Android project CopresenceDataCollector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.sesy.coco.datacollector.net; /*from w w w . j a v a2 s. c o m*/ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.net.InetAddress; import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.http.conn.util.InetAddressUtils; public class NetInfo { // 0x1 is HW Type: Ethernet (10Mb) [JBP] // 0x2 is ARP Flag: completed entry (ha valid) private final static String MAC_RE = "^%s\\s+0x1\\s+0x2\\s+([:0-9a-fA-F]+)\\s+\\*\\s+\\w+$"; public final static int BUF = 8 * 1024; public final static String NOMAC = "00:00:00:00:00:00"; public static String getHardwareAddress(String ip) { String hw = NOMAC; try { if (ip != null) { String ptrn = String.format(MAC_RE, ip.replace(".", "\\.")); Pattern pattern = Pattern.compile(ptrn); BufferedReader bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"), BUF); String line; Matcher matcher; while ((line = bufferedReader.readLine()) != null) { matcher = pattern.matcher(line); if (matcher.matches()) { hw = matcher.group(1); break; } } bufferedReader.close(); } } catch (IOException e) { return hw; } return hw; } 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; }*/ } public static String getIPAddress() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { //Log.i("net",addr.getHostAddress()); if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) return sAddr; } } } } catch (Exception ex) { } // for now eat exceptions return ""; } public static String getBroadcastAddress() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InterfaceAddress> iaddrs = intf.getInterfaceAddresses(); for (InterfaceAddress iaddr : iaddrs) { InetAddress addr = iaddr.getBroadcast(); if(addr != null) { String sAddr = addr.getHostAddress().toUpperCase(); //Log.i("net",sAddr); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (isIPv4) return sAddr; } } } } catch (Exception ex) { } // for now eat exceptions return ""; } public static short getCidr() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InterfaceAddress> iaddrs = intf.getInterfaceAddresses(); for (InterfaceAddress iaddr : iaddrs) { InetAddress addr = iaddr.getBroadcast(); if(addr != null) { short cidr = iaddr.getNetworkPrefixLength(); return cidr; } } } } catch (Exception ex) { } // for now eat exceptions return 24; } public static long getUnsignedLongFromIp(String ip_addr) { String[] a = ip_addr.split("\\."); return (Integer.parseInt(a[0]) * 16777216 + Integer.parseInt(a[1]) * 65536 + Integer.parseInt(a[2]) * 256 + Integer.parseInt(a[3])); } public static String getIpFromLongUnsigned(long ip_long) { String ip = ""; for (int k = 3; k > -1; k--) { ip = ip + ((ip_long >> k * 8) & 0xFF) + "."; } return ip.substring(0, ip.length() - 1); } }