【问题标题】:How to get bluetooth mac address from local pc?如何从本地电脑获取蓝牙mac地址?
【发布时间】:2009-11-30 13:57:02
【问题描述】:

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

我尝试了以下方法:

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”不匹配。它不打印我的蓝牙 mac 地址。 有什么解决办法吗?

我已准备好解析从 'ipconfig /all' 获得的输出,但如何将输出作为字符串获取?

【问题讨论】:

    标签: c# bluetooth mac-address


    【解决方案1】:
    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;
    }
    

    【讨论】:

    • 这仍然会返回一些以太网连接。我唯一能做的就是过滤包含“以太网”的名称/描述。不理想,但不确定任何其他唯一标识符。
    【解决方案2】:

    您也许可以使用 WMI 来获得结果。特此提供一个指向通过网络设备跟踪的 WMI 解决方案的链接。

    我在这里发布代码以防网站出现故障,但所有功劳归原作者 PsychoCoder 所有。 Use WMI to get MAC Address in C#

    还有代码:

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-27
      • 2014-10-28
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2020-10-27
      相关资源
      最近更新 更多