Author Topic: wclThread c++ example  (Read 4068 times)

Offline MFC

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
wclThread c++ example
« 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

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: wclThread c++ example
« Reply #1 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.

Offline MFC

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Re: wclThread c++ example
« Reply #2 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



Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: wclThread c++ example
« Reply #3 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.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: wclThread c++ example
« Reply #4 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

 

Sitemap 1 2 3 4 5 6 7