Sponser Link

How to get mac address in xamarin android programmatically

Mac address is universal contact alphanumeric address of any communication device. It’s basically unique no of LAN or Wi-Fi (Network hardware) which is assign by its manufacturing company.

In xamarin, you can find the mac address using network interface class. Device can be depending upon LAN or Wi-Fi network. Therefore we will find all available networks. Mac address will be store in array and by default we will pick the mac address on zero indexes. You can modify given below function if you want specific wireless or Ethernet mac address. Given below function will be return mac address of device for wireless or LAN which one is available.

  private string GetDeviceInfo()
        {
            string mac = string.Empty;
            string ip = string.Empty;

            foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    var address = netInterface.GetPhysicalAddress();
                    mac = BitConverter.ToString(address.GetAddressBytes());

                    IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
                    if (addresses != null && addresses[0] != null)
                    {
                        ip = addresses[0].ToString();
                        break;
                    }
                }
            }

            return mac;
        }