获取安卓设备MAC地址工具类

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
获取安卓设备的MAC地址需要使用WifiManager,以下是一个获取MAC地址的工具类:
 
```
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
 
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
 
public class DeviceInfoUtils {
 
    /**
     * 获取设备 MAC 地址
     *
     * @param context 上下文
     * @return 设备 MAC 地址
     */
    @SuppressLint({"HardwareIds", "MissingPermission"})
    public static String getDeviceMacAddress(Context context) {
        String macAddress = "";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { // Android 6.0 以下
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo != null) {
                    macAddress = wifiInfo.getMacAddress();
                }
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { // Android 6.0 - Android 10.0
            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 macByte : macBytes) {
                        res1.append(String.format("%02X:", macByte));
                    }
 
                    if (res1.length() > 0) {
                        res1.deleteCharAt(res1.length() - 1);
                    }
 
                    macAddress = res1.toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Android 10.0 以上
            try {
                String iface = "wlan0";
                NetworkInterface networkInterface = NetworkInterface.getByName(iface);
                if (networkInterface == null) {
                    return "";
                }
                byte[] hardwareAddress = networkInterface.getHardwareAddress();
                if (hardwareAddress == null || hardwareAddress.length == 0) {
                    return "";
                }
                StringBuilder builder = new StringBuilder();
                for (byte b : hardwareAddress) {
                    builder.append(String.format("%02X:", b));
                }
                if (builder.length() > 0) {
                    builder.deleteCharAt(builder.length() - 1);
                }
                macAddress = builder.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return macAddress;
    }
}
```
调用该工具类获取设备的MAC地址:
 
```
String macAddress = DeviceInfoUtils.getDeviceMacAddress(context);
```
需要注意的是,Android 6.0 以上获取 MAC 地址需要申请 ACCESS_WIFI_STATE 权限。另外,从 Android Q 开始,不能直接获取设备的 MAC 地址,需要使用其他方式来获取设备的唯一标识符。

  

posted @   今年我十八岁  阅读(432)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示