androidandroid-studioandroid-wifiwifi-directwifip2p

Wifi Direct - This device address not work on android 10


I use this method to get the MAC address of my device. This works well in Android 9 and below. But in Android 10, the MAC address is not given correctly and it gives this value every time: 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;
     }
}
}

Solution

  •   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;
        }