question

HarikrishnanVR-4367 avatar image
0 Votes"
HarikrishnanVR-4367 asked HarikrishnanVR-4367 edited

Application hanged after the 'co_await BluetoothLEDevice::FromIdAsync' call.

I have an application which uses the BLE DLL. The BLE library will take care of the device scan, connection, etc.

I have tried to link the BLE DLL dynamically. The Bluetooth scanning is completed without any issue. When I tried to call the connection API from the BLE DLL. the issue occurs.

  fire_and_forget SensorDeviceInfo::ConnectToDevice(condition_variable& signal, bool& bConnected)
  {
    
      // Returns a BluetoothLEDevice object for the given Id and initiate a connection
      m_BluetoothLEDevice = co_await BluetoothLEDevice::FromIdAsync(GetId());
        
      if (m_BluetoothLEDevice == nullptr)
      {
             LogError(m_Log, __func__, "Failed to connect to device.");
      }
    
  if (m_BluetoothLEDevice != nullptr)
  {
  m_ConnectionStatusEventToken = m_BluetoothLEDevice.ConnectionStatusChanged({ this, &SensorDeviceInfo::ConnectionStatusChangeHandler });
     }
    
    signal.notify_one();
 }

Above is the code snippet. Since this is a asynchronous call, I have used condition variable to block the thread until the function completes. Because I need the call like synchronous.

             mutex mtxConnect;
             unique_lock lockConnect(mtxConnect);
             condition_variable cvConnect;
    
             // Connect to the sensor ble device
             m_CurrentDevice->ConnectToDevice(cvConnect, bConnected);
             cvConnect.wait(lockConnect);

While debugging the application, the call enter in the FromIdAsync place, but after some time it is getting hanged. The callstack disappeared..

I can see the mfc10.dll having some issue in Dependencies tool. Which have dependency with api-ms-win-core-wow64-l1-1-0.dll,
api-ms-win-core-wow64-l1-1-1.dll, api-ms-win-core-winrt-string-l1-1-0.dll

I am using VS2019 and latest redistributable files.

110855-mfc140-error.png


windows-uwpc++
mfc140-error.png (96.3 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There is an official BLE sample on GitHub: BluetoothLE. In scenario 2, the sample also uses FromIdAsync() method to get the device. But it also have a note that BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent. Could you please check if you had called this method in the UI thread?

Another thing is that, when you are using the official sample, will the issue happen as well?



0 Votes 0 ·

The BluetoothLE sample is working perfectly in my system. I think this is related to some dependencies with the C++/WinRT library.
My C++/WinRT library working smoothly with C++ console application as well.

I want this to be called from an MFC application.

0 Votes 0 ·
RoyLi-MSFT avatar image RoyLi-MSFT HarikrishnanVR-4367 ·

Could you confirm if you are calling this in the UI thread?

0 Votes 0 ·
Show more comments

0 Answers