LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2017, 02:10 PM   #1
Jason_25
Member
 
Registered: Nov 2001
Posts: 180

Rep: Reputation: 23
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
result: > is displayed and nothing happens

Code:
echo -e 'pair 54:46:6B:01:6C:CC' | bluetoothctl
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 send commands:
Code:
tmux send-keys -t ServerFault -l 'pair BT_MAC_OF_SLAVE_GOES_HERE'
tmux send-keys -t ServerFault Enter
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.

References:
https://serverfault.com/questions/17...a-screen-sessi
https://www.reddit.com/r/tmux/commen...zh&sh=48426000

Last edited by Jason_25; 10-09-2017 at 10:16 PM.
 
Old 10-16-2017, 11:11 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

In bash you can use e.g. coproc to start and communicate with another process:
Code:
#!/bin/bash

coproc bluetoothctl
echo -e 'info 54:46:6B:01:6C:CC\nexit' >&${COPROC[1]}
output=$(cat <&${COPROC[0]})
echo $output
Here COPROC is a two-element array containing output/input file descriptors respectively. See the Coprocesses section of man bash for details.
 
Old 05-03-2018, 06:44 AM   #3
pantiri
LQ Newbie
 
Registered: May 2018
Posts: 4

Rep: Reputation: Disabled
Unhappy

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
 
Old 05-04-2018, 05:35 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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'
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...
 
Old 05-04-2018, 06:19 AM   #5
pantiri
LQ Newbie
 
Registered: May 2018
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 05-04-2018, 06:21 AM   #6
pantiri
LQ Newbie
 
Registered: May 2018
Posts: 4

Rep: Reputation: Disabled
So i thing, from your answer, the point 1, 2, 3 are OK
For the point 4.. not very clear for me sorry
 
Old 07-28-2018, 06:03 AM   #7
mgerber
LQ Newbie
 
Registered: Mar 2005
Location: Gland, Switzerland
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
Hi.

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.

Code:
echo -e 'connect AE:2D:22:00:35:A2\nexit' >&${COPROC[1]}
The output is then like this:

Code:
~$ connect-bluetooth-speaker.sh 
Restarting bluetooth service.
[NEW] Device AE:2D:22:00:35:A2 MS-10DMKII [bluetooth]# agent on Failed to register agent object [bluetooth]# connect AE:[DEL] Device 50:1A:A5:76:ED:F8 Jabra MOVE v2.5.0 [DEL] Device DD:B3:5F:80:79:3F HP Tilt Pen [DEL] Device C5:1B:74:09:FC:45 fenix 3 HR [DEL] Device CC:D2:AC:A8:78:B7 MX Anywhere 2S No agent is registered [DEL] Controller 60:F6:77:45:DA:4D BlueZ 5.48 [default]

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.
sudo service bluetooth restart

coproc bluetoothctl
echo -e 'agent on\nconnect AE:2D:22:00:35:A2\nexit' >&${COPROC[1]}
output=$(cat <&${COPROC[0]})
echo $output
 
Old 07-28-2018, 06:05 AM   #8
mgerber
LQ Newbie
 
Registered: Mar 2005
Location: Gland, Switzerland
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
Launching bluetoothctl manually has the following effect.

Code:
~$ bluetoothctl 
[NEW] Controller 60:F6:77:45:DA:4D spectre [default]
...
...
...
[NEW] Device AE:2D:22:00:35:A2 MS-10DMKII
Agent registered
[MX Anywhere 2S]# connect AE:2D:22:00:35:A2
Attempting to connect to AE:2D:22:00:35:A2
[CHG] Device AE:2D:22:00:35:A2 Connected: yes
Connection successful
[CHG] Device AE:2D:22:00:35:A2 ServicesResolved: yes
[MS-10DMKII]#
 
Old 07-28-2018, 06:12 AM   #9
mgerber
LQ Newbie
 
Registered: Mar 2005
Location: Gland, Switzerland
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
Additionally, like pantiri, when I launch your commands manually (not in bash script), it works fine.
 
Old 07-28-2018, 06:24 AM   #10
mgerber
LQ Newbie
 
Registered: Mar 2005
Location: Gland, Switzerland
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
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'
Restarting bluetooth service.

Waiting to connect to bluetoothd...
[bluetooth]# 
[NEW] Controller 60:F6:77:45:DA:4D BlueZ 5.48 [default] [bluetooth]# 
[NEW] Device AE:2D:22:00:35:A2 MS-10DMKII [bluetooth]# 
...
[NEW] Device CC:D2:AC:A8:78:B7 MX Anywhere 2S [bluetooth]# agent on Failed to register agent object [bluetooth]# connect AE:2D:22:00:35:A2 Attempting to connect to AE:2D:22:00:35:A2 [bluetooth]# exit [bluetooth]# 
[DEL] Controller 60:F6:77:45:DA:4D BlueZ 5.48 [default] No agent is registered
 
Old 07-30-2018, 12:43 AM   #11
mgerber
LQ Newbie
 
Registered: Mar 2005
Location: Gland, Switzerland
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
Solved it by using the following automation script:
https://gist.github.com/RamonGilaber...b302b4d9fb0055

Code:
set prompt "#"
set address AE:2D:22:00:35:A2
# set address [lindex $argv 0]

send_user "\nRestarting bluetooth service.\r"
spawn sudo service bluetooth restart

send_user "\nConnecting to MS-10DMKII.\r"
spawn bluetoothctl
expect -re $prompt
expect "Agent registered"
send "connect $address\r"
expect "Connection successful"
send "quit\r"
expect eof
 
Old 01-08-2020, 06:28 AM   #12
nvergontbij
LQ Newbie
 
Registered: Jan 2020
Posts: 1

Rep: Reputation: Disabled
zsh version:

Code:
#!/usr/bin/zsh

coproc bluetoothctl
echo -e 'agent on\nconnect F0:13:C3:4B:84:E9\nexit' >&p
output=$(cat <&p)
echo $output
just my 2 cents.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problems with bluetooth (bluetoothctl, blueman, rfcomm) ahTh6aaf Slackware 5 10-08-2017 10:56 PM
bluetoothctl can't find keyboard on certain computer dpeterson3 Linux - Hardware 0 06-23-2016 04:45 AM
LXer: Shell Scripting Part 4: Repetition Control Structures LXer Syndicated Linux News 0 05-17-2015 09:02 AM
Need help scripting ..version control a file linuxuser2010 Linux - Newbie 6 05-29-2010 07:32 AM
X control scripting thatto Linux - Software 0 02-14-2005 03:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration