Never one to back down from a challenge, I decided to try to get the MAC address from my Raspberry Pi running Windows 10 IoT Core.  It was clear to me from doing some searching that Windows Universal Apps cannot get the MAC address, but all of that advice might just be wrong.

 

Conventional wisdom is that we don’t need to know the MAC address because it is just a unique number, and we can get a UUID associated with our system – but that is to ignore the reasons why we might want the MAC address, which include but are not limited to:

    1.      1.  We have a tradition of identifying systems using the MAC address of the on board Ethernet port, so changing it now would create unnecessary work.
    2.      2.  We need to show the user the MAC address so they can set up their router with a static IP for the device.
    3.      3.  Manufacturing needs a way to confirm the MAC address

 

Then I noticed a little thing, in the web interface for the Raspberry Pi it shows the MAC address for the onboard Ethernet port.  Being creative I decided to figure out how it does this.  It turns out that it is a simple REST API, or HTTP GET, that returns JSON data with information about the network devices.

 

The following code will return the MAC address of the first Ethernet device found:

 

String  MacAddress = await GetMAC();

 

    private async Task<String> GetMAC()
    {
        String MAC = null;
        StreamReader SR = await GetJsonStreamData("http://localhost:8080/api/networking/ipconfig");
        JsonObject ResultData = null;
        try
        {
            String JSONData;

            JSONData = SR.ReadToEnd();

            ResultData = (JsonObject)JsonObject.Parse(JSONData);
            JsonArray Adapters = ResultData.GetNamedArray("Adapters");

            //foreach (JsonObject Adapter in Adapters)
            for (uint index = 0; index < Adapters.Count; index++)
            {
                JsonObject Adapter = Adapters.GetObjectAt(index).GetObject();
                String Type = Adapter.GetNamedString("Type");
                if (Type.ToLower().CompareTo("ethernet") == 0)
                {
                    MAC = ((JsonObject)Adapter).GetNamedString("HardwareAddress");
                    break;
                }
            }
        }
        catch (Exception E)
        {
            System.Diagnostics.Debug.WriteLine(E.Message);
        }

        return MAC;
    }

    private async Task<StreamReader> GetJsonStreamData(String URL)
    {
        HttpWebRequest wrGETURL = null;
        Stream objStream = null;
        StreamReader objReader = null;

        try
        {
            wrGETURL = (HttpWebRequest)WebRequest.Create(URL);
            wrGETURL.Credentials = new NetworkCredential("Administrator", "p@ssw0rd");
            HttpWebResponse Response = (HttpWebResponse)(await wrGETURL.GetResponseAsync());
            if (Response.StatusCode == HttpStatusCode.OK)
            {
                objStream = Response.GetResponseStream();
                objReader = new StreamReader(objStream);
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("GetData " + e.Message);
        }
        return objReader;
    }

 

 

Copyright © 2015 – Bruce Eitman and Embedded101.com
All Rights Reserved