1
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.

asked Jun 21, 2024 at 6:42
8
  • Have you considered that this might be a problem with ptyprocess? Also, the arbitrary sleeps are potentially unreliable Commented Jun 21, 2024 at 6:51
  • 1
    This 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' Commented 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. Commented 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. Commented Jun 21, 2024 at 10:16
  • Surely the sleep will damage your script. Bluetooth communication is much faster. Have you tried something like expect? Commented Jun 21, 2024 at 11:40

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.