Active2 years, 5 months ago
It emulates a serial cable to provide a simple substitute for existing RS-232, including the familiar control signals. It is the basis for DUN, FAX, HSP and AVRCP. SPP maximum payload capacity is 128 bytes. Serial Port Profile defines how to set up virtual serial ports and connect two Bluetooth enabled devices.
My company developed a device that communicates with a PC via Bluetooth using a virtual COM port.
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).
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.
![Heroes season 4 episode 14](/uploads/1/2/4/9/124913032/503389388.jpg)
How can I do it in c#? (a solution independent of installed bluetooth stack is appreciated).
Thanks in advance.
Łukasz Bownik
Łukasz BownikŁukasz Bownik3,2141212 gold badges3737 silver badges6060 bronze badges
6 Answers
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.
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!
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:
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.
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.
![Bluetooth Bluetooth](/uploads/1/2/4/9/124913032/153171982.jpg)
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).
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..
Community♦
alanjmcfalanjmcf3,36411 gold badge1313 silver badges1313 bronze badges
First, create a Management Object Searcher to search the WMI database:
Next, use LINQ to get all the serial ports into a query:
You can now print all the COM ports, their friendly names and you can even filter through their PNPDeviceID's to find the bluetooth device address. Here's an example:
0014![Port Port](/uploads/1/2/4/9/124913032/947284123.jpg)
49322 gold badges99 silver badges2929 bronze badges
Matt WilliamsMatt Williams
I manage to get the bluetooth name and the COM port by fiddling the registry key
The pseudo code to obtain the bluetooth information is below:
- enumerate all the COM port available in the PNP
- obtain the device classGuid
- search the bluetooth address from the classGuid
- when the bluetooth address is known, the bluetooth name can be obtained from the this registry
SYSTEMCurrentControlSetServicesBTHPORTParametersDevices
The code is below, just call the GetBluetoothPort(), it will return a list of bluetooth devices, and you could connect them by passing the COM port number to the SerialPort class
TimTim2,33011 gold badge2323 silver badges4545 bronze badges
Maybe it is not what you are looking for, and maybe you already found your answer..
I just found a question not exactly like yours but worked for me. With this one you can find out which one of your COM Ports are from a Bluetooth device:StackOverflow - Determine if serial port is normal COM or SPP
I hope it helps somehow. If you find out how to do what you wanted, please let me know. Thanks.
Community♦
VicVic
So, to get the information about a remote device including its name, using 32feet.NET do:
If not using the library you'll have to P/Invoke Win32's BluetoothGetDeviceInfo.
alanjmcfalanjmcf3,36411 gold badge1313 silver badges1313 bronze badges
CJ_CJ_
Not the answer you're looking for? Browse other questions tagged c#serial-portbluetooth or ask your own question.
-->Definition
Represents an RFCOMM service ID.
ContractVersionAttributeDualApiPartitionAttributeMarshalingBehaviorAttributeStaticAttributeThreadingAttribute
Windows 10 requirements
Device family | Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox) |
API contract | Windows.Foundation.UniversalApiContract (introduced v1) |
Capabilities |
Properties
GenericFileTransferGenericFileTransferGenericFileTransferGenericFileTransferGenericFileTransfer | Creates a RfcommServiceId object corresponding to the service id for the standardized Generic File Transfer service (with short id 0x1202). |
ObexFileTransferObexFileTransferObexFileTransferObexFileTransferObexFileTransfer | Creates a RfcommServiceId object corresponding to the service id for the standardized OBEX File Transfer service (with short id 0x1106). |
ObexObjectPushObexObjectPushObexObjectPushObexObjectPushObexObjectPush | Creates a RfcommServiceId object corresponding to the service id for the standardized OBEX Object Push service (with short id 0x1105). |
PhoneBookAccessPcePhoneBookAccessPcePhoneBookAccessPcePhoneBookAccessPcePhoneBookAccessPce | Creates a RfcommServiceId object corresponding to the service id for the standardized Phone Book Access (PCE) service (with short id 0x112E). |
PhoneBookAccessPsePhoneBookAccessPsePhoneBookAccessPsePhoneBookAccessPsePhoneBookAccessPse | Creates a RfcommServiceId object corresponding to the service id for the standardized Phone Book Access (PSE) service (with short id 0x112F). Lost vikings pc game download. |
SerialPortSerialPortSerialPortSerialPortSerialPort | Creates a RfcommServiceId object corresponding to the service id for the standardized Serial Port service (with short id 0x1101). |
UuidUuidUuidUuidUuid | Retrieves the 128-bit service id. |
Methods
Windows Bluetooth Serial Terminal
AsShortId()AsShortId()AsShortId()AsShortId()AsShortId() | Converts the RfcommServiceId to a 32-bit service id if possible. |
AsString()AsString()AsString()AsString()AsString() | Converts the RfcommServiceId to a string. |
FromShortId(UInt32)FromShortId(UInt32)FromShortId(UInt32)FromShortId(UInt32)FromShortId(UInt32) | Creates a RfcommServiceId object from a 32-bit service id. |
FromUuid(Guid)FromUuid(Guid)FromUuid(Guid)FromUuid(Guid)FromUuid(Guid) | Creates a RfcommServiceId object from a 128-bit service id. |