Java InetAddress Create getMacAddress(InetAddress addr)

Here you can find the source of getMacAddress(InetAddress addr)

Description

Returns the mac address in format like 'FF-FF-...'.

License

Open Source License

Parameter

Parameter Description
addr the addr at the target network interface.

Exception

Parameter Description
SocketException an exception

Declaration

public static String getMacAddress(InetAddress addr) throws SocketException 

Method Source Code


//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();
    }
}

Related

  1. getLocalInetAddress(String host)
  2. getLocalInetAddress(String host)
  3. getLocalInetAddresses()
  4. getLocalInetAddresses()
  5. getLocalInetAddresses(boolean includeLoopback)
  6. getMacAddress(InetAddress address)
  7. getMieleUUIDLowerPart(int deviceType50523, InetAddress address)
  8. getNatType(final InetAddress localAdr, final InetAddress publicAdr)
  9. getNetwork(InetAddress start, InetAddress end)