获取Android mac地址,4.0一直到6.0,7.0系统都可以获取得到Mac地址
在AndroidManifest.xml中加入以下权限:
<uses-permission android:name="android.permission.INTERNET" />
-
- public static String getMacAddr() {
- try {
- List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
- for (NetworkInterface nif : all) {
- if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
-
- byte[] macBytes = nif.getHardwareAddress();
- if (macBytes == null) {
- return "";
- }
-
- StringBuilder res1 = new StringBuilder();
- for (byte b : macBytes) {
- res1.append(String.format("%02X:",b));
- }
-
- if (res1.length() > 0) {
- res1.deleteCharAt(res1.length() - 1);
- }
- return res1.toString();
- }
- } catch (Exception ex) {
- }
- return "02:00:00:00:00:00";
- }
用于自己记录防止忘记,觉得有用的也可以拿走。