From 0f183c6c3a5731b95d439b87723b64c75c5c5bec Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 24 Nov 2024 04:22:30 +0100 Subject: [PATCH] Add `--write-with-response` flag, default false Restores pre bleak 0.21 behaviour to use write without response per default Fixes #109 --- README.md | 2 ++ ble_serial/bluetooth/ble_interface.py | 9 ++++++--- ble_serial/cli.py | 2 ++ ble_serial/main.py | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a9b2d51..6795fc3 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,8 @@ device parameters: -r READ_UUID, --read-uuid READ_UUID The GATT characteristic to subscribe to notifications to read the serial data (default: None) --permit {ro,rw,wo} Restrict transfer direction on bluetooth: read only (ro), read+write (rw), write only (wo) (default: rw) + --write-with-response + Wait for a response from the remote device before sending more. Better data integrity, higher latency and less througput (default: False) ``` In any case it needs to know which device to connect, the simple and most reliable way to specify this is by device address/id: diff --git a/ble_serial/bluetooth/ble_interface.py b/ble_serial/bluetooth/ble_interface.py index 179d7ce..0b215c5 100644 --- a/ble_serial/bluetooth/ble_interface.py +++ b/ble_serial/bluetooth/ble_interface.py @@ -31,12 +31,15 @@ async def connect(self, addr_str: str, addr_type: str, timeout: float): await self.dev.connect() logging.info(f'Device {self.dev.address} connected') - async def setup_chars(self, write_uuid: str, read_uuid: str, mode: str): + async def setup_chars(self, write_uuid: str, read_uuid: str, mode: str, write_response_required: bool): self.read_enabled = 'r' in mode self.write_enabled = 'w' in mode if self.write_enabled: - self.write_char = self.find_char(write_uuid, ['write', 'write-without-response']) + self.write_response_required = write_response_required + + write_cap = ['write' if write_response_required else 'write-without-response'] + self.write_char = self.find_char(write_uuid, write_cap) else: logging.info('Writing disabled, skipping write UUID detection') @@ -101,7 +104,7 @@ async def send_loop(self): logging.warning(f'Ignoring unexpected write data: {data}') continue logging.debug(f'Sending {data}') - await self.dev.write_gatt_char(self.write_char, data) + await self.dev.write_gatt_char(self.write_char, data, self.write_response_required) def stop_loop(self): logging.info('Stopping Bluetooth event loop') diff --git a/ble_serial/cli.py b/ble_serial/cli.py index 9da5559..a80e7b8 100644 --- a/ble_serial/cli.py +++ b/ble_serial/cli.py @@ -30,6 +30,8 @@ def parse_args(): help='The GATT characteristic to subscribe to notifications to read the serial data') dev_group.add_argument('--permit', dest='mode', required=False, default='rw', choices=['ro', 'rw', 'wo'], help='Restrict transfer direction on bluetooth: read only (ro), read+write (rw), write only (wo)') + dev_group.add_argument('--write-with-response', dest='write_with_response', required=False, action='store_true', + help='Wait for a response from the remote device before sending more. Better data integrity, higher latency and less througput') log_group = parser.add_argument_group('logging options') log_group.add_argument('-l', '--log', dest='filename', required=False, diff --git a/ble_serial/main.py b/ble_serial/main.py index 7422ac2..15cde90 100644 --- a/ble_serial/main.py +++ b/ble_serial/main.py @@ -43,7 +43,7 @@ async def _run(self): self.uart.start() await self.bt.connect(args.device, args.addr_type, args.timeout) - await self.bt.setup_chars(args.write_uuid, args.read_uuid, args.mode) + await self.bt.setup_chars(args.write_uuid, args.read_uuid, args.mode, args.write_with_response) logging.info('Running main loop!') main_tasks = {