Skip to content

Commit

Permalink
Add timeout parameter to ble-serial
Browse files Browse the repository at this point in the history
use -t, rename address-type to -a
ref #30
  • Loading branch information
Jakeler committed May 26, 2021
1 parent ca3f4d8 commit 6a9586f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ble_serial/bluetooth/ble_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions ble_serial/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a9586f

Please sign in to comment.