Ciao a tutti,
Devo ottenere il MAC Address, ma ho un problemuccio.
Ho già letto la doc in merito a NetworkAddress, ed in particolare
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.
ma non riesco a venirne a capo...
A me il programma ritorna sempre null insomma.
Ecco il codice:
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());
}
}
Grazie a tutti! ^^