学向勤中得,萤窗万卷书。这篇文章主要讲述在安卓6.0(及以上)设备上无法获取无线网卡MAC地址的解决方案相关的知识,希望能为你提供帮助。
在安卓6.0以下的设备上,通过WifiManager.getConnectionInfo().getMacAddress()即可获取WLAN物理地址,
而在6.0及以上,以此方式获取到的MAC地址为固定值02:00:00:00:00:00,而非真实值
解决方案
WlanMacAddressGetter.java
package com.example.buyishi.myapplication; import android.util.Log; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class WlanMacAddressGetter { private static final String TAG = WlanMacAddressGetter.class.getName(); public static String getWlanMacAddress() { try { Enumeration< NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); if (networkInterface.getName().equals("wlan0")) { StringBuilder mac = new StringBuilder(); byte[] hardwareAddress = networkInterface.getHardwareAddress(); String hex = Integer.toHexString(hardwareAddress[0] & 0xff); if (hex.length() == 1) { mac.append(‘0‘); } mac.append(hex); for (int i = 1; i < hardwareAddress.length; ++i) { mac.append(‘:‘); hex = Integer.toHexString(hardwareAddress[i] & 0xff); if (hex.length() == 1) { mac.append(‘0‘); } mac.append(hex); } return mac.toString(); } } } catch (SocketException ex) { Log.e(TAG, null, ex); } return null; } }
注意:须声明权限android.permission.INTERNET,否则在获取MAC时会引发SocketException
【在安卓6.0(及以上)设备上无法获取无线网卡MAC地址的解决方案】
推荐阅读
- 掌上考勤app|掌上考勤最新版下载
- 微博下载|微博app下载
- LeetCode算法题-Happy Number(Java实现)
- How to sign app
- StandardWrapper
- A Survey of Machine Learning Techniques Applied to Software Defined Networking (SDN): Research Issue
- APP自动化测试各项指标分析
- Android下的adb命令大集合
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"(示