有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取扩展底座的MAC地址忽略MAC地址传递

我的目标是通过办公应用程序的MAC地址来识别扩展底座,以自动确定哪些桌子被占用。使用不同的扩展底座,它可以正常工作。但是,当Dell笔记本电脑连接到Dell扩展底座时,我无法实现这一点,因为它们使用MAC地址传递。因此,他们使用笔记本电脑的MAC地址,我无法请求扩展底座的MAC地址

有人知道我如何用Java获得这个MAC地址吗?或者我可以用哪个命令来实现?我没有找到任何东西,因为所有的方法都只给我笔记本电脑的MAC地址。解决方案不必是独立于平台的

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MacAddressReader {
    public static String getMacAddressOfDockingStation(String interfaceName) {
        String macAddress = getAllInterfacesNamesAndMacs().get(interfaceName);
        if (macAddress != null && !macAddress.isEmpty())
            return macAddress;

        return "";
    }

    private static Map<String, String> getAllInterfacesNamesAndMacs() {
        Map<String, String> addresses = new HashMap<>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                addresses.put(
                        networkInterface.getDisplayName(),
                        macAddressAsString(networkInterface.getHardwareAddress())
                );
            }
            return addresses;
         } catch (SocketException e) {
            return addresses;
         }
    }

    private static String macAddressAsString(byte[] mac) {
        if (mac == null)
            return "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return sb.toString();
    }

}

共 (1) 个答案

  1. # 1 楼答案

    我无法使用MAC地址解决问题。如果有人有同样的问题,你可以使用连接的监视器来唯一地识别工作站。使用一个简单的powershell SKcript,您可以为监视器获取唯一的序列号,以识别工作站。这是我的代码,也许这能帮到别人

    Powershell Skcript:

    $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi
    
    function Decode {
        If ($args[0] -is [System.Array]) {
            [System.Text.Encoding]::ASCII.GetString($args[0])
        }
        Else {
            "Not Found"
        }
    }
    
    ForEach ($Monitor in $Monitors) {
        $Manufacturer = Decode $Monitor.ManufacturerName -notmatch 0
        $Product = Decode $Monitor.ProductCodeID -notmatch 0
        $Name = Decode $Monitor.UserFriendlyName -notmatch 0
        $Serial = Decode $Monitor.SerialNumberID -notmatch 0
    
        echo "$Manufacturer;$Product;$Name;$Serial"
    }
    

    使用jPowerShell的Java代码:

    try (PowerShell powerShell = PowerShell.openSession()){    
         String script = "../MyMonitorScript.txt";
         String result = powerShell.executeScript(script).getCommandOutput();
         ...
    

    字符串结果包含有关已连接监视器的信息,包括唯一序列号