p4a: update example to use context manager
Using a context manager simplifes the code.
@@ -58,9 +58,8 @@ class ExampleApp(App):
for device in scanned_devices:
self.line("Connecting to {0} ...".format(str(device)[:24]))
- client = bleak.BleakClient(device.address)
try:
- await client.connect()
+ async with bleak.BleakClient(device) as client:
services = await client.get_services()
for service in services.services.values():
self.line(" service {0}".format(service.uuid))
@@ -74,8 +73,6 @@ class ExampleApp(App):
)
except bleak.exc.BleakError as e:
self.line(" error {0}".format(e))
- finally:
- await client.disconnect()
except bleak.exc.BleakError as e:
self.line("ERROR {0}".format(e))
await asyncio.sleep(1)
