Pergunta

Eu quero obter o endereço MAC do dispositivo Bluetooth no PC meu aplicativo está sendo executado.

Eu tentei o seguinte:

private void GetMacAddress()
{
     string macAddresses = "";
     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
     {
          if (nic.OperationalStatus == OperationalStatus.Up)
          {
               macAddresses += nic.GetPhysicalAddress().ToString();
               Console.WriteLine(macAddresses);
          }
     }
}

Mas a saída não coincide com 'ipconfig / all' em CommandPrompt. Não imprimir o meu endereço blueotths mac. Todas as soluções?

Estou pronto para analisar a saída que recebo de 'ipconfig / all', mas como faço para obter o resultado como uma String?

Foi útil?

Solução

Você pode talvez usar WMI para obter os resultados. Com isto um link para uma solução WMI que trilhas através dos dispositivos de rede.

Estou postando o código aqui no caso do site está em baixo, mas todo o crédito vai para o autor original, PsychoCoder. Use WMI para obter o endereço MAC em C #

E o código:

//Namespace reference
using System.Management;

/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
    //create out management class object using the
    //Win32_NetworkAdapterConfiguration class to get the attributes
    //af the network adapter
    ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
    //create our ManagementObjectCollection to get the attributes with
    ManagementObjectCollection objCol = mgmt.GetInstances();
    string address = String.Empty;
    //My modification to the code
    var description = String.Empty;
    //loop through all the objects we find
    foreach (ManagementObject obj in objCol)
    {
        if (address == String.Empty)  // only return MAC Address from first card
        {
            //grab the value from the first network adapter we find
            //you can change the string to an array and get all
            //network adapters found as well
            if ((bool)obj["IPEnabled"] == true)
            {
                address = obj["MacAddress"].ToString();
                description = obj["Description"].ToString();
            }
        }
       //dispose of our object
       obj.Dispose();
    }
    //replace the ":" with an empty space, this could also
    //be removed if you wish
    address = address.Replace(":", "");
    //return the mac address
    return address;
}

Certifique-se de incluir a referência ao System.Management. Para que você obtenha o nome do dispositivo de rede, você pode usar o obj["Description"].ToString();

Você também pode dar uma olhada no MSDN sobre WMI, especificamente para o Win32_NetworkAdapterConfiguration Classe

Espero que isso ajude.

Outras dicas

public static PhysicalAddress GetBTMacAddress()  {

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {

        // Only consider Bluetooth network interfaces
        if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx && 
            nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211){

            return nic.GetPhysicalAddress();
        }
    }
    return null;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top