none
cppwinrt for Bluetooth LE Device Notifications RRS feed

  • Question

  • Hello,

    I'm trying to talk to a Bluetooth LE device from a desktop non-managed C++ application using cppwinrt. I've managed to use the BluetoothLEAdvertisementWatcher to discover devices and access the values in the GATT services. However, now I'm trying to get notifications for value changes:

    characteristic.ValueChanged([&](GenericAttributeProfile::GattCharacteristic characteristic, GenericAttributeProfile::GattValueChangedEventArgs eventArgs) {
      std::wclog << L"Notification received!" << std::endl;
    });
    
    GenericAttributeProfile::GattCommunicationStatus status = co_await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GenericAttributeProfile::GattClientCharacteristicConfigurationDescriptorValue::Notify);
    if (status != GenericAttributeProfile::GattCommunicationStatus::Success) {
      std::wclog << L"Device had error " << int(status) << L" while trying to write to characteristic configuration" << std::endl;
      return;
    }
    

    The status I receive back is success, but I don't get any notifications (even though the device constantly changes the values). The notifications work fine from a desktop C# application with the same calls, but I need this in non-managed C++. Does anybody have an idea what's wrong? Maybe there's a problem with the lack of an event loop of some kind? I'm not sure on which thread this ValueChanged event is supposed to be fired on (my std::wclog is safe to be called from any thread there). Just reading the values with ReadValueAsync() works fine.

    Friday, April 21, 2017 4:01 AM

All replies

  • Hi anlumo,

    thanks for posting here.

    >>The notifications work fine from a desktop C# application with the same calls, but I need this in non-managed C++. Does anybody have an idea what's wrong? Maybe there's a problem with the lack of an event loop of some kind? I'm not sure on which thread this ValueChanged event is supposed to be fired on (my std::wclog is safe to be called from any thread there). Just reading the values with ReadValueAsync() works fine.

    How do you call these methods in your native c++ code?

    The WinRT GATT API related Async operation you used is based on Windows Runtime feature, used in UWP applications. In my opinion, it may need some support with .Net framework.

    If you want to use this, maybe you could use COM to call from a managed DLL with C++/CLI.

    Here are some documents for you.

    https://msdn.microsoft.com/en-us/library/68td296t.aspx

    https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx?f=255&MSPPError=-2147217396

    If you want to know more things about UWP, please post on UWP forum for better support.

    Hope this could be help of you.

    Best Regards,

    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Monday, April 24, 2017 8:59 AM
    Friday, April 21, 2017 9:04 AM