2009-11-30 11 views
2

Je souhaite obtenir l'adresse MAC du périphérique Bluetooth sur le PC sur lequel l'application est en cours d'exécution.Comment obtenir l'adresse MAC bluetooth à partir du PC local?

J'ai essayé ce qui suit:

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

Mais la sortie ne correspond pas à 'ipconfig/all' dans CommandPrompt. Il n'imprime pas mon adresse mac blueotths. Des solutions?

Je suis prêt à analyser la sortie que je reçois de 'ipconfig/all' mais comment puis-je obtenir la sortie en tant que chaîne?

Répondre

2

Vous pouvez utiliser WMI pour obtenir les résultats. Ci-joint un lien vers une solution WMI qui suit les périphériques réseau.

Je poste le code ici au cas où le site est en panne, mais tout le mérite revient à l'auteur original, PsychoCoder. Use WMI to get MAC Address in C#

Et le code:

//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; 
} 

Assurez-vous d'inclure la référence à System.Management. Pour vous d'obtenir le nom du périphérique réseau, vous pouvez utiliser le obj["Description"].ToString();

Vous pouvez également jeter un oeil à MSDN concernant WMI, plus précisément à la Win32_NetworkAdapterConfiguration Class

Hope this helps.

+0

Cool! Ça a marché. Merci :) –

+0

Content de pouvoir aider. – Riaan

2
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; 
} 
Questions connexes