android4.0-7.0获取mac地址,方法是google提供。

来源:互联网 发布:官方刷机软件 编辑:程序博客网 时间:2024/05/18 22:16

获取Android mac地址,4.0一直到6.0,7.0系统都可以获取得到Mac地址

在AndroidManifest.xml中加入以下权限:

<uses-permission android:name="android.permission.INTERNET" />

[html] view plain copy

  1.    
  2.     public static String getMacAddr() {  
  3.         try {  
  4.             List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());  
  5.             for (NetworkInterface nif : all) {  
  6.                 if (!nif.getName().equalsIgnoreCase("wlan0")) continue;  
  7.   
  8.                 byte[] macBytes = nif.getHardwareAddress();  
  9.                 if (macBytes == null) {  
  10.                     return "";  
  11.                 }  
  12.   
  13.                 StringBuilder res1 = new StringBuilder();  
  14.                 for (byte b : macBytes) {  
  15.                     res1.append(String.format("%02X:",b));  
  16.                 }  
  17.   
  18.                 if (res1.length() > 0) {  
  19.                     res1.deleteCharAt(res1.length() - 1);  
  20.                 }  
  21.                 return res1.toString();  
  22.             }  
  23.         } catch (Exception ex) {  
  24.         }  
  25.         return "02:00:00:00:00:00";  
  26.     }   

用于自己记录防止忘记,觉得有用的也可以拿走。

原创粉丝点击