How do you retrieve the WiFi Direct MAC address?

前端 未结 4 505
猫巷女王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:50

    The WiFi Direct mac address is going to be different. It's explained beautifully by @auselen here https://stackoverflow.com/a/14480530/3167704.

    I just figured out a way to retrieve WiFi Direct mac address. It isn't pretty but gets the job done. Here's the code,

        final WifiP2pManager p2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
        final WifiP2pManager.Channel channel = p2pManager.initialize(this, getMainLooper(), null);
    
        p2pManager.createGroup(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                p2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
                    @Override
                    public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
                        Log.i("", wifiP2pGroup.getOwner().deviceAddress);
    
                        // Following removal necessary to not have the manager busy for other stuff, subsequently
                        p2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {
                            @Override
                            public void onSuccess() {
                                Log.i("", "Removed");
                            }
    
                            @Override
                            public void onFailure(int i) {
                                Log.i("", "Failed " + i);
                            }
                        });
                    }
                });
            }
    
            @Override
            public void onFailure(int i) {
                Log.i("", String.valueOf(i));
            }
        });
    

提交回复
热议问题