Java代码
- public static List<String> getAllMacAddresses()
- {
- List<String> addresses = new ArrayList<String>();
- StringBuffer sb = new StringBuffer();
- try
- {
- Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
- while(networkInterfaces.hasMoreElements())
- {
- NetworkInterface netInterface = networkInterfaces.nextElement();
- byte[] mac = netInterface.getHardwareAddress();
- if(mac != null)
- sb.delete(0, sb.length());
- for(byte b : mac)
- {
- String hexString = Integer.toHexString(b & 0xFF);
- sb.append((hexString.length() == 1) ? "0" + hexString : hexString);
- }
- addresses.add(sb.toString());
- }
- }
- catch(SocketException e)
- {
- e.printStackTrace();
- }
- return addresses;
- }