所以,我找到了解决方案。我从这些人那里抢了图书馆:
http://32feet.codeplex.com/
使用该库,添加了以下两行:
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
这给了我设备“DeviceName”(我想要的名称)和“DeviceAddress”(基本上是设备 ID 的一部分)。
然后我使用“ManagementObjectSearcher”查询系统,它给了我一个 COM 端口和设备 ID 列表(System.Management 命名空间)。
ConnectionOptions options = ProcessConnection.ProcessConnectionOptions();
ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity");
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);
...etc as I looped over the results, pulled out the COM ports, and so on
我将“ManagementObject”值中的设备 ID 映射回“设备”列表,合并结果,最后得到具有名称、设备 ID、指示它是否是蓝牙设备的标志以及蓝牙设备中的“人类可读”名称(如果存在)。
很痛苦,但效果还不错。它很慢(client.DiscoverDevices() 需要一段时间),但在我的情况下这是可以生存的。