我怎样才能找到在C#中的蓝牙设备的COM端口号? [英] How can I find out a COM port number of a bluetooth device in c#?

查看:1446
本文介绍了我怎样才能找到在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%的用户将在此TAKS失败)。

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).

所以我想我的应用程序present与配对的蓝牙设备(它们的友好名称列表)的列表中的用户之后,我想找出selecded设备的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#? (解决方案的独立安装的蓝牙堆栈的是AP preciated)。

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

先谢谢了。

推荐答案

请参阅我的答案在<一个href=\"http://stackoverflow.com/questions/2004730/widcomm-bluetooth-how-to-open-the-virtual-com\">http://stackoverflow.com/questions/2004730/widcomm-bluetooth-how-to-open-the-virtual-com我许可的理解:使用二进制版本是免费用于商业用途。而且,也是我的库维护者。

See my answer at http://stackoverflow.com/questions/2004730/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。我们可以使用WMI从.NET,运行该查询,筛选具有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。请参阅用户手册中的蓝牙串行端口一节如在<一个href=\"http://www.alanjmcf.me.uk/comms/bluetooth/32feet.NET%20--%20User%20Guide.html\">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).

这是所有具体到微软蓝牙堆栈。我没有调查其他堆在这方面的行为方式。一个简短的检查后的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天全站免登陆