3

Android デバイスの MAC アドレスを取得しようとしています。これは通常、WiFi がオンになっている場合、WiFiManager API を通じて可能です。

WiFi がオフで WiFi Direct がオンの場合、MAC アドレスを取得する方法はありますか? スマートフォンで WiFi と WiFi Direct を同時にオンにすることはできません。

ありがとう

4

4 に答える 4

10

I had been searching for this during my project. My requirements were to uniquely identify devices in an adhoc P2p network formed with WiFi Direct. Each device should identify its friend device the next time when it comes into proximity. I needed my own WiFi (Direct) MAC and my friends' to create a Key for this friend zone creation.

My Research: The design is in such a way that there is an Unique Universal ID and a Local ID. Reason: Universal ID can only be used to connect to Infrastructure mode Networks. Local ID could be used for "ad-hoc" mode networks(device to device). In this ad-hoc mode, there are possibilities that a single device might simultaneosly belong to several ad-hoc groups.

  1. Hence to support this concurrent operations, P2p devices support Multiple MAC entities, possibly on different channels.
  2. For each session, a persistent group MAY use a different channel and device MAC for each session.
  3. P2P devices use their global MAC address as Device ID during discovery and negotiation, and a temporary local MAC address for all frames within a group. Understood from here

However, there is NO straight forward way to obtain one's own WiFi P2p MAC address. Issue 53437: Android.

In this issue discussion, the project member from google has suggested this is possible and just that it hasn't been documented

Solution: Using intent filter WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION and the extra from the intent WifiP2pManager.EXTRA_WIFI_P2P_DEVICE

This is how I have 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!

于 2015-03-01T17:20:28.210 に答える
2

WiFi の MAC アドレスは、WiFi Direct の MAC アドレスとは異なります。

次のコードを使用して 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;
}
于 2015-04-16T16:30:58.903 に答える
2

WiFi の MAC アドレスは、WiFi Direct の MAC アドレスとは異なります。通常、最初の 2 文字は異なる場合があります。そのことに気をつけてください。

于 2013-03-28T15:05:47.287 に答える
0

WiFi Direct の MAC アドレスは異なります。ここで @auselen によって美しく説明されています https://stackoverflow.com/a/14480530/3167704

WiFi Direct の MAC アドレスを取得する方法を見つけました。それはきれいではありませんが、仕事を成し遂げます。これがコードです。

    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));
        }
    });
于 2014-05-01T13:27:58.053 に答える