Reputation: 1
Currently working on a project, I am trying to communicate with my RPi 3 to a BLE device (only reading information from the device). I am using bluez and bluetoothctl. I succeed in connecting the RPi to the device, and can select the attributes and read the characteristics, but I would like to do it with a python script (no need to write the commands).
My issue is, when I ran my code, it seems that it got stuck at the first command. This is my code :
import subprocess
subprocess.call('bluetoothctl')
subprocess.call('connect E5:10:78:27:B5:22')
and here is the result
pi@raspberrypi:~ $ cd Desktop/bluetooth/
pi@raspberrypi:~/Desktop/bluetooth $ python main.py
[NEW] Controller 5C:F3:70:87:7E:2E raspberrypi #1 [default]
[NEW] Device E5:10:78:27:B5:22 PARKING CONNECTE
[NEW] Controller B8:27:EB:6B:EC:CE raspberrypi
[NEW] Device E5:10:78:27:B5:22 PARKING CONNECTE
[NEW] Device 0C:8F:FF:59:61:48 Wifi a vendre - 5e l'acces
[NEW] Device CF:8E:BE:9C:C1:30 Nordic_UART
[bluetooth]#
Does anyone have a solution or another way to read the characteristics of the device? (and potentially get it back in a file)
Upvotes: 0
Views: 2488
Reputation: 1436
I'd advise you to take a look at the subprocess module's docs and especially the subprocess.Popen() function.
I'm not sure if bluetoothctl
is a program that takes arguments on launch, or if it has to keep running in order to listen to incoming commands.
If it's the second some untested dummy-code could look like something as:
import subprocess
process = subprocess.Popen(['bluetoothctl'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write('connect E5:10:78:27:B5:22')
process.stdin.flush()
process.wait()
output, errors = process.communicate()
output_to_write_to_file = output.decode()
Then, all you have to do is Google on how to write the content of a variable to a file in Python. (Which really shouldn't be too hard to find).
Upvotes: 2