通过c#中的设备名称获取蓝牙设备的COM端口

我在C#中编写了一些代码来获取COM端口,其中映射了具有特定名称的蓝牙设备。 我尝试了几个与此处列出的解决方案类似的解决方案,但是没有一个解决方案我可以访问蓝牙设置窗口中的名称,如下图所示:

图片

如果它在那个窗口,我肯定有办法找到它。 有帮助吗?

非常感谢

请尝试以下代码段。 我用它在我每天使用的应用程序中按名称查找串行设备。

public static bool DoesSerialDeviceExist(string name) { using (var search = new ManagementObjectSearcher ("SELECT * FROM WIN32_SerialPort")) { string[] portnames = SerialPort.GetPortNames(); var ports = search.Get().Cast().ToList(); var tList = (from n in portnames join p in ports on n equals p["DeviceID"].ToString() select n + " - " + p["Caption"]).ToList(); string serial = tList.FirstOrDefault(o => o.Contains(name)); bool isAvailable = false; if (serial != null) { isAvailable = true; } return isAvailable; } } 
  private void FillInComportComboBox() { Hashtable PortNames = new Hashtable(); string[] ports = System.IO.Ports.SerialPort.GetPortNames(); //string st = ComPortList.Text; ComPortList.Items.Clear(); if (ports.Length == 0) { ComPortList.Items.Add("ERROR: No COM ports exist"); } else { PortNames = BuildPortNameHash(ports); //foreach (string s in ports) foreach(String s in PortNames.Keys) { //ComPortList.Items.Add(s + ": " + (string)PortNames[s]); ComPortList.Items.Add(PortNames[s] + ": " + s); } } } // Begins recursive registry enumeration // array of port names (ie COM1, COM2, etc) // a hashtable mapping Friendly names to non-friendly port values Hashtable BuildPortNameHash(string[] oPortsToMap) { Hashtable oReturnTable = new Hashtable(); MineRegistryForPortName("SYSTEM\\CurrentControlSet\\Enum", oReturnTable, oPortsToMap); return oReturnTable; } // Recursively enumerates registry subkeys starting with strStartKey looking for // "Device Parameters" subkey. If key is present, friendly port name is extracted. // the start key from which to begin the enumeration // hashtable that will get populated with // friendly-to-nonfriendly port names // array of port names (ie COM1, COM2, etc) void MineRegistryForPortName(string strStartKey, Hashtable oTargetMap, string[] oPortNamesToMatch) { if (oTargetMap.Count >= oPortNamesToMatch.Length) return; RegistryKey oCurrentKey = Registry.LocalMachine; try { oCurrentKey = oCurrentKey.OpenSubKey(strStartKey); string[] oSubKeyNames = oCurrentKey.GetSubKeyNames(); if (((IList)oSubKeyNames).Contains("Device Parameters") && strStartKey != "SYSTEM\\CurrentControlSet\\Enum") { object oPortNameValue = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + strStartKey + "\\Device Parameters", "PortName", null); if (oPortNameValue == null || ((IList)oPortNamesToMatch).Contains(oPortNameValue.ToString()) == false) return; object oFriendlyName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" + strStartKey, "FriendlyName", null); string strFriendlyName = "N/A"; if (oFriendlyName != null) strFriendlyName = oFriendlyName.ToString(); if (strFriendlyName.Contains(oPortNameValue.ToString()) == false) strFriendlyName = string.Format("{0} ({1})", strFriendlyName, oPortNameValue); oTargetMap[strFriendlyName] = oPortNameValue; //oTargetMap[oPortNameValue] = strFriendlyName; } else { foreach (string strSubKey in oSubKeyNames) MineRegistryForPortName(strStartKey + "\\" + strSubKey, oTargetMap, oPortNamesToMatch); } } catch { } }