Wireless Communication Library Support Forum

Legacy => Wireless Communication Library => Topic started by: madjoe on June 29, 2014, 11:06:55 PM

Title: Creating WCL objects in threads (C++ Builder XE5)
Post by: madjoe on June 29, 2014, 11:06:55 PM

I am using Embarcadero C++ Builder XE5 and the latest WCL for VCL.

When i create TwclAPI, TwclBluetoothDiscovery and TwclClient over the IDE in a Form, everything works fine.
I can connect to other devices and event handlers like OnConnect or OnData are called correctly.

Since it is common, that we are using threads for automated things, i want to use WCL in threads.
Therefore i created objects like this:

class BluetoothThread : public TThread
   TwclBluetoothRadio* __fastcall GetSelectedRadio();

   void __fastcall Execute();
   void __fastcall DoTerminate();

   TwclAPI *wclAPI1;
   TwclBluetoothDiscovery *wclBluetoothDiscovery1;
   TwclClient *wclClient1;

   void __fastcall wclClient1Connect(TObject *Sender, int Error);
   void __fastcall wclClient1Disconnect(TObject *Sender);
   __fastcall BluetoothThread();

   __property Terminated;

TwclBluetoothRadio* __fastcall BluetoothThread::GetSelectedRadio()
   TwclBluetoothRadio* Result = NULL;
   TwclBluetoothRadios* Radios = new TwclBluetoothRadios();
   if (!wclShowError(wclBluetoothDiscovery1->EnumRadios(Radios)) && Radios->Count > 0)
      Result = new TwclBluetoothRadio();

   return Result;

void __fastcall BluetoothThread::Execute()
   wclAPI1 = new TwclAPI(NULL);
   wclBluetoothDiscovery1 = new TwclBluetoothDiscovery(NULL);
   wclClient1 = new TwclClient(NULL);
   wclClient1->OnConnect = wclClient1Connect;
   wclClient1->OnDisconnect = wclClient1Disconnect;
   wclClient1->BluetoothParams->Authentication = false;
   wclClient1->BluetoothParams->Channel = 0;
   wclClient1->BluetoothParams->Encryption = false;
   wclClient1->BluetoothParams->Address = "(D4:94:A1:54:32:8F)";
   wclClient1->BluetoothParams->Radio = Radio;
   wclClient1->BluetoothParams->Service = SerialPortServiceClass_UUID;
   wclClient1->ConnectTimeout = 20000;
   wclClient1->Transport = trBluetooth;

   TwclBluetoothRadio* Radio = GetSelectedRadio();
   if (Radio)

My problem is, that when i try to connect the wclClient1, it stays in FState = scConnecting and never reaches the event handler OnConnect.

What could be wrong here?
Maybe i need to change the method creating WCL objects?

Thanks for any help
Title: Re: Creating WCL objects in threads (C++ Builder XE5)
Post by: Mike Petrichenko on June 30, 2014, 08:28:47 AM

As WCL is asynchronous I do not recomend to use it in threads (as it does not block main thread). But if you exactly need WCL in thread then:

1. TwclAPI must be always in main thread
2. Other WCL objects must be created in thread which uses it.
3. Thread with WCL object must have Windows Message Loop