java 获取设备所有Mac
public static List<String> getMacList() throws SocketException {
java.util.Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
StringBuilder sb = new StringBuilder();
ArrayList<String> tmpMacList = new ArrayList<>();
while(en.hasMoreElements()){
NetworkInterface iface = en.nextElement();
List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
for (InterfaceAddress addr : addrs){
InetAddress ip = addr.getAddress();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network==null) {
continue;
}
byte[] mac = network.getHardwareAddress();
if (mac == null) {
continue;
}
sb.delete(0, sb.length());
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i<mac.length-1) ? "" : "" ));
}
tmpMacList.add(sb.toString());
}
}
for(int i=0; i<tmpMacList.size(); i++){
for (int j=0; j<tmpMacList.size(); j++){
if (i!=j && tmpMacList.get(i)==tmpMacList.get(j)){
tmpMacList.remove(tmpMacList.get(j));
}
}
}
return tmpMacList;
}