-
Notifications
You must be signed in to change notification settings - Fork 185
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
Add callback or method for user to detect device disconnections #306
Comments
This fixes a RuntimeError reported in #306. There's a circular dependency between the backend and device classes. When stopping the BGAPI backend, we call `disconnect` on each device. When a disconnection event is received from the BLED112 adapter, the device is removed from `self._connections` - the same dict we are iterating over.
This fixes a RuntimeError reported in #306. There's a circular dependency between the backend and device classes. When stopping the BGAPI backend, we call `disconnect` on each device. When a disconnection event is received from the BLED112 adapter, the device is removed from `self._connections` - the same dict we are iterating over.
No, you aren't missing anything. Today there isn't a way for the library user to be notified when a device disconnects. I'll re-title this ticket to be a feature request since I agree it's an obvious omission. I fixed the bug in the |
As a temporary workaround to this I have been periodically checking the RSSI of the device - a value of 0 seems to indicate a disconnect so can act as a proxy for connection status: rssi = device.get_rssi()
if rssi == 0:
try:
print("Connecting...")
device = adapter.connect(closestDevice, address_type=ADDRESS_TYPE)
print("Done...")
except pygatt.backends.bgapi.exceptions.ExpectedResponseTimeout:
pass
else:
print("RSSI: ", rssi) |
As far as I see, there is no callback or way of detecting if the BLE device disconnected, shut off, or went out of range. Even after I turn off the BLE device,
adapter._connections
still shows the device handle, so I don't see any way of polling to find out whether the device is currently connected or not.However, running
adapter.stop()
will fail atfor device in self._connections.values()
becauseRuntimeError: dictionary changed size during iteration
, even thoughprint(adapter._connections.values())
looks the same to me, no matter if the device was disconnected or not during the test run.Further, once this has happened, any attempt to exit the script and start again with
adapter.start()
will fail due toBGAPIError: Unable to detect BLED112 serial port: COM8.
unless I exit the entire Python console and start a new one.Is there a more graceful way of dealing with devices disconnecting? Am I missing something?
The text was updated successfully, but these errors were encountered: