Here you can find the source of getMacAddress(InetAddress addr)
Parameter | Description |
---|---|
addr | the addr at the target network interface. |
Parameter | Description |
---|---|
SocketException | an exception |
public static String getMacAddress(InetAddress addr) throws SocketException
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; public class Main { /**/*from w w w . j a va 2 s . co m*/ * Returns the mac address in format like 'FF-FF-...'. * @param addr the addr at the target network interface. * @return * @throws SocketException */ public static String getMacAddress(InetAddress addr) throws SocketException { NetworkInterface ni = NetworkInterface.getByInetAddress(addr); if (ni == null) { return null; } StringBuilder sb = new StringBuilder(); byte[] mac = ni.getHardwareAddress(); for (int i = 0; mac != null && i < mac.length; i++) { if (i > 0) { sb.append("-"); } sb.append(String.format("%02X", mac[i])); } return sb.toString(); } }