Obtaining the MAC Address from an Oculus GO device.

Recently I needed to obtain the MAC address from an Oculus GO device, from inside an Unity script.

private string GeMacAddress()
{ 
   ... 
}

Here is what I did.

The idea is to gather all the Network Interfaces on the Oculus GO device and search for the one named ‘wlan0’. This entry is usually the device main wireless network interface.
Once you have that you can get the hardware address as a signed byte array. Than you simply convert it to an hexadecimal array. That should be your device’s mac address.

Since you need to call methods only available on the native Android, you must use the AndroidJavaClass and AndroidJavaObject Unity API.

Full code:

private string GeMacAddress()
{
    Dictionary<string, sbyte[]> results = new Dictionary<string, sbyte[]>();

    #if UNITY_ANDROID && !UNITY_EDITOR
    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        using (AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity"))
        {
            AndroidJavaClass NetworkInterfaceClass = new AndroidJavaClass("java.net.NetworkInterface");
            AndroidJavaClass CollectionsObject = new AndroidJavaClass("java.util.Collections");
            AndroidJavaObject lst = CollectionsObject.CallStatic<AndroidJavaObject>("list",NetworkInterfaceClass.CallStatic<AndroidJavaObject>("getNetworkInterfaces"));  
                              
            int size = lst.Call<int>("size");
            for (int i = 0; i < size; i++)
            {
                string n = lst.Call<AndroidJavaObject>("get", i).Call<string>("getName");
 
                AndroidJavaObject obj = NetworkInterfaceClass.CallStatic<AndroidJavaObject>("getByName",n).Call<AndroidJavaObject>("getHardwareAddress");
                if (obj != null)
                {
                     if (obj.GetRawObject() != null)
                     { 
                         if (obj.GetRawObject().ToInt32() != 0) 
                         {
                             sbyte[] result = AndroidJNIHelper.ConvertFromJNIArray<sbyte[]>(obj.GetRawObject());
                              if (n != null && result != null)
                              {
                                  results.Add(n,result);
                              }
                         }
                     }
                }
            }       
        }
    }
    #endif
    foreach (KeyValuePair<string, sbyte[]> kvp in results)
    {
        if (kvp.Key.Equals("wlan0"))
        {
            return ByteArrayToString((byte[])(Array)kvp.Value);
        }
    }
    return "Mac address not found..";
}

 

The ByteArrayToString that I use on the function above (obtained from SO)

public static string ByteArrayToString(byte[] ba)
{
    if (ba == null)
    {
        Debug.Log("Error array is null");
        return "";
    }
    StringBuilder hex = new StringBuilder(ba.Length * 2);
    foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
    return hex.ToString();
}

 

 

Leave a Reply