Search Unity

Android and Iphone MacAddress

Discussion in 'Scripting' started by zhapness, Oct 17, 2011.

  1. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    Hi

    Is there any way to get the mac addresses of Android and Iphone devices?


    I have tried with


    for(var stuff : NetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
    {
    if (stuff.OperationalStatus == OperationalStatus.Up)
    {
    var macAddress = stuff.GetPhysicalAddress().ToString();
    }
    print(macAddress);
    }


    But it prints 4 things, only one of them the mac address
    and this is just for pc.

    Thanks :)
     
    Last edited: Oct 18, 2011
  2. zhapness

    zhapness

    Joined:
    Apr 10, 2011
    Posts:
    58
    Okey I got the mac address from my pc with this:

    Code (csharp):
    1.  
    2.     for(var stuff : NetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
    3.  
    4.     {
    5.  
    6.         if (stuff.OperationalStatus == OperationalStatus.Up)
    7.  
    8.         {
    9.  
    10.             var macAddress = stuff.GetPhysicalAddress().ToString();
    11.  
    12.             if(!macAddress.StartsWith("0000")  macAddress!="")
    13.  
    14.             {
    15.  
    16.                 print(macAddress);
    17.  
    18.             }
    19.  
    20.         }
    21.  
    22.     }
    23.  
    But it does not work with android, I can imagine that it should look totaly different for Android and Iphone, but I have found nothing about it.
     
  3. marclar83

    marclar83

    Joined:
    Jun 7, 2012
    Posts:
    14
    I was wondering the same thing, can you by any chance post the whole script that worked on your PC, the script as it is cannot run on it's own.

    It would be great also if it could work on both iOS and Android.

    Right now I have found a way in pure C for iOS but I don't know how to make it work with Unity3D and if it can be used on Android as well.

    If anybody can find more informations it will be very appreciated.
     
  4. rodrigotakase

    rodrigotakase

    Joined:
    May 31, 2010
    Posts:
    2
    Hey guys! I found the solution. This code works on MacOS and on iOS. I haven't tested on Android.

    Code (csharp):
    1.  
    2. // Returns the 1st valid Mac Address
    3. function GetMacAddress(){
    4.     var macAdress = "";
    5.     var nics : NetworkInterface[] = NetworkInterface.GetAllNetworkInterfaces();
    6.    
    7.     var i = 0;
    8.     for (var adapter: NetworkInterface in nics){
    9.         var address: PhysicalAddress = adapter.GetPhysicalAddress();
    10.         if(address.ToString() != ""){
    11.             macAdress = address.ToString();
    12.             return macAdress;
    13.         }
    14.     }
    15.     return 0;
    16. }
    17.  

    Cheers! Good Luck on your project!
     
  5. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,467
    This works great just don't forget
    at the top.

    EDIT:: Still doesn't work on my kindle fire but works correctly on my PC....hmm
     
    Last edited: Jul 30, 2012
  6. Billboard

    Billboard

    Joined:
    Aug 22, 2012
    Posts:
    7
    One solution for Android devices, using Unity's AndroidJavaObject constructor:

    Code (csharp):
    1.  
    2.    AndroidJavaObject mWiFiManager;
    3.  
    4.    string ReturnMacAddress()
    5.    {
    6.       string macAddr = "";
    7.       if (mWiFiManager == null)
    8.       {
    9.          using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
    10.          {
    11.             mWiFiManager = activity.Call<AndroidJavaObject>("getSystemService","wifi");
    12.          }
    13.       }
    14.       macAddr = mWiFiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<string>("getMacAddress"
    15.       return macAddr;
    16.   }
    17.  
    Note, this only returns a valid MAC address if the WiFi adapter is already enabled on the device.
     
    Last edited: Nov 15, 2012
  7. iscaru1988

    iscaru1988

    Joined:
    Aug 9, 2013
    Posts:
    1
    Hello sorry for keep wth this thread, but Im still unable to access the mac of my android device from unity.
    I think your answer is the most aproximate to the solution but I still can't access the mac of the device,
    ¿Please can someone help me?
     
  8. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    Hello, I can't make this work, this part is not recognised on mono develop: AndroidJavaObject mWiFiManager;
     
    master2001 likes this.
  9. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    756
    I dont know exactly how people feel about resurrecting dead threads but I would recommend creating your own one thread as this usually annoys people on forums.
     
  10. master2001

    master2001

    Joined:
    Jul 14, 2015
    Posts:
    1
    I hope this help
    in unity go to
    Assets => Plugins => Android => AndroidManifest.xml
    and add this line of Code as permissions

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

    and script

    1. Code (CSharp):
      1.        AndroidJavaObject mWiFiManager;
      2.  
      3.        string ReturnMacAddress()
      4.        {
      5.           string macAddr = "";
      6.           if (mWiFiManager == null)
      7.           {
      8.              using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
      9.              {
      10.                 mWiFiManager = activity.Call<AndroidJavaObject>("getSystemService","wifi");
      11.              }
      12.           }
      13.           macAddr = mWiFiManager.Call<AndroidJavaObject>("getConnectionInfo").Call<string>("getMacAddress");
      14.           return macAddr;
      15.       }
     
    osamansr2o1oo and xakus like this.
  11. Black_Demon

    Black_Demon

    Joined:
    Jul 12, 2014
    Posts:
    12
    Not working on Android 6.0+
     
    APGame likes this.
  12. APGame

    APGame

    Joined:
    Jul 18, 2017
    Posts:
    2
    Работает на 7-м Анройде а на 6-м по прежнему хуй... =(
    public void DisplayTypeAndAddress()
    {
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    txt.text = txt.text + (nics.Length + " nics").ToString() + "\n";
    foreach (NetworkInterface adapter in nics)
    {
    IPInterfaceProperties properties = adapter.GetIPProperties();
    txt.text = txt.text + (adapter.Description).ToString() + "\n";
    txt.text = txt.text + (String.Empty.PadLeft(adapter.Description.Length, '=')).ToString() + "\n";
    txt.text = txt.text + (" Interface type .......................... :" + adapter.NetworkInterfaceType) + "\n";
    txt.text = txt.text + (" Physical Address ........................ :" + adapter.GetPhysicalAddress()) + "\n";
    txt.text = txt.text + (" Is receive only.......................... :" + adapter.IsReceiveOnly) + "\n";
    txt.text = txt.text + (" Multicast................................ :" + adapter.SupportsMulticast) + "\n";
    }
    }
     
  13. APGame

    APGame

    Joined:
    Jul 18, 2017
    Posts:
    2
    Если те кто нашёл решение под 6-й андройд, прошу поделиться.
     
  14. Black_Demon

    Black_Demon

    Joined:
    Jul 12, 2014
    Posts:
    12
    @APGame Hi, I would like to point out that I was using @master2001 solution, which worked fine on my testing device (Android 4.0), but after testing in others devices and some research I found this code it is not working anymore on Android 6.0+. Unfortunately, I didn´t find another way to retrieve the Mac Address, but in my case I needed a unique identifier for each device, so the solution I found (although it is not the best one, it is the most reliable at the moment) is quite simple, use https://docs.unity3d.com/ScriptReference/SystemInfo-deviceUniqueIdentifier.html instead. I took the solution from here: http://answers.unity3d.com/questions/659273/how-to-find-mac-address-in-ios-and-android.html. Hope this helps you or somebody else.
     
    sandolkakos and Xpoint like this.
  15. Ban-jason

    Ban-jason

    Joined:
    Nov 11, 2014
    Posts:
    7
    I have search this solution for a long time, there is no way that i can find to sovle this problem by Using Unity API, the way i take is building plugins to Android/IOS.

    if anyone know the solution to sovle this issue, I am glad to hear from you~~
     
  16. shrutisinghcse056

    shrutisinghcse056

    Joined:
    Mar 10, 2018
    Posts:
    3
    i executed this code. i got 02:00:00:00::00:00... This is not my mac.
     
    anamta93 likes this.
  17. anamta93

    anamta93

    Joined:
    Mar 21, 2014
    Posts:
    12
    m having the same issue