Search Unity

Has anyone successfully grabbed Mac Address in Android before?

Discussion in 'Multiplayer' started by CSEliot, Nov 9, 2015.

  1. CSEliot

    CSEliot

    Joined:
    May 9, 2014
    Posts:
    33
    My client requests it, but google searches come up with not-so-useful content. Here's what I've tried:
    Code (CSharp):
    1.     string ReturnMacAddress()
    2.    {
    3.       string macAddr = "";
    4.       if (mWifiManager == null)
    5.       {
    6.          using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
    7.          {
    8.             mWifiManager = activity.Call<AndroidJavaObject>("getSystemService","wifi");
    9.          }
    10.       }
    11.       macAddr = mWifiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<string>("getMacAddress");
    12.       return macAddr;
    13.   }
    This one has a "permission to "access_wifi_state"" error which I'm assuming is XML related, but as far as I can tell, XML permission settings are for google play downloads only and my client won't be using google play.

    Code (CSharp):
    1.     private string GetMacAddress()
    2.     {
    3.         NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    4.         string macAddress = "";
    5.  
    6.         foreach (NetworkInterface adapter in nics)
    7.         {
    8.             PhysicalAddress address = adapter.GetPhysicalAddress();
    9.             byte[] bytes = address.GetAddressBytes();
    10.             string mac = null;
    11.             for (int i = 0; i < bytes.Length; i++)
    12.             {
    13.                 mac = string.Concat(mac + (string.Format("{0}", bytes[i].ToString("X2"))));
    14.                 if (i != bytes.Length - 1)
    15.                 {
    16.                     mac = string.Concat(mac + "-");
    17.                 }
    18.             }
    19.             macAddress += mac;
    20.         }
    21.         return macAddress;
    22.     }
    This one gets a "System.Net.NetworkInformation.GetAllNetworkInterfaces() Fails" which seems to be an old bug that got fixed for android 4.2.2+ but . . . well it doesn't work on my 4.4.2 tablet @_@

    Any ideas? The darn thing is due in less than a week, any help would be greatly appreciated!
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    The first code you showed seems like the way to go. It still requires you to declare permissions in the AndroidManifest.xml (that is a file that describes certain things about your app/game to the Android OS). It is not related to the store or where you downloaded the game from.
     
  3. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    Has Anyone successfully read a mac address on Android Device? I can Only make it work on my mac.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Android provides the APIs to access this information. You can call these APIs from Unity to get the MAC address. I can post the code that is required for that if you want that.
     
    Munchy2007 likes this.
  5. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    That would be great! please!
     
  6. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    @liortal Would be great If you can post the code.
     
  7. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    @liortal hi sir can you provide the code that can get the mac address on android?
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't have such code. you can PM me and maybe i can assist you
     
  9. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Pmed