Skip to content

Commit

Permalink
Add --write-with-response flag, default false
Browse files Browse the repository at this point in the history
Restores pre bleak 0.21 behaviour to use write without response per default
Fixes #109
  • Loading branch information
Jakeler committed Nov 24, 2024
1 parent a9564b1 commit 0f183c6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions ble_serial/bluetooth/ble_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions ble_serial/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ble_serial/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 0f183c6

Please sign in to comment.