I have the goal to identify docking stations by their MAC address for an office application to automate which desks are occupied. With different docking stations it works fine. However, I cannot achieve this when a Dell Laptop is connected to a Dell docking station because they use MAC address pass through. Thus, they use a MAC address of the laptop, and I cannot request the MAC address of the docking station.
Has anyone an idea how I can get this MAC address with Java or maybe with which command I can achieve this? I have not found anything because all approaches just give me the MAC address of the laptop. The solution does not have to be platform independent.
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();
}
}