Java代码

  1. public static List<String> getAllMacAddresses()
  2. {
  3. List<String> addresses = new ArrayList<String>();
  4.  
  5. StringBuffer sb = new StringBuffer();
  6. try
  7. {
  8. Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
  9. while(networkInterfaces.hasMoreElements())
  10. {
  11. NetworkInterface netInterface = networkInterfaces.nextElement();
  12. byte[] mac = netInterface.getHardwareAddress();
  13. if(mac != null)
  14. sb.delete(0, sb.length());
  15. for(byte b : mac)
  16. {
  17. String hexString = Integer.toHexString(b & 0xFF);
  18. sb.append((hexString.length() == 1) ? "0" + hexString : hexString);
  19. }
  20. addresses.add(sb.toString());
  21. }
  22. }
  23. catch(SocketException e)
  24. {
  25. e.printStackTrace();
  26. }
  27.  
  28. return addresses;
  29. }