1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public String getAdresseMac(){
ArrayList<String> list = new ArrayList<String>();
String adresseMac = "";
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
if (mac!=null) {
// Et si elle existe on la formate afin de la rendre plus lisible :
StringBuilder sb = new StringBuilder();
for (byte b : mac) {
sb.append(String.format("%02X", b));
}
// Avant de la rajouter dans la liste :
list.add(sb.toString());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
String test = list.get(0);
return test;
} |
Partager