Skip to content
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

Open
eraguzin opened this issue Jan 31, 2021 · 2 comments
Open

Add callback or method for user to detect device disconnections #306

eraguzin opened this issue Jan 31, 2021 · 2 comments

Comments

@eraguzin
Copy link

eraguzin commented Jan 31, 2021

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 at for device in self._connections.values() because RuntimeError: dictionary changed size during iteration, even though print(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 to BGAPIError: 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?

peplin added a commit that referenced this issue Feb 21, 2021
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.
peplin added a commit that referenced this issue Feb 21, 2021
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.
@peplin
Copy link
Owner

peplin commented Feb 21, 2021

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 adapter.stop() method (#23) if you'd like to give a full reset and re-connection another try.

@peplin peplin changed the title Is there a way to gracefully handle the BLE device disconnecting? Add callback or method for user to detect device disconnections Feb 21, 2021
@traversjames
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants