由于在网上没有找到正确获取到Mac地址的方式,我原来的代码在Windows上可以获取到,但是苹果电脑上却不行,所以找了一下原因,发现是需要获取本机所有的ip然后找到一个有mac地址信息的数据,代码不复杂,但用得少,记录一下以便日后使用。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | 
 
 
 public static String getLocalMac(){
 
 String mac = "";
 try {
 
 String hostName = InetAddress.getLocalHost().getHostName();
 
 InetAddress[] inetAddresses = InetAddress.getAllByName(hostName);
 
 for (InetAddress inetAddress : inetAddresses) {
 NetworkInterface net = NetworkInterface.getByInetAddress(inetAddress);
 byte[] macBytes = net.getHardwareAddress();
 if (macBytes != null){
 return transBytesToStr(macBytes);
 }
 }
 } catch (UnknownHostException | SocketException e) {
 e.printStackTrace();
 }
 return mac;
 }
 
 |