Wifi Direct - 此设备地址不适用于 android 10
Wifi Direct - This device address not work on android 10
我使用此方法获取我设备的 MAC 地址。这在 Android 9 及以下版本中运行良好。但是在Android10中,MAC地址没有正确给出,每次都给出这个值:02: 00: 00: 00: 00: 00
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:
WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
Log.i("My device wifi direct Mac address", device.deviceAddress);
break;
}
}
}
public String getWFDMacAddress() {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ntwInterface : interfaces) {
if (ntwInterface.getName().equalsIgnoreCase("p2p0")) {
byte[] byteMac = ntwInterface.getHardwareAddress();
if (byteMac == null) {
return null;
}
StringBuilder strBuilder = new StringBuilder();
for (byte b : byteMac) {
strBuilder.append(String.format("%02X:", b));
}
if (strBuilder.length() > 0) {
strBuilder.deleteCharAt(strBuilder.length() - 1);
}
return strBuilder.toString();
}
}
} catch (Exception ignored) {
}
return null;
}
我使用此方法获取我设备的 MAC 地址。这在 Android 9 及以下版本中运行良好。但是在Android10中,MAC地址没有正确给出,每次都给出这个值:02: 00: 00: 00: 00: 00
public class WifiDirectBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:
WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
Log.i("My device wifi direct Mac address", device.deviceAddress);
break;
}
}
}
public String getWFDMacAddress() {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ntwInterface : interfaces) {
if (ntwInterface.getName().equalsIgnoreCase("p2p0")) {
byte[] byteMac = ntwInterface.getHardwareAddress();
if (byteMac == null) {
return null;
}
StringBuilder strBuilder = new StringBuilder();
for (byte b : byteMac) {
strBuilder.append(String.format("%02X:", b));
}
if (strBuilder.length() > 0) {
strBuilder.deleteCharAt(strBuilder.length() - 1);
}
return strBuilder.toString();
}
}
} catch (Exception ignored) {
}
return null;
}