Ciao a tutti,
Devo ottenere il MAC Address, ma ho un problemuccio.
Ho già letto la doc in merito a NetworkAddress, ed in particolare
ma non riesco a venirne a capo...Returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.
Returns:
a byte array containing the address or null if the address doesn't exist or is not accessible.
A me il programma ritorna sempre null insomma.
Ecco il codice:
Grazie a tutti! ^^codice:import java.util.*; import java.net.*; import java.io.*; class NetworkUtility { private NetworkUtility() {} public static String getMacAddress() { try { StringBuffer sb = new StringBuffer(); Enumeration<NetworkInterface> enumerazione = NetworkInterface.getNetworkInterfaces(); while(enumerazione.hasMoreElements()) { NetworkInterface el = enumerazione.nextElement(); System.out.println ("Interface: " + el.getDisplayName().trim()); byte[] addr = el.getHardwareAddress(); System.out.println("EL "+el); if(addr == null) return null; for(int i=0; i<addr.length; i++) { sb.append(String.format(i == 0 ? "%02X" : "-%02X", addr[i])); } } return sb.toString(); } catch(Exception e) { return e.toString(); } } } class TestMac { public static void main(String[] args) { System.out.println(NetworkUtility.getMacAddress()); } }