NetworkInterface不返回所有mac地址

时间:2013-08-26 18:20:50

标签: java network-interface

在我们的部分代码中,我试图将mac地址与我们的许可证文件绑定,并且我面临一个问题,即我的代码用于计算机器的所有mac地址并未返回所有物理mac地址。这是获取所有mac地址的代码

public static Collection<String> getAllLocalMacAddresses() throws IOException {
    final Enumeration<NetworkInterface> inetAddresses = NetworkInterface.getNetworkInterfaces();
    final Collection<String> addresses = new LinkedList<String>();

    while(inetAddresses.hasMoreElements()){
        final byte[] macBytes = inetAddresses.nextElement().getHardwareAddress();

        if(macBytes == null)
            continue;

        addresses.add(getMacAddress(macBytes));
    }

    return addresses;
}

static String getMacAddress(byte[] macBytes){
    final StringBuilder strBuilder = new StringBuilder();

    for (int i = 0; i < macBytes.length; i++){
        strBuilder.append(String.format("%02X%s", macBytes[i],
                (i < macBytes.length - 1) ? ":" : ""));
    }

    return strBuilder.toString().toUpperCase();
}

这是我从这个方法得到的结果

[, 00:00:00:00:00:00:00:E0, 00:26:B9:30:6D:CB, 00:26:B9:30:6D:CB, 00:00:00:00:00:00:00:E0, 00:26:B9:30:6D:CB, 02:26:B9:30:6D:C1, 00:26:B9:30:6D:CB, 00:00:00:00:00:00:00:E0]

“ipconfig / all”的输出显示了这些物理地址(如果需要,我可以粘贴此命令的输出)

00-26-B9-30-6D-C3
02-26-B9-30-6D-C1
00-26-B9-30-6D-C5
00-26-B9-30-6D-C9
00-26-B9-30-6D-CB
00-00-00-00-00-00-00-E0

现在如果你看到java代码没有返回地址“00-26-B9-30-6D-C3”并且还打印了地址“00:26:B9:30:6D:CB”三次。知道为什么会发生这种情况,我该如何解决它。

任何帮助都将深表感谢:)

1 个答案:

答案 0 :(得分:0)