Fix disconnect_callback example
This example was not working because of the extra `future` argument.
Also remove depedency on a specific device address while we are touching this file.
Disconnect callback
-------------------
-An example showing how the `set_disconnect_callback` can be used on BlueZ backend.
+An example showing how the `set_disconnected_callback` can be used on BlueZ backend.
Updated on 2019-09-07 by hbldh <henrik.blidh@gmail.com>
@@ -10,19 +10,24 @@ Updated on 2019-09-07 by hbldh <henrik.blidh@gmail.com>
import asyncio
-from bleak import BleakClient
+from bleak import BleakClient, discover
-async def show_disconnect_handling(mac_addr: str):
- async with BleakClient(mac_addr) as client:
+async def show_disconnect_handling():
+ devs = await discover()
+ if not devs:
+ print("No devices found, try again later.")
+ return
+
+ async with BleakClient(devs[0]) as client:
disconnected_event = asyncio.Event()
- def disconnect_callback(client, future):
+ def disconnected_callback(client):
print("Disconnected callback called!")
- asyncio.get_event_loop().call_soon_threadsafe(disconnected_event.set)
+ disconnected_event.set()
- client.set_disconnected_callback(disconnect_callback)
- print("Sleeping until device disconnects according to BlueZ...")
+ client.set_disconnected_callback(disconnected_callback)
+ print("Sleeping until device disconnects...")
await disconnected_event.wait()
print("Connected: {0}".format(await client.is_connected()))
await asyncio.sleep(
@@ -30,6 +35,5 @@ async def show_disconnect_handling(mac_addr: str):
) # Sleep a bit longer to allow _cleanup to remove all BlueZ notifications nicely...
-mac_addr = "24:71:89:cc:09:05"
loop = asyncio.get_event_loop()
-loop.run_until_complete(show_disconnect_handling(mac_addr))
+loop.run_until_complete(show_disconnect_handling())
