Get WiFi MAC address using API on Azure Sphere

stm32 1 Reputation point
2020-07-08T17:12:53.1+00:00

Hello,

Is there a way to get programmatically the MAC address of the WiFi hardware using the API?

Azure Sphere
Azure Sphere
An Azure internet of things security solution including hardware, operating system, and cloud components.
163 questions
{count} votes

2 answers

Sort by: Most helpful
  1. QuantumCache 20,261 Reputation points
    2020-07-17T20:32:50.343+00:00

    Hello @stm32-0752 , We had a similar kind of discussion in the past , if possible could you please share any User voice feedback being submitted from your side on this request.

    MSDN Similar discussion Thread: MAC address in Ethernet

    Quoted Response from Previous discussion:
    Friday, January 17, 2020 5:13 PM
    At this time there is no command to query the Ethernet interface and list properties associated to IP Address (IPv4) or the MAC address, etc...such as the case for the WiFi interface. This is a product backlog item and will be part of a future release.
    If you have additional feedback or questions, please let me know. I would also suggest to add an entry in the Azure IoT UserVoice to detail specific functionality or features you are looking for or that would be desired.
    If you are looking to access the Ethernet IP Address (IPv4) programmatically there is the following library: getifaddrs
    Please see the following Git Issue related to this: https://github.com/Azure/azure-sphere-samples/issues/97

    [3]: /api/attachments/12901-image.png


  2. James Higgins 316 Reputation points
    2021-04-01T16:19:19.487+00:00

    You can get the MAC address from the Sphere high-level application, at least with the latest OS/SDK as of this posting:

    #include <ifaddrs.h>
    #include <net/ethernet.h>
    #include <sys/socket.h>
    #include <netpacket/packet.h>
    
    void getIP(void)
    {
        struct ifaddrs* addr_list;
        struct ifaddrs* it;
        int n;
        const char EthInterface[] = "wlan0";
        if (getifaddrs(&addr_list) < 0) {
            Log_Debug("***** GETIFADDRs failed! ***\n");
        }
        else {
            for (it = addr_list, n = 0; it != NULL; it = it->ifa_next, ++n) {
                if (it->ifa_addr == NULL) {
                    continue;
                }
                if (strncmp(it->ifa_name, EthInterface, sizeof(EthInterface)) == 0) {
                    if (it->ifa_addr->sa_family == AF_INET) {
                        struct sockaddr_in* addr = (struct sockaddr_in*)it->ifa_addr;
                        char* ip_address = inet_ntoa(addr->sin_addr);
                        Log_Debug("**** wlan0 IP found: %s ***\n", ip_address);
                    }
                    else if (it->ifa_addr->sa_family == AF_PACKET) {
                        struct sockaddr_ll* mac_addr = (struct sockaddr_ll*)it->ifa_addr;
                        unsigned char* mac = mac_addr->sll_addr;
    
                        char mac_string[20];
                        sprintf(mac_string, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],
                            mac[1], mac[2], mac[3], mac[4], mac[5]);
                        Log_Debug("**** wlan0 MAC 1 found: %s ****\n", mac_string);
                    }
                }
            }
        }
        freeifaddrs(addr_list);
    }
    
    0 comments No comments