jdk 1.6 부터 java에서 MacAddress를 가져올 수 있다.
private ArrayList<String> getMacAddress(){
ArrayList<String> macAddr = new ArrayList<String>();
try {
Enumeration<NetworkInterface> nif = NetworkInterface.getNetworkInterfaces();
while(nif.hasMoreElements()){
NetworkInterface nextElement = nif.nextElement();
byte[] mac = nextElement.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
macAddr.add(sb.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return macAddr;
}
반응형
'Language > Java' 카테고리의 다른 글
InputStream을 byte[] 하는 방법 (0) | 2019.08.20 |
---|---|
코딩 표기법 정리 (0) | 2019.08.17 |
StringBuffer와 StringBuilder의 차이점 (0) | 2019.08.17 |