-
Notifications
You must be signed in to change notification settings - Fork 301
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
"bleak.exc.BleakError: This service is already present in this BleakGATTServiceCollection!" when reconnecting a device #376
Comments
Do you have two services with the same uuid on the peripheral? In that case this is related to #362. |
No I have ran only one service with |
Can you give a full working code example to reproduce the problem? |
I'm seeing something perhaps related, when I've got a device that drops connectivity (powered off) and then try to reconnect programmatically. If you think this is a different issue, more than willing to split it off. I'm still diagnosing and trying to discover "why". No minimal, reproducible case yet. bluetoothctl: 5.50 Even if I try an explicit disconnect, the problem still seems to be present. In the disconnect callback if not client.willful_disconnect:
asyncio.get_event_loop().create_task(self._reconnect()) async def _reconnect(self):
"""
Will try immediately, then 1, 2, 3, ..., 10, 10, ... seconds later
"""
await self.disconnect()
class_name = type(self).__name__
if self._logging_reconnect:
logger.info(
f"Will try reconnecting to {class_name} at {self.address} "
f"after waiting {self._reconnect_delay} seconds.")
await asyncio.sleep(self._reconnect_delay)
await self.connect()
if self.is_connected:
self._reconnect_delay = 0
self._logging_reconnect = True
else:
# code to try again in a bit
The wrapper tries to manage clean-up atexit, marginally successfully (mainly around if there is a running loop and things complete before Python shuts it down). async def connect(self, **kwargs) -> bool:
atexit.register(self.sync_disconnect)
retval = await super(BleakClientWrapped, self).connect(**kwargs)
if retval:
self._willful_disconnect = False
return retval
async def disconnect(self) -> bool:
self._willful_disconnect = True
retval = await super(BleakClientWrapped, self).disconnect()
self._willful_disconnect = False
if retval:
logger.debug("Unregistering atexit disconnect "
f"{self.name} at {self.address}")
atexit.unregister(self.sync_disconnect)
return retval On powering back on the Skale, it does show up in
Without deep knowledge of what is supposed to be happening, it is my guess that a previous service discovery is not being cleared before adding in the re-discovered services. Breakpoint at line 325 in |
To reproduce:
import asyncio
import logging
from bleak import BleakClient
format_string = "%(asctime)s %(levelname)s %(name)s: %(message)s"
logging.basicConfig(level=logging.DEBUG,
format=format_string,
)
logger = logging.getLogger('run')
def disconnect_handler(c: BleakClient):
logger.info("Disconnect handler")
asyncio.get_running_loop().create_task(reconnect(c))
async def reconnect(c: BleakClient):
logger.info("Reconnect")
await c.connect()
if not c.is_connected:
# lazily call recursively, as shouldn't be more than a few
await asyncio.sleep(10)
await reconnect(c)
async def run():
BLE_ID = 'CF:75:75:90:8C:E3'
client = BleakClient(BLE_ID)
client.set_disconnected_callback(disconnect_handler)
await client.connect()
logger.info("Connected")
await asyncio.sleep(90)
logger.info("90 seconds, disconnecting")
await client.disconnect()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(run()) |
Ugly fix, but this at least appears to be functional for this specific host and implementation Use "private access" with
|
For those seeing this issue when reconnecting after a disconnect, I think #621 (comment) will fix the issue (it essentially does this: #376 (comment)). If anyone is seeing this error at a different time, it would be interesting/helpful if you could confirm the sequence of events I describe in #625 (comment). |
Test example for hbldh/bleak#376
This should be fixed by #902. |
bluetoothctl -v
) in case of Linux: 5.50Description
For very first time the central code for scanning BLE Devices worked and connected with uart device. But after connection I was not able to disconnect it.
What I Did
I had scanned BLE Devices and make connection. It worked and shown
disconnected, scanning
connected
Then I had stopped peripheral service. After service was stopped I had ran scanner class again then i was showing below error.
The text was updated successfully, but these errors were encountered: