获取有线 WLAN 蓝牙MAC地址

这段代码展示了如何在Android设备上获取有线网络(eth0)、蓝牙和WiFi(wlan0)的MAC地址。当WiFi关闭时,提供了避免获取默认空MAC地址的处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取有线 WLAN 蓝牙MAC地址

	 /**
     * 获取有线MAC地址
     */
    public static String getEthernetMac() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface netWork = interfaces.nextElement();
                if (!netWork.getName().equals("eth0")) {
                    continue;
                }
                byte[] macBytes = netWork.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }
                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }
                if (!TextUtils.isEmpty(res1)) {
                    res1.deleteCharAt(res1.length() - 1);
                }

                return res1.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 获取蓝牙 MAC 地址
     *
     * @return
     */
    public static String getBtMac() {
        return BluetoothAdapter.getDefaultAdapter().getAddress();
    }

    /**
     * 获取 WiFi MAC地址 避免wifi关闭时获取到”02:00:00:00:00:00“
     */
    public static String getWirelessMacAddress(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String wifiMacAddress = "NA";
        if (wifiInfo != null) {
            if ("02:00:00:00:00:00".equals(wifiInfo.getMacAddress())) {
                wifiMacAddress = getMacAddress();
            } else {
                wifiMacAddress = wifiInfo.getMacAddress();
            }
        }
        return wifiMacAddress == null ? "NA" : wifiMacAddress.toUpperCase();
    }

    private static String getMacAddress() {
        String address = null;
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface netWork = interfaces.nextElement();
                byte[] by = netWork.getHardwareAddress();
                if (by == null || by.length == 0) {
                    continue;
                }
                StringBuilder builder = new StringBuilder();
                for (byte b : by) {
                    builder.append(String.format("%02X:", b));
                }
                if (builder.length() > 0) {
                    builder.deleteCharAt(builder.length() - 1);
                }
                String mac = builder.toString();
                if (netWork.getName().equals("wlan0")) {
                    address = mac;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return address;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值