Author Topic: BTF with Unreal Engine  (Read 786 times)

Offline Antidamage

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
BTF with Unreal Engine
« on: February 16, 2021, 05:20:30 AM »
Hello everyone

I've integrated the C++ version of the BT framework into Unreal Engine. One of the issues I've encountered is that if a device disconnects unexpectedly the library hangs the main game thread, as that's where it mostly runs from.

To try to resolve this I've moved the library to its own thread. However once this is done all callbacks stop working.

I've attempted to debug this to a fairly low level but I'm getting mired in the details of figuring it out. Beginning with, say, DeviceDiscovery, I'm seeing it send a PostMessage() indicating that discovery is started, but none of the message receiving loops inside the framework ever get the message.

I also notice that the framework branches off into a thread named ucrtbase.dll. Is this the main messaging loop thread? I've put breakpoints on all the instances of TranslateMessage() and they're only ever hit during startup.

Any advice on how to proceed?
« Last Edit: February 16, 2021, 05:24:42 AM by Antidamage »

Offline Antidamage

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: BTF with Unreal Engine
« Reply #1 on: February 16, 2021, 06:00:37 AM »
As a test I've added some toggles to run the framework back on the main thread. So much more stuff runs.

Of note, in wclMessaging.cpp a call to ProcessMessages()) on line 221 is hit, followed by the next breakpoint in wclBluetooth.cpp at line 1137 DoDiscoveringStart().

With the process in a separate thread this never takes place. Specifically _WndProc() is never called, although I have verified that _WndProc()'s window handle is still created/registered.

I've verified that calls made via the framework are taking place on its new thread (via a TQueue and an FRunnable in UE4) and are being correctly consumed.

Only other thing that stood out for me is the call stack when it's not threaded:



Is it possible that UE needs to "pump" the framework in order for its messaging thread to continue to run? I'm unsure why it's present in the callstack at all.

What is stopping the framework from receiving messages when it's not on the main game thread?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3632
  • Karma: 1000
    • Wireless Communication Libraries
Re: BTF with Unreal Engine
« Reply #2 on: February 16, 2021, 06:18:53 AM »
Hello,

As your separate thread has no message ;loop it will never process any messages. If you run it in separate thread the thread must have message loop. Or use APC sync (https://forum.btframework.com/index.php?topic=3520.0). Also take a look on IoT demo applications and on Console demo application.

About freezing on disconnect it would be see your code to understand what can be wrong there. It would be great if you can prepare simple test app that reproduce the issue and send it to us at support@btframework.com so we can reproduce and fix it.

Offline Antidamage

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: BTF with Unreal Engine
« Reply #3 on: February 16, 2021, 07:13:04 AM »
That's exactly right! I just discovered this myself. I had to add this to my thread loop:

Code: [Select]
MSG Msg;
while (!StopThread) {
PeekMessageA(&Msg, NULL, 0, 0, 0);
TranslateMessage(&Msg);
DispatchMessageA(&Msg);
BluetoothSubsystem->ProcessBluetoothQueue();
}

...and after that it simply worked.

I will send through something that can reproduce the hang if I get some time. :)

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3632
  • Karma: 1000
    • Wireless Communication Libraries
Re: BTF with Unreal Engine
« Reply #4 on: February 16, 2021, 07:56:56 AM »
Actually there is wclThread class (https://docs.btframework.com/bluetooth/c++/classwcl_common_1_1_cwcl_thread.html) that provides such functional and designed specially for such things.

Offline Antidamage

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: BTF with Unreal Engine
« Reply #5 on: February 16, 2021, 09:10:21 AM »
Aah, that's handy. Cheers!

 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13