¿Cómo obtener la dirección MAC de Bluetooth de la PC local?
-
10-07-2019 - |
Pregunta
Quiero obtener la dirección mac del dispositivo Bluetooth en la PC en la que se ejecuta mi aplicación.
He intentado lo siguiente:
private void GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
Console.WriteLine(macAddresses);
}
}
}
Pero el resultado no coincide con 'ipconfig / all' en commandprompt. No imprime mi dirección mac de blueotths. ¿Alguna solución?
Estoy listo para analizar el resultado que obtengo de 'ipconfig / all', pero ¿cómo obtengo el resultado como una cadena?
Solución
Quizás pueda usar WMI para obtener los resultados. Adjunto un enlace a una solución WMI que rastrea los dispositivos de red.
Estoy publicando el código aquí en caso de que el sitio web esté caído, pero todo el crédito va al autor original, PsychoCoder. Use WMI para obtener la dirección MAC en C #
Y el 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;
}
Asegúrese de incluir la referencia a System.Management.
Para que pueda obtener el nombre del dispositivo de red, puede usar el obj["Description"font>.ToString();
También puede echar un vistazo a MSDN con respecto a WMI, específicamente a Win32_NetworkAdapterConfiguration Class
Espero que esto ayude.
Otros consejos
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;
}