ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
result: command is entered and bluetoothctl immediately quits without waiting for the pin prompt so it can be captured
Code:
bluetoothctl <<< 'pair 54:46:6B:01:6C:CC'
result: command is entered and bluetoothctl immediately quits without waiting for the pin prompt so it can be captured
What is the solution here?
edit: Fixed.
I tried to use:
Code:
hcitool cc <bdaddr>; hcitool auth <bdaddr>
This always failed.
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'
To debug you can tmux attach-session -t ServerFault or cat /run/shm/BLUETOOTH_OUTPUT.
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.
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'
Waiting to connect to bluetoothd...
[bluetooth]#
[NEW] Controller C4:85:08:A0:67:51 alar [default] [bluetooth]#
[NEW] Device 1C:52:16:9A:DC:D5 QCY-QY19 [bluetooth]#
[NEW] Device 9C:DF:03:0E:C7:EC harman BT [bluetooth]#
[NEW] Device 00:0D:18:A1:50:2F MMC12 [bluetooth]# info 54:46:6B:01:6C:CC Device 54:46:6B:01:6C:CC not available [bluetooth]# exit [bluetooth]#
[DEL] Controller C4:85:08:A0:67:51 alar [default] [bluetooth]#
Waiting to connect to bluetoothd...
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.
I tried your script @firstfire.
It always tells me the agent is not registered.
Code:
~$ connect-bluetooth-speaker.sh
Restarting bluetooth service.
[NEW] Device CC:D2:AC:A8:78:B7 MX Anywhere 2S [bluetooth]# connect AE:2D:22:00:35:A2 Attempting to connect to AE:2D:22:0[DEL] Controller 60:F6:77:45:DA:4D BlueZ 5.48 [default] No agent is registered
But if I launch bluetoothctl manually, the agent registers, and I can connect the device.
I tried adding "agent on\n" to the connect line, but to no avail.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.