Author Topic: Windows 10 perhipheral disconnect long timeout  (Read 16842 times)

Offline mksiezopolski

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Windows 10 perhipheral disconnect long timeout
« on: May 24, 2022, 10:28:21 PM »
Hi,
We have the following problem while developing an app using BLE on Windows 10 and the Bluetooth Framework library. This problem occurs both with our app and the demo app Gatt Client. Whenever any notifications are turned on and the peripheral device gets turned off or resets unexpectedly, Windows 10 sees the disconnect almost immediately (couple of seconds due to the BLE connection timeout) - we see this in the Bluetooth Manager window of Windows 10 where the device changes state from Connected to Paired. However, the library disconnect handler only gets triggered after a long time - let's call this time the "library disconnect delay time".
What is more interesting I just ran a test where I turned each notification on one by one and measured the library disconnect delay time - for 1 notifications turned on it was 8s, for 2 notifications it was 16s and for 3 notifications it was 24s!!! So each notification that is turned on whilst connected to the device adds 8s to the library disconnect delay time (maybe the 8s is somehow connected to the connection parameters of the device and may be different for other devices).
If I turn off all notifications I get the disconnect immediately - at the same time that Windows Bluetooth Manager sees it.

This behavior does not occur if we trigger a disconnect from the Windows host - then the disconnect occurs immediately as well.

So my question is:
1. Why does this delay occur in the first place?
2. Why is it related to the number of notifications turned on?
3. How to get rid of it? Obviously we cannot turn off all notifications during connection since we want them for the system to work properly.

Regards,
Michał


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Windows 10 perhipheral disconnect long timeout
« Reply #1 on: May 25, 2022, 05:45:47 AM »
Hi,

To be able to reproduce and fix the issue it would be great to know which notifications you turned off/on.

Offline mksiezopolski

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Windows 10 perhipheral disconnect long timeout
« Reply #2 on: May 25, 2022, 09:43:38 AM »
Hey,
Not sure what you mean by "which notifications" but I will try to answer.

We turn on notifications for various characteristics - standard BLE battery service (0x180f) and battery characteristic (0x2a19) and 8 custom characteristics in our device (in 4 custom services). It does not matter whether we turn on notifications in our custom services or only the standard BLE battery service (custom services not used in this case) - the problem always occurs. All the characteristics are either Read/Notify or Read/Write/Notify - no indications are used.

Here is a snippet for the code turning on notifications:
Code: [Select]
BleError BleCharacteristic::setNotificationEnabled(bool suscribe) {

  if (!characteristic.IsNotifiable && !characteristic.IsIndicatable) {
    return BleError::CharacteristicNotNotifiable;
  }
  if (!cccdPresent) {
    return BleError::CharacteristicNoCccd;
  }
  qDebug() << "Start write descriptor for char" << getUuid() << suscribe;

  if (suscribe) {
    int result = gattClient.SubscribeForNotifications(characteristic, goReadFromDevice, plNone);
    if (result != WCL_E_SUCCESS) {
      auto errorString = BleUtils::convertWclErrorCodeToString(result);
      qWarning() << "Error writing CCCD for char" << getUuid() << errorString;
      return {BleError::CharacteristicSubscribeError, errorString};
    }
  } else {
    int result = gattClient.UnsubscribeFromNotifications(characteristic, goReadFromDevice, plNone);
    if (result != WCL_E_SUCCESS) {
      auto errorString = BleUtils::convertWclErrorCodeToString(result);
      qWarning() << "Error writing CCCD for char" << getUuid() << errorString;
      return {BleError::CharacteristicUnsubscribeError, errorString};
    }
  }
  return BleError::NoError;
}

We hook up the notifications using:
Code: [Select]
  __hook(&CwclGattClient::OnCharacteristicChanged, gattClient, &BleDevice::handleCharacteristicChanged);

Everything works perfectly during normal operation of the device: we can connect/disconnect, interact with the device, pair/unpair etc. The only major problem is the one that I described, that if the device turns off suddenly (or performs a reset which leads to a spurious disconnect) then the library waits a long time before calling the event:
Code: [Select]
  __hook(&CwclGattClient::OnDisconnect, gattClient, &BleDevice::handleDisconnected);
If we turn off the notifications then this problem does not occur and as I have mentioned the more characteristics we have subscriptions turned on, the longer this delay lasts...

Let me just mention that this problem does not occur in Android (using Android BLE library) in our custom Android application nor in the nRF connect Android application - the disconnect is caught within a few seconds (according to connection timeout parameter of the BLE device).
« Last Edit: May 25, 2022, 09:47:02 AM by mksiezopolski »

Offline mksiezopolski

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Windows 10 perhipheral disconnect long timeout
« Reply #3 on: May 25, 2022, 09:49:27 AM »
Also a collegue of mine sent you a video (to support) which maybe helps a bit to see what happens  ;). I just wanted to describe the problem more thoroughly here.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Windows 10 perhipheral disconnect long timeout
« Reply #4 on: May 25, 2022, 09:59:29 AM »
Got it. It may take time when disconnecting because when you call Disconnect() or when emote device disconnects its implementation unsubscribes from all subscribed characteristics. This operation includes 2 steps:

1. Call to Unsubscribe()
2. Write client configuration descriptor.

So in case your app calls Disconnect() it does not take too much time because device is available. However if device is not available writing client configuration descriptor takes time.

We will try to find better solution for this case. I will let you know once we find it (should not take too long).

Offline mksiezopolski

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Windows 10 perhipheral disconnect long timeout
« Reply #5 on: May 25, 2022, 10:28:17 AM »
OK. Thanks.
Just out of curiosity, does the Unsubscribe timeout occur in the library or is it an OS thing (trying to write to a disconnected device produces such a timeout)? Is the timeout related to the connection parameters of the peripheral device or is it hardcoded somewhere?


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Windows 10 perhipheral disconnect long timeout
« Reply #6 on: May 25, 2022, 10:43:15 AM »
Its OS timing. We will add flag to indicate that remote device disconnects so Bluetooth Framework will not try to unsubscribe.

Offline mksiezopolski

  • Newbie
  • *
  • Posts: 5
  • Karma: 0
Re: Windows 10 perhipheral disconnect long timeout
« Reply #7 on: June 01, 2022, 10:10:09 AM »
Hi,
Any progress on this issue or maybe some estimated time when we might expect an update?


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Windows 10 perhipheral disconnect long timeout
« Reply #8 on: June 01, 2022, 10:12:13 AM »
Hi,

If you use version with source code please send an e-mail to us at support@btframework.com and we will provide a latest beta version that possible fixes the issue.
Unfortunately if you use version without source code you have to wait for release. We are planing the release of new version in about a couple of weeks.

 

Sitemap 1 2 3 4 5 6 7