Wireless Communication Library Support Forum

Frameworks => Bluetooth Framework => Topic started by: MFC on November 10, 2021, 04:58:25 PM

Title: wclThread c++ example
Post by: MFC on November 10, 2021, 04:58:25 PM
Where can I find an example on how to use wclThread (I'm using the C++ Edition)? (I would like to use the functions of CwclGattClient inside a thread)

Thanks
Marco
Title: Re: wclThread c++ example
Post by: Mike Petrichenko on November 10, 2021, 05:45:57 PM
Hello,

Unfortunately there is no example however usage the class is very simple.

Create descendant class and override 2 methods: OnInitialize() and OnTerminate(). Inside OnInitialize() create GattClient and start connection. Inside OnTerminate() call disconnect.

From the main thread create your class and call Run() to start thread and Terminate() to stop thread.
Title: Re: wclThread c++ example
Post by: MFC on November 15, 2021, 04:22:26 PM
Dear Mike,
I implemented the functions as you suggested, but I have a problem: the following is the implementation I used for the OnInitialize() function

bool CBLEthread::OnInitialize()
{
    wclBluetoothManager.Open();
    FCharacteristics.clear();
    FDescriptors.clear();
    FServices.clear();
    CwclBluetoothRadio* radio = GetRadio();
    wclGattClient.SetAddress(0xcf1a325d9c9d);
    wclGattClient.SetConnectOnRead(false);
    int Res = wclGattClient.Connect(radio);
    wclClientState a;
    do
    {
        Sleep(500);
        a = wclGattClient.GetState();
        TRACE("GetState 0x%x\n", a);
       
    }  while(a != csConnected);

return true;
}

The function GetState, in the wait loop, returned the value 2=csConnecting but never the value 3=csConnected.
If instead I eliminate the wait loop, the connection is established but I don't know when the connection takes place.

I tried to use the hook to the function OnConnect
 __hook(&CwclGattClient::OnConnect, &wclGattClient, &CBLEthread::wclBluetoothManagerOnConnect);

but the behavior is the same: the function OnConnect is called only when the wait loop ends.

how can i fix this problem?
Thanks
Marco


Title: Re: wclThread c++ example
Post by: Mike Petrichenko on November 15, 2021, 04:28:54 PM
All Bluetooth Framework events always fires in the same thread that called Open()/Connect(). So your waiting loop blocks the execution and state will never change.
Title: Re: wclThread c++ example
Post by: Mike Petrichenko on December 03, 2021, 08:57:56 PM
We have update one of our demo code to show how to use wclThread class. This is very simple demo app but it gives some ideas how to use the class.

https://github.com/btframework/ConcurrentBleConnections