Android获取设备的 mac 地址

Android获取设备的 mac 地址

直接上代码,方法很简单,一般默认是优先获取有线网络eth0,如果没有有线网络的mac地址再获取无线网络的;实际开发中,设备的mac地址的可以用于设备授权的认证,数据的推送识别等等
  /**
     * eth0 MAC地址获取,适用api9 - api24
     */
    public static String getEth0Mac() {

        String Mac = "";
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("eth0")) continue;

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "Didn\'t get eth0 MAC address";
                }
                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }
                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                    Mac = res1.toString();
                }
                return Mac;
            }
        } catch (Exception ex) {
        }
        return "Didn\'t get eth0 MAC address";
    }

    /**
     * wlan0 MAC地址获取,适用api9 - api24
     */
    public static String getWlan0Mac() {

        String Mac = "";
        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 "Didn\'t get Wlan0 MAC address";
                }
                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:", b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                    Mac = res1.toString();
                }
                return Mac;
            }
        } catch (Exception ex) {
        }
        return "Didn\'t get Wlan0 address";
    }

    /**
     * 取得 Mac-Address
     * 1. 先取得 eth0 Mac-Address
     * 2. 再取得 wlan0 Mac-Address
     *
     * @return
     */
    public static String getDeviceMacAddress() {
        String mac = !getEth0Mac().equals("Didn\'t get eth0 MAC address") ? getEth0Mac() : getWlan0Mac();
        return mac;
    }

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在Android系统中,获取蓝牙MAC地址有多种方法,其中最常见的方法是使用BluetoothAdapter类中的getAddress()方法。该方法返回一个String类型的字符串,表示设备的蓝牙MAC地址。 使用该方法需要先获取BluetoothAdapter实例,可以通过调用getDefaultAdapter()静态方法来获得,如下所示: BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 接着,使用getAddress()方法获取设备的蓝牙MAC地址: String bluetoothMacAddress = bluetoothAdapter.getAddress(); 获得蓝牙MAC地址后,可以根据需要进行后续操作,例如在蓝牙设备发现和连接时使用。 需要注意的是,在一些设备上,蓝牙MAC地址可能无法获取或者该方法返回一个固定的值,这是由于硬件或者系统限制所导致的。因此,在应用程序中获取蓝牙MAC地址时需要谨慎,特别是对于涉及到隐私的应用场景。 ### 回答2: Android获取蓝牙MAC地址的方案: 1.使用BluetoothAdapter类获取MAC地址 首先,使用getSystemService方法获取BluetoothAdapter对象: BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 然后,使用getAddress方法得到MAC地址: String macAddress = bluetoothAdapter.getAddress(); 注意:这种方法只能在蓝牙处于开启状态时才能获取MAC地址。 2.使用BluetoothDevice类获取MAC地址 如果想获取已经配对的蓝牙设备MAC地址,可以使用BluetoothDevice类。 首先,使用getBondedDevices方法获取已配对的蓝牙设备集合: Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices(); 然后,遍历集合,使用getAddress方法获取MAC地址: for (BluetoothDevice device : bondedDevices) { String macAddress = device.getAddress(); } 注意:这种方法只能获取到已经配对的蓝牙设备MAC地址。 3.使用反射获取MAC地址 如果以上两种方式都无法获取MAC地址,可以尝试使用反射的方式获取。 首先,获取BluetoothAdapter的Class对象: Class<?> bluetoothAdapterClass = BluetoothAdapter.class; 然后,使用getMethod方法获取getAddress方法: Method getAddressMethod = bluetoothAdapterClass.getMethod("getAddress"); 接下来,使用invoke方法执行方法: String macAddress = (String) getAddressMethod.invoke(bluetoothAdapter); 注意:使用反射可能存在风险,因为某些手机厂商可能会修改了BluetoothAdapter类的实现。 总之,以上三种方法都可以用于获取蓝牙MAC地址,具体应该根据实际情况选择使用哪种方式。 ### 回答3: Android获取蓝牙MAC地址,可以通过BluetoothAdapter的方法来进行操作。步骤如下: 1. 获取BluetoothAdapter对象 首先需要获取BluetoothAdapter对象,可以使用以下代码: ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ``` 2. 获取设备的蓝牙MAC地址 获取到BluetoothAdapter对象后,可以使用getAddress()方法获取设备的蓝牙MAC地址: ``` String bluetoothAddress = bluetoothAdapter.getAddress(); ``` 值得注意的是,getAddress()方法获取到的是设备的本地蓝牙MAC地址,而不是连接到该设备的蓝牙设备MAC地址。 3. 权限声明 在进行蓝牙操作时,需要在AndroidManifest.xml中声明相应的权限,否则无法进行蓝牙相关操作。以下是需要声明的权限: ``` <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 以上是获取蓝牙MAC地址的基本步骤,需要注意:在Android 6.0系统及以上版本中,Android强制要求应用程序在运行时动态申请权限。因此,需要在代码中加入动态权限申请的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心影流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值