如何从本地PC的蓝牙MAC地址? [英] How to get bluetooth mac address from local pc?
问题描述
我要得到我的应用程序运行在PC上的蓝牙设备的MAC地址。
I want to get the mac address of the bluetooth device on the pc my application is running on.
我曾尝试以下内容:
private void GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
Console.WriteLine(macAddresses);
}
}
}
但输出不随commandprompt匹配IPCONFIG / ALL。它不打印我blueotths MAC地址。
任何解决方案?
But the output does not match with 'ipconfig /all' in commandprompt. It does not print my blueotths mac address. Any solutions?
我准备解析输出,我从IPCONFIG / ALL获得,但如何获取输出作为一个字符串?
I am ready to parse the output I get from 'ipconfig /all' but how do I get the output as a String?
推荐答案
您也许可以使用WMI来获得满意的结果。附上一个链接到一个WMI的解决方案,通过网络设备落后。
You can maybe use WMI to get the results. Herewith a link to a WMI solution that trails through the network devices.
我在这里张贴code的情况下,该网站已关闭,但都归功于原作者,心理$ C $铬。
使用WMI获取MAC地址在C#
I'm posting the code here in case the website is down, but all credit goes to the original author, PsychoCoder. Use WMI to get MAC Address in C#
而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;
}
请务必包括参考System.Management。
为了让您获得网络设备的名称,可以使用的obj [说明]的ToString();
您还可以看看MSDN有关WMI,特意到的 Win32_NetworkAdapterConfiguration类
You can also have a look at MSDN regarding WMI, specifically to the Win32_NetworkAdapterConfiguration Class
希望这有助于。
这篇关于如何从本地PC的蓝牙MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!