Search code examples
c#visual-studio-2010bluetooth32feet

32feet.net: How to connect to a specific COM port via BluetoothClient


I am trying to build a windows application with C#/Visual Studio 2010 which would do the following functions (in sequence): For bluetooth operations, I am using 32feet.Net library functions.

  1. Find the bluetooth devices and list them in the list/combo box for the user to select. (I want to allow user to select multiple devices and hence NOT going for SelectBluetoothDeviceDialog class option.). I can get the list of devices via BluetoothClient.DiscoverDevices(), however, it is not an async operation. If possible, I would like to go for the async operation. I read about the BluetoothComponent class with events DiscoverDevicesProgress and DiscoverDevicesComplete and method DiscoverDevicesAsync but could not get it working. If possible, kindly share a sample code for this.

  2. User selects the devices from the list and clicks 'Pair' button. So far I can successfully pair the devices via BluetoothSecurity.PairRequest. No issues here.

  3. User now selects one/multiple device(s) from the 'paired' list and clicks 'connect'. Here I tried to connect to the device using BeginConnect (for async operation) and Connected methods of the BluetoothClient class but getting following exception.

System.Net.Sockets.SocketsException: {"An invalid argument was supplied 000319002CC6:0000110100001000800000805f9b34fb"}.

The number in the above exception is GUID number required for the Connected method which I passed using BluetoothService.SerialPort. This will fail as my device is expecting to be connected at COM7 port. I am not sure how can I connect/pair a device at the specific COM port? Is is even possible by 32feet.Net library functions? If so, kindly provide a code sample.

References Note:I have already read and tried to implement the code explained in the article below on StackOverflow. However, due to my requirements (allowing user to pair and connect to multiple devices at COM ports) I am unable to run the same code. However, it did help understanding the concept. 32feet.net howto discover nearby bluetooth devices async in c#

Request you to advise the best way to handle this situation. In case I should try using any other library/functions other than 32feet.Net , do let me know.

My ultimate goal is to BOTH read and write data from and to the connected device(s). So, as of now, I am just trying to get connected on a specific COM port via bluetooth.

Regards, Rumit

=========================== EDIT: updated information for answer 1:

I have received a bluetooh device (a patch) which is supposed to be connected to TOSHIBA VIRTUAL BT COM port. I apologize if the TOSHIBA information was necessary to better answer the question. I am new to the communication with ports. So far I know that I need to use COM7. I have an application built in C++ which connects to the same patch on COM7 via bluetooh. However, I don't have the source code and I have been asked to implement the same utility in C#. From your reply, can I assume that the C++ application might be using Windows Sockets 2 by any chance?

Also, I could see an option to specify a port (integer value) value in BluetoothEndClient but that also did not work. So, I assume that the port was not COM and was some other type of port.

Regards, Rumit


Solution

  • I have found a solution to successfully connect to a bluetooth device using WCL library as described below.

    Step1: Make the wclClient's Transpport property to ctSerial.

    client.Transport = wclClientTransport.ctSerial; //This makes the wclClient to listen to the COM ports.
    

    Step2: Specify the COM port number by setting client.SerialParams.Port property. For Example,

    client.SerialParams.Port = 5; // For COM5
    

    Regards, Rumit