Android 目标版本 = 29,编译 SDK = 29
我在 Android 10、11、12 中获得了 Wifi Mac 地址,但仅在 Android 13 中无法获得 Wifi Mac 地址。
我们仅获得默认 Mac 地址 02:00:00:00:00:00。
未获取在 Wifi 中可用的正确 Mac 地址
Build.gradle 文件
plugins {
id("com.android.application")
}
android {
namespace = "com.mantra.wifimacaddress"
compileSdk = 29
defaultConfig {
applicationId = "com.mantra.wifimacaddress"
minSdk = 24
targetSdk = 29
versionCode = 1
versionName = "1.0"
multiDexEnabled = true
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.1.0")
implementation("com.google.android.material:material:1.2.1")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("com.karumi:dexter:6.2.3")
}
获取Mac地址
public String getMacAddr() {
try {
WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
String bssid = info.getBSSID();
System.out.println("Get SSID ====> " + ssid + " Get BSSID====> " + bssid);
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 "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
// res1.append(Integer.toHexString(b & 0xFF) + ":");
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "02:00:00:00:00:00";
}
多年来无法获得正确的 MAC 地址。任何非根方法只会给出假值。网上有大量关于这种行为的文章。这是由于操作系统的隐私设置造成的。这是一项安全功能。它无法被绕过。