How do you get a direct wifi MAC address?

I am trying to get the MAC address of an Android device. This is usually possible through the WiFiManager API if WiFi is enabled.

Is there a way to get the MAC address if WiFi is turned off and WiFi Direct is turned on? WiFi and WiFi Direct cannot be on my phone at the same time.

thank

+3
source share
4 answers

I looked for it during my project. My requirements were to uniquely identify devices in an adhoc P2p network configured using Wi-Fi Direct. Each device should identify its friendly device the next time it approaches. I need my own WiFi (Direct) MAC and my friends to create a key to create this zone for friends.

:. , . : . "ad-hoc" ( ). ad-hoc , ad-hoc.

  • , P2p- MAC, , .
  • MAC .
  • P2P MAC- MAC- .

MAC- WiFi P2p. 53437: Android.

Google , ,

: WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION WifiP2pManager.EXTRA_WIFI_P2P_DEVICE

:

@Override
public void onReceive(Context context, Intent intent) {
....
....
String action = intent.getAction();

if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
            .equals(action)) {

        WifiP2pDevice device = (WifiP2pDevice) intent
                .getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);

        String myMac = device.deviceAddress;

        Log.d(TAG, "Device WiFi P2p MAC Address: " + myMac);

/* Saving WiFi P2p MAC in SharedPref */

        sharedPref = context.getSharedPreferences(context.getString(R.string.sp_file_name), Context.MODE_PRIVATE);
        String MY_MAC_ADDRESS = sharedPref.getString(context.getString(R.string.sp_field_my_mac), null);

        if (MY_MAC_ADDRESS == null || MY_MAC_ADDRESS != myMac) {
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString(context.getString(R.string.sp_field_my_mac), myMac);
            editor.commit();
        }

, -!

+7

MAC- Wi-Fi MAC- WiFi Direct. . .

+2

Mac- WiFi MAC- WiFi.

WiFi, :

    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 (int i=0; i<byteMac.length; i++) {
                    strBuilder.append(String.format("%02X:", byteMac[i]));
                }

                if (strBuilder.length()>0){
                    strBuilder.deleteCharAt(strBuilder.length()-1);
                }

                return strBuilder.toString();
            }

        }
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}
+2

WiFi Direct MAC address will be different. He clearly explained here @auselen fooobar.com/questions/917720 / ... .

I just figured out a way to get WiFi Direct mac address. It is ugly, but it does its job. Here is 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));
        }
    });
0
source

All Articles