首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从本地电脑获取蓝牙mac地址?

如何从本地电脑获取蓝牙mac地址?
EN

Stack Overflow用户
提问于 2009-11-30 21:57:03
回答 2查看 9.3K关注 0票数 3

我想要获取运行应用程序的pc上蓝牙设备的mac地址。

我尝试过以下几种方法:

代码语言:javascript
复制
private void GetMacAddress()
{
     string macAddresses = "";
     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
     {
          if (nic.OperationalStatus == OperationalStatus.Up)
          {
               macAddresses += nic.GetPhysicalAddress().ToString();
               Console.WriteLine(macAddresses);
          }
     }
}

但输出与命令提示符中的'ipconfig /all‘不匹配。它不会打印我的blueotths地址。有什么解决方案吗?

我已经准备好解析从'ipconfig /all‘得到的输出了,但是我怎么才能得到字符串形式的输出呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-11-30 22:22:33

您也许可以使用WMI来获取结果。这里有一个指向WMI解决方案的链接,该解决方案通过网络设备跟踪。

我在这里发布了代码,以防网站宕机,但所有的功劳都归功于原始作者PsychoCoder。Use WMI to get MAC Address in C#

和代码:

代码语言:javascript
复制
//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["Description"].ToString();

您还可以查看有关WMI的MSDN,特别是与Win32_NetworkAdapterConfiguration Class有关的内容。

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2014-04-17 01:16:11

代码语言:javascript
复制
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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1819971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档