Are Wi-Fi Direct and “normal” Wi-Fi different MAC addresses?

I'm currently trying to connect two phones that know each other's MAC address via Wi-Fi Direct, and stumbled upon the following problem: The MAC address I get from

WifiManager wifiMan = (WifiManager) this .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInf = wifiMan.getConnectionInfo(); MAC_ADDRESS = wifiInf.getMacAddress(); 

slightly different from the one I get from WifiP2pManager when I discover and request peers. Example: a0:xx:xx:... turns into a2:xx:xx.... Does anyone know why? I did not find a way to get the “direct Wi-Fi MAC address”, and as I thought the MAC address should be unique, and this is the same Wi-Fi module that handles both (regular Wi-Fi and P2P / Direct) , This is very strange .. What should I do? For two devices (Galaxy Nexus), I only have the first two characters that differ by MAC address - should I just drop them? Is the chance of running into problems (two devices that differ only in the first part of the MAC address) too high?

Thanks.

+6
source share
6 answers

I looked for it during my project. My requirements were to uniquely identify devices in an adhoc P2p network formed 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.

My research:. The design is designed in such a way that there is a unique universal identifier and a local identifier. Reason: A universal identifier can only be used to connect to infrastructure mode networks. The local identifier can be used for ad-hoc networks (device for device). In this ad-hoc mode, it is possible that one device can belong to several ad-hoc groups at the same time.

  • Therefore, to support these parallel operations, support for P2p devices. Several MAC objects, possibly on different channels.
  • For each session, the persistent group MAY use a different channel and MAC device for each session.
  • P2P devices use their global MAC address as a device identifier during discovery and negotiation and a temporary local MAC address for all frames within the group. Clear from here

However, there is no direct way to get your own WiFi P2p MAC address. Problem 53437: Android .

In this discussion of the problem, a project member from Google suggested that it was possible and simple that it was not documented

Solution: Use the intent filter WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION and the optional intent WifiP2pManager.EXTRA_WIFI_P2P_DEVICE

This is how I used it in my project:

 @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(); } 

Hope this helps someone!

+4
source

Read about the MAC address on Wikipedia.

Addresses can be either generally accepted addresses or locally managed addresses.

Addresses administered globally and locally administered are distinguished by setting the second least significant bit of the most significant address byte. This bit is also referred to as the U / L bit, short for Universal / Local, which defines how the address is administered. If the bit is 0, the address is administered universally. If it is 1, the address is locally administered.

MAC 48 Address

Since Wi-Fi Direct is another stack on top of the MAC, you should also check what this bit can mean for it. I found a post discussion shedding some light on this. Obviously, the quote below is from the WFA specification.

The P2P device must assign a P2P interface address in the appropriate format described in clause 7.1.3.3.1 of the IEEE Std 802.11-2007 standard 1 , which is used to communicate with the owner or clients of a P2P group in a P2P group. The P2P interface address does not have to be globally unique and can be entered locally. The P2P interface address can be the same as the P2P device address provided P2P interface requirements. The address in this section is satisfied.

So, I believe that the answer to this question is: you should not take the MAC address from WifiManager and use it with Wi-Fi P2P connections.

+3
source

iFixit says the Galaxy Nexus uses the BCM4330 for its MAC, Baseband, and PHY, so if you have friends at Broadcom, you can ask them.

Unfortunately, the technical description is not public; the best i can do is to link you with a flowchart .

0
source

I struggled to find a way to get the WiFi Direct mac address instead, as my requirements were drafted on the assumption that this is possible.

Be that as it may, you create a single group of devices and get together with it the owner and address of the device.

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

Forget Wifi Manager. The direct Wifi address is not equal to the MAC address. The direct Wifi address is used for direct Wi-Fi connection. You cannot use anything.

To connect two devices with a direct Wi-Fi device, you need to create a WifiP2pGroup with one of your devices. Using another device, you need to search WifiP2pGroups, select your own and connect.

0
source

You can get the direct WiFi address using the following code:

 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; 

}

0
source

Source: https://habr.com/ru/post/917720/


All Articles