-
Notifications
You must be signed in to change notification settings - Fork 298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated notifications when reconnecting #83
Comments
If you let the BleakClient go and create a new one for each reconnect, does it still show multiple notifications? If so it is a bug in bleak, and I need to find a way to handle it. |
Yes. In case of disconnection (is_connected() == False), the code is exiting the async with BleakClient(.. block and starting a new one, causing the duplicated notifications. I've checked out the disconnect method (which is being called when leaving the async with block). I've also checked the raw dbus messages with dbus-monitor and I can see the notifications coming duplicated in dbus. Looks like the RemoveMatch dbus message is not working as expected for the PropChanged rule. When comparing the addMatch params vs the removeMatch params sent in runtime and I can't see anything obviously wrong, but I'm not a dbus expert, so... Hope it helps. Thank you :D |
I have been looking a bit into this. The addMatch and delMatch routines work fine for the PropChanged rule. I have only very superficial understanding of DBus and BlueZ, so please challenge my assumptions. When StartNotify is called, it is not saved as a rule and therefore not cleaned up when disconnect is called. BlueZ therefore stores the subscription even after disconnection. If the connection is reestablished and another StartNotify is called for the characteristic, the values are sent twice. I tried to fix it by basically storing the UUIDs we subscribe to in a list and call StopNotify for all of them on disconnect. This works but doesn't feel very elegant... If there is a better way, please let me know. |
Description
I'm trying to maintain a connection to a BLE device permanently. In case of connection lost (caused by an out of range o standby situation), I am reconnecting and resubscribing for the same notifications callback.
To accomplish this, my code looks something like this (simplified):
This seems to work, but every time I use start_notify the notification_handler method is triggered one additional time. Just to clarify: First connection, notification_handler is called 1 time per notification; second connection, notification_handler is called twice; an so on.
I verified the code and seems the problem is that DBUS is sending the notifications duplicated N times because of the resubscriptions. The cause of this is that when it's connecting, the library subscribes to PropertiesChanged rule which is only removed in case of controlled disconnection. I also tried to call disconnect function in case of uncontrolled disconnection but an exception is raised in such case.
Not sure what to do here. I'm doing something wrong? It is a bug?
Thanks!
The text was updated successfully, but these errors were encountered: