在手动配对后获取特定蓝牙设备的传入ComPort

时间:2015-07-24 08:35:18

标签: c# powershell bluetooth

我有一个问题是为了在手动配对后检测蓝牙设备的实际ComPort。 首先,我必须提到在开发环境中我使用的是Windows 8.1。 我搜索了这个问题的互联网解决方案,我在PowerShell中找到了与此命令相关的C#代码:

 Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort"

但是,我的电脑上有3个连接,如下图所示: enter image description here

当我运行命令时,只有两个,我的设备“MiCon_ *”不可用:

enter image description here

它是Windows 8的问题?或者我做错了什么?

如果我检查PnP_Entity,它只给我一个名称,如“蓝牙串口(Com4)”:

enter image description here

如何使用友好名称检测COMPort?

1 个答案:

答案 0 :(得分:0)

HKLM \ HARDWARE \ DEVICEMAP \ SERIALCOMM包含所有有效com设备的列表。

由于您使用c#标记了此问题,因此这是一个列出它们的代码段。

List<string> keys = null;
string rootKeyName = null;
using (RegistryKey RootKey = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM", false))
{
    if (RootKey != null)
    {
        keys = new List<string>(RootKey.GetValueNames());
        rootKeyName = RootKey.Name;
    }
}
if (keys != null && !string.IsNullOrEmpty(rootKeyName))
{
    foreach (string drv in keys)
    {
        string port = Registry.GetValue(rootKeyName, drv, string.Empty) as string;
        if( port != null)
        {
            Console.WriteLine("Port: {0}, Device: {1}", port, drv);
        }
    }
}

产生如下输出:

Port: CNCA0, Device: \Device\com0com10
Port: CNCB0, Device: \Device\com0com20
Port: COM34, Device: \Device\VCP0

设备名称可以像这样转换(wmic样本,而不是c#代码,在命令提示符下输入)

wmic path Win32_PnPSignedDriver where "pdo like '%com0com%'" get devicename,pdo

输出:

DeviceName                      PDO
com0com - serial port emulator  \Device\com0com20
com0com - serial port emulator  \Device\com0com10

wmi查询来自:https://superuser.com/a/790650