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

获取安卓设备的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 @ 2023-04-22 21:15  今年我十八岁  阅读(242)  评论(0编辑  收藏  举报