Control bluetoothctl with scripting?
I am trying to interact with bluetoothctl through scripting. Such as shown in these threads:
https://stackoverflow.com/questions/...alent-commands https://stackoverflow.com/questions/...er-interaction Code:
bluetoothctl << EOF \n pair 54:46:6B:01:6C:CC \n EOF Code:
echo -e 'pair 54:46:6B:01:6C:CC' | bluetoothctl Code:
bluetoothctl <<< 'pair 54:46:6B:01:6C:CC' What is the solution here? edit: Fixed. I tried to use: Code:
hcitool cc <bdaddr>; hcitool auth <bdaddr> I then tried to use screen which was similar to the second two problems above in that the bluetoothctl just quits immediately. I also spent some time needlessly trying to manipulate the file handlers for the process in /proc but ultimately that just writes to the screen not the program itself. Finally the answer was to use Tmux as so: Code:
tmux new-session -d -s ServerFault 'sudo bluetoothctl -a |& tee /run/shm/BLUETOOTH_OUTPUT' Code:
tmux send-keys -t ServerFault -l 'pair BT_MAC_OF_SLAVE_GOES_HERE' Notes: I had to remove all the tr and a through z stuff. Just by accident I stumbled upon the |& tee stuff. That is required because a normal redirect to file does not work and is blank. I chose /run/shm because at least on the Raspberry Pi that is where RAM files are stored. The word ServerFault can be anything you like. I think the newest version of Bluez may have a different, possibly better utility for doing this. As far as bluetoothctl itself goes it takes more to actually do anything than the examples here. It also takes way more to actually write a script that can handle Bluetooth connections through bluetoothctl but hopefully this gets you started on one or allows you to work up a simple solution. References: https://serverfault.com/questions/17...a-screen-sessi https://www.reddit.com/r/tmux/commen...zh&sh=48426000 |
Hi.
In bash you can use e.g. coproc to start and communicate with another process: Code:
#!/bin/bash |
Hi,
seems that your answer cannot be working in a script, but only via command line i faild to launch a script that executes coproc command |
Hi.
Welcome to LQ! It works for me (in the sense that it is possible to interact with background process; it's up to you what exactly you'll do with bluetoothctl) so make sure that 1. You made the script executable (chmod +x script.sh) 2. Your version of bash supports coproc (help coproc; Coprocesses section in man bash) 3. bluetoothctl is actually installed and working. 4. bluetoothctl adds color codes and other control characters to output which may lead to strange results (e.g. it may appear that there are no output at all -- pipe to less or hd to see that it's there). To alleviate this one may use sed, for instance Code:
$ /tmp/test.sh | sed 's/\x1B\[[0-9;]*[JKmsu]//g; s/\r/\n/g' |
Hello
Many thanks for your answer and help actually it's working in command line, so i think coproc is supported and bluetoothctl is installed and working When I launch it in command line, line by line it works Then I create a simple script, I make it executable with others simple commands like echo.. I can see echo results But when I check the process I see that coproc bluetoothctl has not been launched and so I have help coproc showing this i@pi3_portail:~/script_py$ help coproc coproc: coproc [NAME] command [redirections] Create a coprocess named NAME. Execute COMMAND asynchronously, with the standard output and standard input of the command connected via a pipe to file descriptors assigned to indices 0 and 1 of an array variable NAME in the executing shell. The default NAME is "COPROC". Exit Status: The coproc command returns an exit status of 0. |
So i thing, from your answer, the point 1, 2, 3 are OK
For the point 4.. not very clear for me sorry |
Hi.
I tried your script @firstfire. It always tells me the agent is not registered. Code:
~$ connect-bluetooth-speaker.sh I tried adding "agent on\n" to the connect line, but to no avail. Code:
echo -e 'connect AE:2D:22:00:35:A2\nexit' >&${COPROC[1]} Code:
~$ connect-bluetooth-speaker.sh My script looks like this. I restart the bluetooth service because otherwise, after waking up from sleep, audio sink bluetooth connection fail. Code:
echo Restarting bluetooth service. |
Launching bluetoothctl manually has the following effect.
Code:
~$ bluetoothctl |
Additionally, like pantiri, when I launch your commands manually (not in bash script), it works fine.
|
And with your sed command, I get following output.
Code:
~$ connect-bluetooth-speaker.sh | sed 's/\x1B\[[0-9;]*[JKmsu]//g; s/\r/\n/g' |
Solved it by using the following automation script:
https://gist.github.com/RamonGilaber...b302b4d9fb0055 Code:
set prompt "#" |
zsh version:
Code:
#!/usr/bin/zsh |
All times are GMT -5. The time now is 02:59 PM. |