如何在 C# 中找出蓝牙设备的 COM 端口号? [英] How can I find out a COM port number of a bluetooth device in c#?

查看:30
本文介绍了如何在 C# 中找出蓝牙设备的 COM 端口号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司开发了一种设备,该设备使用虚拟 COM 端口通过蓝牙与 PC 通信.

My company developed a device that communicates with a PC via Bluetooth using a virtual COM port.

现在我们需要用户先将设备与 PC(MS Windows 操作系统)配对,然后手动将它的 com 端口号输入到我们的应用程序中(我打赌 95% 的用户会失败).

Now we need a user to pair a device with a PC (MS Windows OS) first and then enter it's com port number manually into our application(I bet 95% of users will fail on this taks).

所以我希望我的应用程序向用户显示配对的蓝牙设备列表(他们的友好名称"列表),然后我想自动找出所选设备的 COM 端口号.

So I'd like my application to present a user with a list of paired bluetooth devices (a list of their "friendly names") and after that I'd like to find out the selecded device's COM port number automatically.

我怎样才能在 C# 中做到这一点?(独立于已安装蓝牙堆栈的解决方案值得赞赏).

How can I do it in c#? (a solution independent of installed bluetooth stack is appreciated).

提前致谢.

推荐答案

Widcomm bluetooth : 如何打开虚拟COM 为我理解的许可证:使用二进制版本可免费用于商业用途.而且,我也是图书馆的维护者.

See my answer at Widcomm bluetooth : how to open the virtual COM for my understanding of the licence: using the binary version is free for commercial use. And, also that I'm maintainer of the library.

那么简短的题外话.我不是虚拟 COM 端口的忠实粉丝.使用直接的套接字"连接似乎总是容易得多,而不是尝试设置 COM 端口,并尝试找到它创建的名称(见下文!),然后必须打开一个 SerialPort 才能使用它,然后,如果连接丢失,人们不知道并且只需要继续重试...有了库,创建和使用直接蓝牙连接就容易多了!

So a brief slight digression. I'm not a big fan of virtual COM ports. It always seems much easier to use a direct 'sockets' connection, rather than attempt to setup a COM port, and try to find what name it was created as (see below!), and then have to open a SerialPort to use it, and then if the connection is lost one doesn't know and have simply to keep retrying... With the library its so much easier to just to create and use that direct Bluetooth connection!

但是,您目前可能需要解决当前任务的方法.:-) 所以,使用 WMI 找到当前的 COM 端口,看看它们中是否有任何适用于您的设备.例如在 PowerShell 中:

However you may want a solution to your current task at the moment. :-) So, use WMI to find the current COM ports in place and see if any of them are for your device. For example in PowerShell:

C:> Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort"
...
...
DeviceID         : COM66
PNPDeviceID      : BTHENUM{00001101-0000-1000-8000-00805F9B34FB}7&1D80ECD3&0&00803A686519_C00000003

在那个长长的字符串中可以看到目标设备的地址:00803A686519.可以使用 .NET 中的 WMI,运行该查询,使用BTHENUM"过滤那些,然后解析出地址.

In that big long string one sees the address of the target device: 00803A686519. One can use WMI from .NET, run that query, filter the ones with "BTHENUM", and then parse out the address.

如果您确实需要创建一个新的蓝牙虚拟 COM 端口,请使用 32feet.NET 的 BluetoothDeviceInfo.SetServiceState(BluetoothService.SerialPort) API.请参阅用户指南中的蓝牙串行端口"部分,例如在 http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html,以及发布中的类文档.

If you the do need to create a new Bluetooth virtual COM port, use 32feet.NET's BluetoothDeviceInfo.SetServiceState(BluetoothService.SerialPort) API. See the "Bluetooth Serial Ports" section in the User Guide e.g. at http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html, and the class documentation in the release.

不幸的是,我们调用的原生 Win32 API 并没有说明它创建的 COM 端口的名称!:-( 因此在调用前后运行 WMI 查询以查看出现的新名称(或使用 System.IO.Ports.SerialPort.GetPortNames 更简单).

Unfortunately the native Win32 API we call does not tell what name of COM port it created! :-( So run the WMI query before and after the call to see what new name appeared (or use System.IO.Ports.SerialPort.GetPortNames as its simpler).

这完全是针对 Microsoft 蓝牙堆栈的.我还没有调查其他堆栈在这方面的表现.简单检查后,Widcomm 的串口出现在 SerialPort.GetPortNames 但不在 WMI 查询中...

That's all specific to the Microsoft Bluetooth stack. I haven't investigated how other stacks behave in this regard. After a brief check Widcomm's serial ports appear in SerialPort.GetPortNames but not in the WMI query...

这篇关于如何在 C# 中找出蓝牙设备的 COM 端口号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆