以编程方式获取 Android 设备的 MAC [英] Programmatically getting the MAC of an Android device

查看:30
本文介绍了以编程方式获取 Android 设备的 MAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Java 获取我的 android 设备的 MAC 地址.我在网上搜索过,但没有找到任何有用的东西.

I need to obtain the MAC address of my android device using Java. I've searched online, but I haven't found anything useful.

推荐答案

正如评论中已经指出的,MAC 地址可以通过 WifiManager.

As was already pointed out in the comment, the MAC address can be received via the WifiManager.

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();

另外不要忘记在你的AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

请参考 Android 6.0 变化.

为了向用户提供更好的数据保护,从本版本开始,Android 移除了使用 Wi-Fi 和蓝牙 API 的应用对设备本地硬件标识符的编程访问.WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法现在返回一个常量值 02:00:00:00:00:00.

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

要通过蓝牙和 Wi-Fi 扫描访问附近外部设备的硬件标识符,您的应用现在必须具有 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 权限.

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.

这篇关于以编程方式获取 Android 设备的 MAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆