diff --git a/ble_serial/bluetooth/ble_interface.py b/ble_serial/bluetooth/ble_interface.py index b178d2d..d6183cd 100644 --- a/ble_serial/bluetooth/ble_interface.py +++ b/ble_serial/bluetooth/ble_interface.py @@ -6,10 +6,13 @@ from typing import Optional class BLE_interface(): - async def start(self, addr_str, addr_type, adapter, write_uuid, read_uuid,): - self.dev = BleakClient(addr_str, adapter=adapter, address_type=addr_type) # address_type used only in Windows .NET currently + async def start(self, addr_str, addr_type, adapter, timeout, write_uuid, read_uuid,): self._send_queue = asyncio.Queue() + + # address_type used only in Windows .NET currently + self.dev = BleakClient(addr_str, adapter=adapter, address_type=addr_type, timeout=timeout) self.dev.set_disconnected_callback(self.handle_disconnect) + logging.info(f'Trying to connect with {addr_str}') await self.dev.connect() logging.info(f'Device {self.dev.address} connected') diff --git a/ble_serial/main.py b/ble_serial/main.py index d0cf852..e8eaf2b 100644 --- a/ble_serial/main.py +++ b/ble_serial/main.py @@ -24,7 +24,9 @@ def parse_args(self): help='Increase verbosity to log all data going through') parser.add_argument('-d', '--dev', dest='device', required=True, help='BLE device address to connect (hex format, can be seperated by colons)') - parser.add_argument('-t', '--address-type', dest='addr_type', required=False, choices=['public', 'random'], default='public', + parser.add_argument('-t', '--timeout', dest='timeout', required=False, default=5.0, type=float, metavar='SEC', + help='BLE connect/discover timeout in seconds') + parser.add_argument('-a', '--address-type', dest='addr_type', required=False, choices=['public', 'random'], default='public', help='BLE address type, either public or random') parser.add_argument('-i', '--interface', dest='adapter', required=False, default='hci0', help='BLE host adapter number to use') @@ -59,7 +61,8 @@ async def _run(self): self.uart.set_receiver(self.bt.queue_send) self.uart.start() - await self.bt.start(args.device, args.addr_type, args.adapter, args.write_uuid, args.read_uuid) + await self.bt.start(args.device, args.addr_type, args.adapter, args.timeout, + args.write_uuid, args.read_uuid) logging.info('Running main loop!') self.main_loop = asyncio.gather(self.bt.send_loop(), self.uart.run_loop()) await self.main_loop