dotnet-bluetooth-le / dotnet-bluetooth-le

Bluetooth LE plugin for Xamarin/MAUI, supporting Android, iOS, Mac, Windows
Apache License 2.0
853 stars 316 forks source link

stuck on await StopUpdatesAsync, and sometimes also on await disconnectFromDevice #238

Open shayo opened 7 years ago

shayo commented 7 years ago
  1. Connect to device.

  2. Get some characteristics. Register for StartUpdatesAsync

  3. Call await StopUpdatesAsync or await disconnectFromDevice

Expected behavior

No more updated (or disconnection)

Actual behavior

App get stuck on await StopUpdatesAsync or on await disconnectFromDevice. Right now I ended up calling those without the await, but this is not optimal.

Crashlog

no crash log.

Configuration

**Version of the Plugin: 1.3.0

**Platform:. iOS 10.3.2

**Device:(iPhone 5c)

smsissuechecker commented 7 years ago

Hi @shayo,

I'm the friendly issue checker. It seems like (50.00 %) you haven't used our issue template :cry: I think it is very frustrating for the repository owners, if you ignore them.

If you think it's fine to make an exception, just ignore this message. But if you think it was a mistake to delete the template, please close the issue and create a new one.

Thanks!

JSchaenzle commented 7 years ago

I've been using the 1.3.0 branch as well and I ran into a similar issue on iOS. For me, I only saw it hang when calling StopUpdatesAsync and I think it only happened when I called it after the peripheral device had already disconnected from the phone. I added a conditional around my call to StopUpdatesAsync that checked to see if the device was still connected before making the call and I haven't had the issue since. I think the issue is that the task relies on a callback from the OS to complete and in some cases, such as when the device is disconnected, the callback never comes. I thought the OS was supposed to throw an exception in that case but I wasn't seeing that on iOS (Not sure about Android).

JSchaenzle commented 7 years ago

I just ran into this issue again. I can reproduce it pretty easily by turning bluetooth off on iOS while my app is connected to a device. I have confirmed that iOS does not throw an exception when SetNotifyValue is called in that case. It just silently does nothing and this method deadlocks because the OS never produces the callback. Also, I noticed that after turning bluetooth back on, when the app attempts to reconnect and then calls SetNotifyValue to re-enable notifications, if I inspect the _nativeCharacteristic the IsNotifying property is already set to true. It seems like the characteristic that is being cached in the service becomes invalid when bluetooth is switched off during a connection.