Restructuring to fix race condition problem
This commit is a pythonnet implementation of hbldh/BleakUWPBridge#1.
@@ -449,6 +449,7 @@ class BleakClientDotNet(BaseBleakClient):
(int) The GattCommunicationStatus of the operation.
"""
+
if (
characteristic_obj.CharacteristicProperties
& GattCharacteristicProperties.Indicate
@@ -462,22 +463,6 @@ class BleakClientDotNet(BaseBleakClient):
else:
cccd = getattr(GattClientCharacteristicConfigurationDescriptorValue, "None")
- # status = await IAsyncOperationAwaitable(
- # characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(cccd),
- # return_type=GattCommunicationStatus, loop=self.loop
- # )
-
- status = await wrap_IAsyncOperation(
- IAsyncOperation[GattCommunicationStatus](
- characteristic_obj.WriteClientCharacteristicConfigurationDescriptorAsync(
- cccd
- )
- ),
- return_type=GattCommunicationStatus,
- loop=self.loop,
- )
- if status == GattCommunicationStatus.Success:
- # Server has been informed of clients interest.
try:
# TODO: Enable adding multiple handlers!
self._callbacks[characteristic_obj.Uuid.ToString()] = TypedEventHandler[
@@ -488,8 +473,29 @@ class BleakClientDotNet(BaseBleakClient):
self._callbacks[characteristic_obj.Uuid.ToString()],
)
except Exception as e:
+ logger.debug("Start Notify problem: {0}".format(e))
+ if characteristic_obj.Uuid.ToString() in self._callbacks:
+ callback = self._callbacks.pop(characteristic_obj.Uuid.ToString())
+ self._bridge.RemoveValueChangedCallback(characteristic_obj, callback)
+
+ return GattCommunicationStatus.AccessDenied
+
+ status = await wrap_IAsyncOperation(
+ IAsyncOperation[GattCommunicationStatus](
+ characteristic_obj.WriteClientCharacteristicConfigurationDescriptorAsync(
+ cccd
+ )
+ ),
+ return_type=GattCommunicationStatus,
+ loop=self.loop,
+ )
+
+ if status != GattCommunicationStatus.Success:
# This usually happens when a device reports that it support indicate, but it actually doesn't.
- # TODO: Do not use Indicate? Return with Notify?
+ if characteristic_obj.Uuid.ToString() in self._callbacks:
+ callback = self._callbacks.pop(characteristic_obj.Uuid.ToString())
+ self._bridge.RemoveValueChangedCallback(characteristic_obj, callback)
+
return GattCommunicationStatus.AccessDenied
return status
