Comment obtenir l'adresse mac Bluetooth sur un PC local?
-
10-07-2019 - |
Question
Je souhaite obtenir l'adresse MAC du périphérique Bluetooth sur le PC sur lequel l'application est exécutée.
J'ai essayé les solutions suivantes:
private void GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
Console.WriteLine(macAddresses);
}
}
}
Mais le résultat ne correspond pas à "ipconfig / all" dans l'invite de commande. Il n’imprime pas mon adresse bleue blueotths. Des solutions?
Je suis prêt à analyser le résultat obtenu par 'ipconfig / all', mais comment puis-je obtenir le résultat sous forme de chaîne?
La solution
Vous pouvez peut-être utiliser WMI pour obtenir les résultats. Ci-joint un lien vers une solution WMI qui parcourt les périphériques réseau.
Je poste le code ici au cas où le site Web serait en panne, mais tout le mérite revient à l'auteur original, PsychoCoder. Utiliser WMI pour obtenir une adresse MAC en 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 obtenir le nom du périphérique réseau, vous pouvez utiliser le obj [" description]]. ToString ();
Vous pouvez également consulter MSDN en ce qui concerne WMI, plus précisément à Classe Win32_NetworkAdapterConfiguration
J'espère que cela vous aidera.
Autres conseils
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;
}