Java Network Interface Get getFirstMACAdress()

Here you can find the source of getFirstMACAdress()

Description

Returns the first MAC adress that is found in the system.

License

Open Source License

Exception

Parameter Description
SocketException if an I/O error occurs.

Return

MAC adress

Declaration

private static byte[] getFirstMACAdress() throws SocketException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2015 Connor Lanigan (email: dev@connorlanigan.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**/*from   w  w  w .j  av a 2  s. c  o m*/
     * Returns the first MAC adress that is found in the system.
     *
     * @return MAC adress
     * @throws SocketException
     *             if an I/O error occurs.
     */
    private static byte[] getFirstMACAdress() throws SocketException {
        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                .getNetworkInterfaces();

        while (networkInterfaces.hasMoreElements()) {
            final NetworkInterface network = networkInterfaces
                    .nextElement();

            final byte[] bmac = network.getHardwareAddress();
            if (bmac != null) {
                return bmac;
            }
        }
        throw new SocketException(
                "No network interfaces with a MAC adress found.");
    }
}

Related

  1. getAvailableNetworkInterface()
  2. getBroadcast()
  3. getCustomMACFormat(NetworkInterface inte)
  4. getDefaultInterface()
  5. getDefaultNetworkInterface()
  6. getFreeTunnelInterface()
  7. getGlobalI()
  8. getGlobalInterfaces()
  9. getGoodNetworkInterfaces()