The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP][BLE]GattServicesChanged triggered unexpectedly after connecting to BLE device RRS feed

  • Question

  • I wrote C++/WinRT code that

    1) connects to a BLE peripheral using BluetoothLEDevice::FromBluetoothAddressAsync

    2) registers a callback to listen for GattServicesChanged event if it ever occurs

    3) read or write to the characteristics of the peripheral.

    The code works fine on Windows 10 build 1803 and 1809 as in after successfully connected to the device, the GattServicesChanged event is only triggered(though multiple times) when the services have actually changed.

    Since we upgraded to build 1903, the GattServicesChanged event is triggered several times after connecting to the device for no reason. If I ignore those events, the peripheral can still be read from/written to as normal. However, this breaks my logic because I need to know when the services have indeed changed. The object parameter of the event callback is always null so that doesn't give me any information about why the GattServicesChanged event is fired at a given time.

    Any reason why this event gets triggered after connecting to a device in 1903? I don't see the doc mentioning any behavior change so is this a regression?



    Friday, October 4, 2019 9:10 PM

All replies

  • Hello hanabanana,

    I test official sample Bluetooth Low Energy sample ( C# ) with TI SensorTag device. 

    For me there are two reasons of receiving GattServicesChanged event when connecting:

    1. Connect the device not paired. 
    2. Call GetGattServicesAsync method with BluetoothCacheMode.Uncached.

    Either I pair the device before connecting or use BluetoothCacheMode.Cached it will not receiving these GattServicesChanged event when connecting.

    I don't test C++/WinRT version of this sample.

    Please check if it helps.

    BTW, this forum is for "Discuss general issues about developing applications for Windows."

    For C++/WinRT you can ask a question at UWP forum for more professional support.

    Best regards,

    Rita


    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.

    Monday, October 7, 2019 2:52 AM
  • Hi Rita, 

    I've confirmed that FromBluetoothAddressAsync is not the problem. The GetGattServicesAsync however shows the following behavior on my Win10 build 1903,

    1. If device is paired, GetGattServicesAsync with Uncached mode does not trigger that event.

    2. If device is unpaired, GetGattServicesAsync with Uncached mode always triggers that event. Cached mode still triggers that event for 5 out of the 6 times I tried.

    Using Uncached mode on unpaired device was not triggering the event in previous builds, so why is this change introduced?

    Thanks.

    Monday, October 7, 2019 3:35 PM
  • Hello hanabanana,

    Since this issue is C++/WinRT related I'll move it to UWP forum for more professional support.

    Best regards,

    Rita


    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.


    Tuesday, October 8, 2019 7:10 AM
  • Hi hanabanana,

    It seems that the services are not cached if the device is not paired and does not support persistent caching. Can you please use this guidence to provide some traces for us?

    Best Regards,

    Fay


    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.

    Wednesday, October 9, 2019 9:39 AM
  • Hi Fay,

    Not sure what exactly you are looking for but I generated a BthTracing.etl file following the link you provided. How can I share it with you?

    Thanks.

    Wednesday, October 9, 2019 5:23 PM
  • Hi,

    Any platform which can download the file is OK(.e.g. OneDrive). Through these traces, I want to see if the services are cached when the device is not paired and does not support persistent caching.

    Best Regards,

    Fay


    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.

    Thursday, October 10, 2019 8:00 AM
  • You can find the file in below link,

    https://www.dropbox.com/s/h1qhwx03der461l/BthTracing.etl?dl=0

    Thanks.

    Thursday, October 10, 2019 7:14 PM
  • Hi,

    I have send the log to our engineers, they will check it as soon as possible. If there is any update, I will tell you.

    Best Regards,

    Fay


    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.

    Friday, October 11, 2019 9:12 AM
  • Hi,

    A quick preliminary look at the traces shows the services changed event being fired for each primary service during the discovery of the services (services are being added) to the cache.  This is by design, as the Services Changed event on the BluetoothLEDevice refers to the state of our local cache (i.e. the GattServices property on the device object), not the remote service changed indication directly.  Windows will automatically subscribe to the remote device’s service change CCCD though, and will remove impacted ranges from the local cache for both paired and unpaired devices, and rediscover the impacted range if the device is paired.  Performing a cached discovery of the services, or accessing the GattServices property (not preferred) will allow an application to see what attributes we have cached locally for the device and make an appropriate over the air discovery if needed.
     
    Note that if a device does not support Gatt robust caching and is not paired, when the device is disconnected, we will purge any attributes from the cache in accordance with the spec.  This will result in a services changed event for each service in the cache.  Also note that the service changed event is primarily to allow an application to synchronize access to their services.  The service objects will remain valid unless they were removed from the device, or a disconnect occurred that resulted in the cache being cleared.
     
    If for some reason an application needs to know when the actual device’s service changed value indication occurs, an app can set the event handler on the Gatt service change CCCD to get those events (writing the CCCD is disallowed, but Windows will always enable the CCCD when supported).

    Best Regards,

    Fay


    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.

    Wednesday, October 16, 2019 9:43 AM
  • Ok that explains the behavior but I still have questions.

    1. The doc page for GattServicesChanged is misleading. It says and I quote "This event fires when the remote device changes its services, or ...". That's why I thought it is actually coming from the device's service changed indication. If it only refers to the local cache, it shall clearly state so.

    2. I indeed want to know when the actual device's service changed indication occurs so I tried to listen for the ValueChanged event for the Service Changed characteristic on Generic Attribute(I think this is what you meant for setting the event handler on the service change CCCD, correct me if wrong). I can see that the CCCD is actually enabled upon connection, but the ValueChanged event is not fired when I change the services on remote device. Is this expected? How can I make it work?

    Thanks.

    Wednesday, October 16, 2019 10:21 PM
  • Hi,

    We have invited other engineers to participate in this case. There might be some time delay and appreciate your patience in advance.

    Best Regards,

    Fay


    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.

    Monday, October 21, 2019 9:35 AM
  • Hi, Is there any update yet? Thanks.
    Monday, October 28, 2019 2:53 PM
  • Hi,

    The device is not paired (so you get a service change on disconnect) and the device is not sending any service change over ATT in the logs provided. You should check with your device to make sure a service change is fired.

    Best Regards,

    Fay


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, November 1, 2019 8:23 AM
  • That log I provide only contains repro steps for the initial question I askesd. I don't believe I mentioned anywhere it contains information for my follow-up question.

    When I tried the ValueChanged event to detect remote device sending Service Changed indication, I am very certain the remote device did send ATT message as I checked it in Wireshark.

    My question now is: I'd like to detect when remote device actually sends service changed indication, your suggestion is to use ValueChanged on Service Changed characteristic, which I tried and did not work.

    Friday, November 1, 2019 3:11 PM
  • Hi,

    Can you provide traces for the latter, where you do not receive a notification for the service change indication?

    Best Regards,

    Fay


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Monday, November 4, 2019 7:40 AM
  • Here is the log. Thanks.

    https://www.dropbox.com/s/h1qhwx03der461l/BthTracing.etl?dl=0

    Monday, November 4, 2019 11:34 PM
  • Any update yet?
    Monday, November 11, 2019 3:07 PM
  • Hi,

    You are hitting an older issue that doesn’t exist anymore in newer builds. Please try again on the 19h1 or newer builds.

    Best Regards,

    Fay


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, November 15, 2019 9:04 AM