How do you retrieve the WiFi Direct MAC address?

前端 未结 4 594
猫巷女王i
猫巷女王i 2021-01-14 10:12

I am trying to retrieve the MAC address of an Android device. This is ordinarily possible through the WiFiManager API if WiFi is on.

Is there any way to get the MAC

4条回答
  •  误落风尘
    2021-01-14 10:49

    The mac address of WiFi is different than that of WiFi Direct.

    You can get WiFi direct address using next code:

        public String getWFDMacAddress(){
        try {
            List 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 (int i=0; i0){
                        strBuilder.deleteCharAt(strBuilder.length()-1);
                    }
    
                    return strBuilder.toString();
                }
    
            }
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        return null;
    }
    

提交回复
热议问题