Advertisement
retnet

get mac address java android

Jan 2nd, 2023
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public static String getMacAddr() {
  2.     try {
  3.         List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  4.         for (NetworkInterface nif : all) {
  5.             if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
  6.  
  7.             byte[] macBytes = nif.getHardwareAddress();
  8.             if (macBytes == null) {
  9.                 return "";
  10.             }
  11.  
  12.             StringBuilder res1 = new StringBuilder();
  13.             for (byte b : macBytes) {
  14.                 String hex = Integer.toHexString(b & 0xFF);
  15.                 if (hex.length() == 1)
  16.                     hex = "0".concat(hex);
  17.                 res1.append(hex.concat(":"));
  18.             }
  19.  
  20.             if (res1.length() > 0) {
  21.                 res1.deleteCharAt(res1.length() - 1);
  22.             }
  23.             return res1.toString();
  24.         }
  25.     } catch (Exception ex) {
  26.     }
  27.     return "";
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement