LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 03:10 PM   #1
Jason_25
Member
 
Registered: Nov 2001
Posts: 55

Rep: Reputation: 15
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 11:16 PM.
 
Old 10-17-2017, 12:11 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:56 PM
bluetoothctl can't find keyboard on certain computer dpeterson3 Linux - Hardware 0 06-23-2016 05:45 AM
LXer: Shell Scripting Part 4: Repetition Control Structures LXer Syndicated Linux News 0 05-17-2015 10:02 AM
Need help scripting ..version control a file linuxuser2010 Linux - Newbie 6 05-29-2010 08:32 AM
X control scripting thatto Linux - Software 0 02-14-2005 04:07 AM

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

All times are GMT -5. The time now is 01:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration