Author Topic: UI hangs when calling Pair() even in worker thread  (Read 3022 times)

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
UI hangs when calling Pair() even in worker thread
« on: October 09, 2008, 03:38:31 PM »
Hello,

I'm using the Bluetooth Framework X v5.2.2 in a C# application with a Windows Presentation Foundation UI with VS 2008.

When I call Device.Pair() the calling thread hangs for some seconds, so I'm calling it in an extra thread to avoid that the UI hangs.
But even when I call it in this worker thread, the UI does not respond.
Does the Device.Pair() function always run in the main UI thread?
I tested the thread with a sleep command (10 seconds) instead of calling Pair(), the UI doesn't hang then.
What can I do to solve this problem?

I get the same behavior when I create a COM port to the paired device, but this method hangs only a short moment.


Thanks for the help

Roman

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #1 on: October 09, 2008, 04:00:28 PM »
Good day!

Tha pair is synchroniouse operation and it is not recomended to use it in thread. The better way (if you need asynchronious pairing) is to use BFAuthenticator. The component will handle all authentication requests (if authentication required) and allow you to manage the pin code.

If you still need to call the Pair method in thread I do recomend to switch to WCL then. With WCL you should create wclBluetoothDevice object in thread and then call its pair method (in the same thread, different than main thread).
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #2 on: October 10, 2008, 11:05:08 AM »
How do I use the BFAuthenticator class? There is very little documentation.

I replaced the Device.Pair() call with the following code:

//------------------------------------------------------------------------------
using BluetoothFrameworkX;

private string m_sBluetoothPairingPIN = null;

private void CalledByThread_TryToConnect(string MACAddress, string BluetoothPairingPIN)
{
    m_sBluetoothPairingPIN = BluetoothPairingPIN;
   
    BFAuthenticatorXClass bfAuthenticator = new BFAuthenticatorXClass();
    bfAuthenticator.OnPINRequest += new IBFAuthenticatorXEvents_OnPINRequestEventHandler(BFAuthenticatorX_OnPINRequest);
    bfAuthenticator.Open();
}

public void BFAuthenticatorX_OnPINRequest(string AAddress, ref string PIN)
{
    PIN = m_sBluetoothPairingPIN;
}
//------------------------------------------------------------------------------


Is this the correct proceeding?

How do I have to tell the BFAuthenticator the device that I want to connect to (MAC address)?
With Pair() I have a BFBluetoothDeviceX object that knows the MAC address.

What exceptions do I have to catch to know if paring was successful or not?

Is it correct that BFAuthenticator only works with the Microsoft stack?
In Hussain's documentation I read that it calls the Pair() method internally for other stacks.
Is this true or do I have to check the used stack manually and then call Pair() for non-MS stacks?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #3 on: October 11, 2008, 07:09:24 PM »
Srry for delayed answer - trying to find good solution for you.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #4 on: October 14, 2008, 11:57:48 AM »
Thanks for your help, I'm waiting for your answer...

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #5 on: October 16, 2008, 05:09:54 PM »
Can you please answer me my questions until the beginning of next week?
Otherwise it will be too late for my current project.
Thank you

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #6 on: October 16, 2008, 05:27:00 PM »
BFAuthenticator should be used on main form with main thread only. Applicatin must have only one instance of the component. You should write OnPinRequest event handler. The event handler gets 2 parameters: the device address and the PIN (you should fill it with the PIN). The PIN will send to the device then.

BFAuthenticator works with MS and BS stacks only.

The pair function can be used in thread, but you shoukd think about synchronization (it is impossible to perform discovery and pairing or connecting and pairing, or more than 1 pairing at the same time).
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #7 on: October 22, 2008, 10:46:36 AM »
Thank you for your answer.

How can I tell the BFAuthenticator the device MAC address to connect to?

Until now I'm doing this with:

BFBluetoothDeviceX Device;
Device = bfBluetoothDevices.get_Device(0);

Device.Address = sAddress;


How can I set the MAC address when I am using BFAuthenticator?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #8 on: October 22, 2008, 11:09:02 AM »
BFAuthenticator does not need MAC address. It handles all PIN requests and pass the MAC address of the device which wants to pair to the OnPINRequest event.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #9 on: October 22, 2008, 11:46:23 AM »
I want to start the paring from the PC to the device. The device has no functionality to start a pairing.

Is this not possibile with BFAuthenticator?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #10 on: October 22, 2008, 12:27:46 PM »
It is not possible with BFAuthenticator. Use Pair method then with synchronization (if you want to use it in threads). It is impossible to run 2 or more Pair operations at the same time. Only 1 can be active.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline DaOne

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: UI hangs when calling Pair() even in worker thread
« Reply #11 on: October 22, 2008, 02:33:16 PM »
I already use synchronization and I only call 1 Pair operation at the same time.

I call the Pair method in an extra worker thread beside the GUI thread. It calls an event when paring is finished or failed to tell the GUI the result.

My problem is that the GUI hangs when calling the Pair method, so it seems that Pair runs in the main thread even when called in another thread.
(I tested the worker thread with a Sleep-call instead of Pair, then the GUI does not hang)


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Re: UI hangs when calling Pair() even in worker thread
« Reply #12 on: October 22, 2008, 03:26:38 PM »
That is because the BFBluetoothDevice object was created in main thread and as soon as OCX used single threaded model the COM subsystem synchronize all calls with main thread.

To solve that and create BFBluetoothDevice object in separate thread it is recomended to use WCL instead BTF.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager