Author Topic: UI freezes after ble device unexpectedly disconnects  (Read 84 times)

Offline Iker

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
UI freezes after ble device unexpectedly disconnects
« on: July 12, 2019, 12:04:39 PM »
Hi all, I have been suffering from this since some time ago. Whenever the ble device disconnects unexpectedly (out of range) my VB.NET application UI freezes. This problem didn't arise until the version of Wcl that detect and trigger an event when the client disconnect.

This is the call stack of the event:

>   mscorlib.dll!System.Threading.Thread.Join() L

Online Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3389
  • Karma: 1000
    • Wireless Communication Libraries
Re: UI freezes after ble device unexpectedly disconnects
« Reply #1 on: July 12, 2019, 04:36:43 PM »
Hi,

Sorry for delay with the answer, we had to test it before answering the question.

This may apper only if you read characteristics/values form separate thread. That isnot recommended. You must do all the calls in the same thread.

Offline Iker

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: UI freezes after ble device unexpectedly disconnects
« Reply #2 on: July 12, 2019, 07:12:08 PM »
Hi Mike, thanks for quick answer.

In general, all calls to your framework are made from one single thread. But there is an exception, the connection to the BLE device is managed from a VB Timer (as far as I understand this means the UI thread), could this explain the issue?.

Regards,

Iker.

Online Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3389
  • Karma: 1000
    • Wireless Communication Libraries
Re: UI freezes after ble device unexpectedly disconnects
« Reply #3 on: July 12, 2019, 07:38:21 PM »
Should not. Can you please (if possible) prepare simple demo app that can reproduce the issue and send to us at support@btframework.com so we can reproduce the problem and fix it. Unfortunately (you probably know how it is) our code always (here is a big smile) works on our side.

Thank you.

Offline Iker

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: UI freezes after ble device unexpectedly disconnects
« Reply #4 on: July 22, 2019, 11:48:09 AM »
Let me add some adittional information, as I told the connection to the BLE device is managed from a Timer. In my code the connection procedure comprises this:

1. Call to Client.Connect()
2. Call to Client.ReadServices()
3. Call to Client.ReadCharacteristics()
4. All subsecuent call to your API are now managed from a dedicated thread.

All this calls (1 to 3) are made from the Timer. Modifying my code to move all this to the dedicated BLE thread will requiere some effort so I will be very gratefull if you can confirm that making all this calls from the UI is a probable cause of my problem.

Thanks.

Online Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3389
  • Karma: 1000
    • Wireless Communication Libraries
Re: UI freezes after ble device unexpectedly disconnects
« Reply #5 on: July 22, 2019, 01:29:49 PM »
You muist move the steps 2-4 to the OnConnect event handler. And yes, because all the operations (except connection) are synchronous it may block UI.

Offline Iker

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: UI freezes after ble device unexpectedly disconnects
« Reply #6 on: Today at 12:46:19 PM »
I have moved steps 2 and 3 to the OnConnect event but still get the same result, UI freezes.

To make it as clear as possible this is what I am doing:

1. From VB Timer: Call to Client.Connect()
2. From OnConnect event: Client.ReadServices()
3. From OnConnect event: Client.ReadCharacteristics()
4. From a dedicated thread all the rest of calls to your API to manage the communication with my device.

Because you said on your previous post I should move steps 2 to 4 to OnConnect I wonder if I should manage step 4 really from inside OnConnect event instead of creating a dedicated task.

Online Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3389
  • Karma: 1000
    • Wireless Communication Libraries
Re: UI freezes after ble device unexpectedly disconnects
« Reply #7 on: Today at 01:00:14 PM »
Hi,

OnConnect() event fires when connection operation completed. Connect is asynchronous operation, Once you called Connect() it starts connection procedure but does not really connect to device yet (when Connect() method returns). When connection operation completed OnConnect event fires with operation result. If the Error parameter is WCL_E_SUCCESS then connection was established with success. (It all described in the Bluetooth Framework documentation).

As I wrote in my previous answer the ReadServices and ReadCharacteristics are synchronouse operations. They do not return until operation completed. That means that it blocks all the execution until reading attributes completed. As you know Windows based on Windows messages and any Window must have message loop to process messages. Once execution is blocked by call to the function no one message can be processed by window. That is the basic of Windows programming.

About threading. All Bluetooth Framework classes must be used in the same thread that ceated the object. If you created GattClient in main thread you must execute all calls to its method in this main thread. If you need to use separate threads you must create the Gattclient object in that separate thread and call all its methods in that separate thread. There are few execptions (when you can create object in one thread and call its methods from other threads) but you must know eactly what you do and synchronize threads and calls to the object methods by your code. Because interthreading synchronization ca be comples it is better to just say that thread that created th eobject must use the object.

 

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