Java Mac Address Get getMacAddresses()

Here you can find the source of getMacAddresses()

Description

get Mac Addresses

License

Apache License

Exception

Parameter Description
SocketException an exception

Return

a list of all network interfaces mac addresses. e.g. aa:bb:cc:dd:ee:00

Declaration

public static String[] getMacAddresses() throws SocketException 

Method Source Code

//package com.java2s;
/*//from  w w w. j  av  a  2 s  .c om
 * Copyright 2016 tamas.csaba@gmail.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.NetworkInterface;

import java.net.SocketException;
import java.util.ArrayList;

import java.util.Enumeration;
import java.util.List;

public class Main {
    /**
     *
     * @return a list of all network interfaces mac addresses. e.g.
     * aa:bb:cc:dd:ee:00
     * @throws SocketException
     * @see NetworkInterface.getNetworkInterfaces()
     * @see NetworkInterface.getHardwareAddress()
     */
    public static String[] getMacAddresses() throws SocketException {
        List<String> result = new ArrayList<>();
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        if (nis != null) {
            while (nis.hasMoreElements()) {
                byte[] mac = nis.nextElement().getHardwareAddress();
                if (mac != null && mac.length == 6) {
                    result.add(String.format("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3],
                            mac[4], mac[5]));
                }
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

Related

  1. getMACAddress()
  2. getMacAddress(String host)
  3. getMACAddress(String interfaceName)
  4. getMacAddress(String separator)
  5. getMacAddressBytes()
  6. getMacAddressString()
  7. getMacAddrs()
  8. GetMachineAddress()