private static byte [ ] chooseMACAddress ( ) { byte [ ] result = new byte [ 6 ] ; boolean bFound = false ; try { Enumeration < NetworkInterface > ifaces = NetworkInterface . getNetworkInterfaces ( ) ; while ( ! bFound && ifaces . hasMoreElements ( ) ) { // Look for a real NIC. NetworkInterface iface = ifaces . nextElement ( ) ; try { byte [ ] hwAddress = iface . getHardwareAddress ( ) ; if ( hwAddress != null ) { int copyBytes = Math . min ( result . length , hwAddress . length ) ; for ( int index = 0 ; index < copyBytes ; index ++ ) { result [ index ] = hwAddress [ index ] ; } bFound = true ; } } catch ( SocketException e ) { // Try next NIC } } } catch ( SocketException e ) { // Possibly no NICs on this system? } if ( ! bFound ) { ( new Random ( ) ) . nextBytes ( result ) ; } return result ; }
Choose a MAC address from one of this machine s NICs or a random value .