I can see what's happening in the real terminal with "OK" in the end of the response.
But the read method does not return the last two lines.
@staticmethod
def communicate(write_uuid, read_uuid, data):
# Requires a writable and a readable GATT uuid and data to be written
# The method returns the data retrieved from the readable GATT uuid
hex_string = ''
for char in data:
hex_string += hex(ord(char)) + ' '
timeout = .1
read_process = ptyprocess.PtyProcessUnicode.spawn(['/bin/bash'])
commands = ['bluetoothctl', 'menu gatt', f'select-attribute {read_uuid}', 'notify on']
for command in commands:
read_process.write(command + '\n')
time.sleep(timeout)
read_process.read(1)
write_process = ptyprocess.PtyProcessUnicode.spawn(['/bin/bash'])
commands = f'bluetoothctl <<EOF\nmenu gatt\nselect-attribute {write_uuid}\nwrite "{hex_string}"\nEOF\n'
write_process.write(commands)
time.sleep(timeout)
write_process.read(1)
time.sleep(timeout)
hex_values = []
pattern = re.compile(r'\b[0-9a-fA-F]{2}\b')
lines = read_process.read(size=10000).splitlines()
for line in lines:
print(line)
This is the response of the method:
[CHG] Attribute /org/bluez/hci0/dev_CE_EE_A8_9B_17_AC/service000b/char000e Value:
[Device:/service000b/char000e]#
30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 0.0, 0.0, 0.0, 0
[Device:/service000b/char000e]#
2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e .0, 0.0, 0.0, 0.
[Device:/service000b/char000e]#
30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 0, 0.0, 0.0, 0.0
[Device:/service000b/char000e]#
2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c , 0.0, 0.0, 0.0,
[Device:/service000b/char000e][
This should be the correct response:
[Device:/service000b/char000e]# [CHG] Attribute /org/bluez/hci0/dev_CE_EE_A8_9B_17_AC/service000b/char000e Value:
[Device:/service000b/char000e]# 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 0.0, 0.0, 0.0, 0
[Device:/service000b/char000e]# 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e .0, 0.0, 0.0, 0.
[Device:/service000b/char000e]# 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 0, 0.0, 0.0, 0.0
[Device:/service000b/char000e]# 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c , 0.0, 0.0, 0.0,
[Device:/service000b/char000e]# 20 30 2e 30 2c 20 30 2e 30 2c 20 30 2e 30 2c 20 0.0, 0.0, 0.0,
[Device:/service000b/char000e]# 30 2e 30 2c 20 30 2e 30 20 4f 4b 0a 0.0, 0.0 OK.
As you can see we have 18 values and an OK at the end of the correct response. While we only have 13 values without OK in the read() method's response. Also: The read method returns an ESC character for some reason.
-
Have you considered that this might be a problem with ptyprocess? Also, the arbitrary sleeps are potentially unreliablejackal– jackal2024年06月21日 06:51:15 +00:00Commented Jun 21, 2024 at 6:51
-
1This is not a reproductible code, so we cannot really know where the problem is. But if I have to guess, i think you are printing with a '\n' between commands, and that kind of execution is noted in the line 'for command in commands:'. Try to print without command + '\n'Jose M. González– Jose M. González2024年06月21日 07:08:55 +00:00Commented Jun 21, 2024 at 7:08
-
run these commands verbatim from outside your python code and see if you capture matches what you think you expect. There could well be additional line-noise traffic produced from the bluetoothctl command.ticktalk– ticktalk2024年06月21日 07:55:11 +00:00Commented Jun 21, 2024 at 7:55
-
Unfortunately I have to use sleeps because the pseudo terminal needs time to react. Removing even a single line of sleep causes the code to malfunction. It's just 100 msec, I don't think that's the issue. I tried printing without '\n' and it did not change anything. I ran the code outside my python code and the result is already posted. The commands work fine in a terminal.sonus89– sonus892024年06月21日 10:16:10 +00:00Commented Jun 21, 2024 at 10:16
-
Surely the sleep will damage your script. Bluetooth communication is much faster. Have you tried something like expect?Risto– Risto2024年06月21日 11:40:31 +00:00Commented Jun 21, 2024 at 11:40
lang-py