Hi Mike,
Here's the issue i'm trying to solve.
I'm creating a tool to be used on our production floor. I have a legacy piece of code that uses a COM Port to talk a device. I'm currently using the WCL and the Microsoft BT stack. I'm using the wclAPI, wclBluetoothDiscover and wclVirtualCOMPort components.
I scan for a device using a TwclBluetoothDiscover. I place the address and the and the friendly name in a listbox similar to the demo apps.
I then have a connect button that creates a thread to do the following:
1. Closes the wclVirtualCOMPort Component, to destroy any old connections
2. Gets the pin from the friendly name
3. Pairs the device if necessary
4. Assigns the device address, radio, Service class
5. Opens the virtual port.
I then return the resulting COMPort number/handle to the legacy section of the code where i assign it to the 3rd party serial port component and flag the user that the connection has been established.
Here's a snippet of the code:
function TConnectThread.SetupBluetoothConnection( ): Integer;
var
Device :TwclBluetoothDevice;
radio :TwclBluetoothRadio;
Name: WideString;
pin: string;
isPaired : boolean;
j: Integer;
res : Integer;
begin
Result := -1;
radio := TwclBluetoothRadio.Create;
try
radio.API := baMicrosoft;
Device := TwclBluetoothDevice.Create;
try
if ( _activeAddress <> '') then
begin
_criticalSection.Enter;
try
_virtualPort.Close();
finally
_criticalSection.Leave;
end;
// DEBUG
//sleep(100);
Device.Address := _activeAddress;
// Reconmended in examples
for j := 0 to 2 do
begin
res := device.GetName(radio, Name);
if (Name <> '') or (Res <> WCL_E_SUCCESS) then Break;
end;
if ( pos( 'VR4U', Name) = 0 ) then
raise Exception.Create('Unable to get Device Name.');
pin := GetPinFromName( Name );
if ( device.GetPaired( radio, isPaired ) <> WCL_E_SUCCESS ) then
raise Exception.Create('Unabled to communicate with device');
{ TODO: might want to always unpair then re pair
if ( (isPaired) and ( device.Unpair( radio ) <> WCL_E_SUCCESS )) then
raise Exception.Create('Unable to unpair device.');
}
if (not isPaired) and (Device.Pair( radio, pin ) <> WCL_E_SUCCESS ) then
raise Exception.Create('Unable to pair device.');
_criticalSection.Enter;
try
_virtualPort.Address := _activeAddress;
_virtualPort.Radio := radio;
_virtualPort.Service := SerialPortServiceClass_UUID;
wclShowError(_virtualPort.Open);
finally
_criticalSection.Leave;
end;
end;
if not _virtualPort.Active then
begin
_exceptionMessage := 'COM Port was not created';
end
else
begin
Result := _virtualPort.Port
end;
finally
Device.Free;
end;
finally
radio.Free;
end;
end;
Ideally i'd like to create and destroy the virtual port each time a user tries to establish a new connection. And the above seemed to work okay on my development machine.
Given the parameters, Is this a decent way to go about this?
When i tried the application on the target machine, things kinda went haywire. It seems like the _virtualPort.Open(), returns immediately saying it has setup the port, but infact it hasn't.