0

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

Community
  • 1
  • 1
rumit patel
  • 1,891
  • 5
  • 23
  • 28

2 Answers2

0

Just briefly just now. You seem to be mostly on the right lines.

1) That error is presumably the one with name 'InvalidArgument' and code 10022 (check SocketException.SocketErrorCode).

So for the MSFT Bluetooth stack it has meaning (See http://32feet.codeplex.com/wikipage?title=Errors):

"Plug and Play, driver-stack event, or other error caused failure." 

So that means that something is going wrong at the hardware level with the connection, either with the dongle itself or the pair of devices are mis-communicating. It's not a Parani module you are connecting to, is it?

Pair with it manually in the Bluetooth UI, then see if the connection works then.

2) Do you really want a COM port? I very much prefer working with Sockets and System.IO.Streams. COM ports are hard to set-up, very hard to maintain, and hard to use. Only if you have a third-party program that only uses COM ports should you need to use them.

BluetoothClient doesn't create a COM port, it uses Sockets and returns a Stream to read and write to.

alanjmcf
  • 3,430
  • 1
  • 17
  • 14
  • I have updated my original post for further information. Let me know if that helps. Also, regarding error code, I received two errors randomly: 10058: Socket was shutdown error (Could be due to invalid port) and 10022 in general. – rumit patel Mar 13 '13 at 17:39
  • I have found out that I could use WCL library. However, in that also, I do not see an option to specify a COM port. Let me know in case if you could suggest any help using WCL? – rumit patel Mar 13 '13 at 22:50
0

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

rumit patel
  • 1,891
  • 5
  • 23
  • 28