如何在Android应用中获取并显示设备MAC地址的详细教程

2025-09-22 00:36:28

如何在Android应用中获取并显示设备MAC地址的详细教程

引言

在Android开发中,获取设备的MAC地址是一个常见的需求,尤其是在需要进行设备识别和网络调试时。然而,随着Android系统的不断更新,获取MAC地址的方法也在不断变化。本文将详细讲解如何在不同的Android系统版本中获取并显示设备的MAC地址。

权限声明

首先,我们需要在AndroidManifest.xml文件中添加必要的权限声明,以便应用能够访问WiFi状态信息。

获取MAC地址的基本方法

在Android 6.0(API级别23)及以下版本中,获取MAC地址相对简单,可以通过WifiManager类来实现。

步骤1:获取WifiManager实例

在Activity中,通过以下代码获取WifiManager实例:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

步骤2:获取WifiInfo对象

通过WifiManager实例获取WifiInfo对象:

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

步骤3:获取MAC地址

调用WifiInfo对象的getMacAddress方法获取MAC地址:

String macAddress = wifiInfo.getMacAddress();

处理Android 6.0到7.0之间的特殊情况

在Android 6.0到7.0之间的版本中,由于隐私保护的增强,使用传统方法获取的MAC地址可能始终是固定值02:00:00:00:00:00。

解决方案:使用命令行方式

可以通过执行命令行来获取真实的MAC地址。

String macAddress = getMacAddressFromCommand();

private String getMacAddressFromCommand() {

String result = "";

try {

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec("ifconfig wlan0");

InputStream inputStream = process.getInputStream();

InputStreamReader reader = new InputStreamReader(inputStream);

BufferedReader bufferedReader = new BufferedReader(reader);

String line;

while ((line = bufferedReader.readLine()) != null) {

if (line.contains("HWaddr")) {

result = line.substring(line.indexOf("HWaddr") + 7);

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

处理Android 7.0及以上版本

在Android 7.0及以上版本中,即使是命令行方式也可能无法获取到真实的MAC地址。此时,可以通过设备的本地IP获取绑定的MAC地址。

步骤1:获取本地IP地址

首先获取设备的本地IP地址:

String ipAddress = getLocalIpAddress();

private String getLocalIpAddress() {

try {

for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {

NetworkInterface intf = en.nextElement();

for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {

InetAddress inetAddress = enumIpAddr.nextElement();

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {

return inetAddress.getHostAddress();

}

}

}

} catch (SocketException ex) {

ex.printStackTrace();

}

return null;

}

步骤2:通过本地IP获取MAC地址

然后通过本地IP地址获取绑定的MAC地址:

String macAddress = getMacAddressByIp(ipAddress);

private String getMacAddressByIp(String ipAddress) {

String macAddress = null;

try {

byte[] bytes = NetworkInterface.getByInetAddress(InetAddress.getByName(ipAddress)).getHardwareAddress();

StringBuilder sb = new StringBuilder();

for (int i = 0; i < bytes.length; i++) {

sb.append(String.format("%02X%s", bytes[i], (i < bytes.length - 1) ? ":" : ""));

}

macAddress = sb.toString();

} catch (Exception e) {

e.printStackTrace();

}

return macAddress;

}

在UI中显示MAC地址

获取到MAC地址后,我们可以将其显示在应用的界面上。以下是一个简单的示例,展示如何在TextView中显示MAC地址:

TextView textViewMacAddress = findViewById(R.id.textViewMacAddress);

textViewMacAddress.setText(macAddress);

完整示例代码

以下是完整的示例代码,涵盖了上述所有步骤:

public class MainActivity extends AppCompatActivity {

private TextView textViewMacAddress;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textViewMacAddress = findViewById(R.id.textViewMacAddress);

String macAddress = getMacAddress();

textViewMacAddress.setText(macAddress);

}

private String getMacAddress() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

return getMacAddressByIp(getLocalIpAddress());

} else {

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

return wifiInfo.getMacAddress();

}

}

private String getLocalIpAddress() {

try {

for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {

NetworkInterface intf = en.nextElement();

for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {

InetAddress inetAddress = enumIpAddr.nextElement();

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {

return inetAddress.getHostAddress();

}

}

}

} catch (SocketException ex) {

ex.printStackTrace();

}

return null;

}

private String getMacAddressByIp(String ipAddress) {

String macAddress = null;

try {

byte[] bytes = NetworkInterface.getByInetAddress(InetAddress.getByName(ipAddress)).getHardwareAddress();

StringBuilder sb = new StringBuilder();

for (int i = 0; i < bytes.length; i++) {

sb.append(String.format("%02X%s", bytes[i], (i < bytes.length - 1) ? ":" : ""));

}

macAddress = sb.toString();

} catch (Exception e) {

e.printStackTrace();

}

return macAddress;

}

}

结语

通过本文的详细讲解,相信你已经掌握了在Android应用中获取并显示设备MAC地址的方法。无论是低版本的Android系统,还是最新的Android版本,我们都可以通过不同的策略来获取到设备的MAC地址。希望这些内容能对你有所帮助,祝你在Android开发的道路上越走越远!

你我贷网贷需要什么条件 ?
D字母开头的明星列表