Setting up a Widcomm Virtual serial port.

Topics: Bluetooth - Widcomm/Broadcom
Apr 27, 2012 at 9:21 AM

Hi,

I'm relatively new to programming  but using 32feet I've managed to Discover, Pair  and Setup a virtal serial port in VB on the MS stack.

I need to be able to do the same on a widcom stack..

I've read the article

http://32feet.codeplex.com/wikipage?title=Bluetooth%20Serial%20Ports

but i'm not 100% sure how to correctly implement it  - does anyone have some working code I could use as a reference ?

Below is a subroutine I use after the device has been "discovered" and has been selected from a listbox - it works for MS Stack but for Widcomm the line

" device.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.SerialPort, True)"    is not working  - can anyone tell me what the equivelent for the Widcomm stack would be ?

Any help or working examples would be most gratefully received ...

 

regards

BG.

 

here is my code:

Dim cli As New InTheHand.Net.Sockets.BluetoothClient
Dim device As Sockets.BluetoothDeviceInfo = Me.ListBox1.SelectedItem
Dim addrr As BluetoothAddress = BluetoothAddress.Parse(device.DeviceAddress.ToString)
Dim state As Boolean
        state = True

       
Dim ep As New BluetoothEndPoint(addrr, InTheHand.Net.Bluetooth.BluetoothService.SerialPort)
Dim PerformanceCounter = 0
Dim pairsuccess = False
Dim pairAttempts = 0

TextBox2.Text = device.DeviceAddress.ToString
TextBox1.Text = Me.ListBox1.SelectedIndex

        MsgBox(addrr.ToString)
        While (Not pairsuccess) And (pairAttempts < 3)

            If (InTheHand.Net.Bluetooth.BluetoothSecurity.PairRequest(addrr, "0000")) Then
                pairsuccess = True
            Else
               pairsuccess = False
            End If
            MsgBox(pairAttempts)
            pairAttempts = pairAttempts + 1

        End While

        If (pairsuccess = False) Then
            MessageBox.Show("Unable to pair.")
            ' System.Windows.Forms.Application.Exit();
        ElseIf (pairsuccess = True) Then
            MessageBox.Show("Pair Sucessful")
            cli.Connect(ep)

            device.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.SerialPort, True)

            cli.Close()

            MessageBox.Show("connect sucessful")

        End If

 

 

 

 

May 2, 2012 at 10:49 AM
Edited May 2, 2012 at 10:49 AM

I am interested in getting help for this too but with C# code.

I have been able to test if the device is connected or not and (I beleive to be able to handle the pairing process) BUT I cannot figure out howto go about getting "physical" access to the device via SPP.

anyone able to post some code sample using c#?

this would be grateful, Note I've read some info found on this site about using WIDCOM and SPP but don't completely understand.

be great to be able to communicate with some via this forum or directly for help as I need it.

 

When i get it going I'd be more than happy to post a small howto via c# code.

chrs,

Dave.

Developer
May 2, 2012 at 10:53 AM

Hi Chaps

Sorry for the slow response.

Have either of you tried the WidcommSerialPort class?

From the docs : "On Widcomm, we recently added support for their CSppClient class. Currently this is exposed by method WidcommSerialPort.Create [...]" http://32feet.codeplex.com/wikipage?title=Bluetooth%20Serial%20Ports

Let me know if that works for you.

Alan

May 2, 2012 at 8:35 PM

Yes I had read this but am dammed if i can follow it ;)

Tried to use Rfcomm in my code but no luck.

I also tried a number of other things in the immediate window that was useless.

At work now so unfortunately cannot post any code.

essentially from the BrowserSPP VB code I'd deduced that you have a loop that runs over a bluetoothdevicesinfo object and within that loop I look for a particular text string (namely OPN2002 - name of the opticon bluetooth device I want to be able to download scanned barcodes from).

 

Once the devicename is found i then go in and check to see if it's connected/remembered (if it isn't then i pair it using part of the device's name - as mentioned in OPN2002 instructions).

It's now this part that I'm floored with, - How to obtain the SPP comm port so i can start to query the scanner for barcodes, Set date/time etc etc.

chrs,

Dadve.

Developer
May 3, 2012 at 12:25 PM

I assumed above that you each required an actual Virtual COM port on the PC.  Maybe that was incorrect, anyway I should have included my standard text: Virtual COM ports are difficult to set-up, difficult to use robustly and hard to maintain.  So my recommendation is to only use a virtual COM port if completely necessary, i.e. when there are two separate programs: one to set-up the connection and another one that only uses a COM port and can't be changed to use a socket.

See the text on the documentation front-page:

"1. Make a direct data connection
Where the program connects directly to a Bluetooth RFCOMM service, and sends and receives the raw data for that connection. See section General Bluetooth Data Connections below. The BluetoothClient provides the Stream to read and write on -- there is no need to use virtual COM ports and the System.IO.Ports.SerialPort class."

Reading your post it seems that you have one program that discovers, connects and reads/writes to a remote device. In that case do not use a virtual COM port, instead simply use a Socket-style connection.  So simply do something like: (copied from http://32feet.codeplex.com/wikipage?title=General Bluetooth Data Connections)

    // Discover
   BluetoothDeviceInfo selectedDev;
... ... ...
selectedDev = ...;
... ... ...
// Connect
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
// - or - etc
// serviceClass = MyConsts.MyServiceUuid
//
var ep = new BluetoothEndPoint(selectedDev.DeviceAddress, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write/Read ...

Hope that helps.

May 4, 2012 at 8:51 AM
Edited May 4, 2012 at 12:05 PM

Hi Alan  - thanks for your response..

yes - I had come accross the WidcommSerialPort.Create ... but I didn't know how to implement it  ( the correct syntax )

and I also thought that when I implemented that command I would see the Serial port created somewhere ... but I have figured it out in the end.

I have a third party hardware test  that needs to communicate with the device through a (virtual) com port  - as part of the test procedure I have to manually setup the V. com Port on each device before the hardware test to continue... thats why I would like to programatically Discover, Pair  and Setup a virtal serial port - automatically.

I'm nearly there  with this project ... but have come accross an issue that when new devices (to the test PC) are introduced ... when running my code the widcomm stack prompts me "Bluetooth Security code required".

In my code I supply the pair_request command with the pin ...  why does Widcomm as me for the Pin ? - can I surpress this prompt ? - I don't want the user to have to do any interaction.

EDIT:

just to add to this..

it is after executing the line 

wsp = inthehand.net.bluetooth.widcomm.widcommserialport.createclient( addr)

that I get this prompt from the widcomm stack to enter a pin .... how can I programatically enter this pin and remove the prompt ?

 

Also - if I don't enter a pin at that stage  - my application eventually crashes  - however if I run it again a second time .. I am not asked for a pin and the port is setup correclty ... any ideas of what is happening here ?

This always happen, ist time,  the pin is requested, then abort, 2nd time , no pin requested. !!

regards

 

 

 

 

 

 

Developer
May 9, 2012 at 8:01 PM

I think Widcomm's philosophy is that the user must always handle the pairing, they certainly have never provided and API for responding to pairing requests (on either platform).  So you call BluetoothSecurity.PairRequest first, and even then you get the prompt dialog?  Hmm. :-(

May 10, 2012 at 9:10 AM
Hi Alan,

I've found a working sequence ... but I'm not sure why it works ..

The sequence that does NOT work is :....

1. .... DiscoverDevicesInRange
2. .. Select the device I wish to pair
3... BluetoothSecurity.PairRequest(addrr, "0000")
4. WidcommSerialPort.CreateClient(addrr)

... it is after stage 4 that I get the prompt for a PIN from the Windows/ Widomc stack ( this only happens if the device has been "discovered" for the first time !!)

The working sequence is :

1. .. DiscoverDevicesInRange
2. .. Select the device I wish to pair
3... BluetoothSecurity.PairRequest(addrr, "0000")
4.... DiscoverDevicesInRange ( a 2nd time)
5... BluetoothSecurity.PairRequest(addrr, "0000")
6. .. WidcommSerialPort.CreateClient(addrr)


.. With this sequence I never get the prompt for the pin.... I'm not sure why it works ... anyone have any ideas why I have to discover the devices a 2nd time for it to work ??



Developer
May 10, 2012 at 5:00 PM

Wierd! Do you know if its maybe only the second Pair or the second discovery that matters?  And does PairRequest return true each time?

May 11, 2012 at 9:23 AM
Hi -

I don't have any more "totally" new devices that I can check this on ... but as far as I can remember it is the second discovery that matters - and yes, both PairRequest return true.



On Thu, May 10, 2012 at 5:00 PM, alanjmcf <notifications@codeplex.com> wrote:

From: alanjmcf

Wierd! Do you know if its maybe only the second Pair or the second discovery that matters? And does PairRequest return true each time?

Read the full discussion online.

To add a post to this discussion, reply to this email (32feet@discussions.codeplex.com)

To start a new discussion for this project, email 32feet@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Developer
May 15, 2012 at 4:33 PM

Thanks.  It is wierd.

I can't think that the second discovery would affect us, so it must be something in Widcomm.  I suppose it might be that the Service database (SDP) can't be accessed on the device until after the pairing and the second device discover allow Widcomm to read the remote SDP afresh.  Or something......

May 20, 2012 at 7:25 AM

Alan, bgengine,

 

Below is the c# code I've used thus far to try and create a SPP connection to a barcode scanner (namely a OPN2002 Bluetooth).

I'd apprechiate it if either of you (or someone else on this list that sees this posting could help me out.

I am at a loss to be honest.

regards,

Dave.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {          
            BluetoothClient bc = new BluetoothClient();
            BluetoothDeviceInfo[] devices = bc.DiscoverDevices();

            foreach(BluetoothDeviceInfo dev in devices)
            {
                Console.WriteLine(Convert.ToString(dev.DeviceName));
                Console.WriteLine(dev.Connected);

                if (dev.DeviceName.Contains("OPN2002") & dev.Remembered == false)
                {
                    string pin = dev.DeviceName.Substring(8, dev.DeviceName.Length - 8);  //OPN aways has something like... OPN2002-5467 << last 4 is the pin Nos

                    BluetoothWin32Authentication mem = new BluetoothWin32Authentication(dev.DeviceAddress, pin);
                    bc.SetPin(pin);
                }

                //device.SetServiceState(InTheHand.Net.Bluetooth.BluetoothService.SerialPort, True)

                                \\ As the scanner that i have *IS* Known I drop to this line a don't go any further at present.
                dev.SetServiceState(BluetoothService.SerialPort,true, true);
                Console.WriteLine(Convert.ToString(bc.GetRemoteMachineName(dev.DeviceAddress)));
                                  
            }
        }
    }
}