Running “bluetoothctl “ command in Linux terminal and in C++ code .
-
When I use Linux terminal I have to type
bluetoothctl --list
to get full and correct response in terminal.When I run the following code
QString process = “bluetoothctl”;
QString argument = “list”proc->start(process, QStringList() <<(argument) );
I get same result WITHOUT “--” Added ANYWHERE .
The code actually fails when the "--" is added.Why does it work without “--” ?
What am I doing wrong ?Secondly
I also need to have this to work in my codebluetoothctl –scan on
with previous" problem" I have no idea how to add the “on” as second argument .
However I need to solve the first issue first and would appreciate any help.
-
@AnneRanch
Did you trybluetoothctl list
from a shell? That is what theQProcess
code executes.From what is shown at e.g. https://waylonwalker.com/til/linux-bluetoothctl/ and https://superuser.com/questions/1500383/bluetoothctl-list-connected-devices there is no indication
bluetoothctl
accepts a--list
argument, it seems to expect commands as argument without a leading--
. Though it's not very clearly documented.bluetoothctl –scan on
Try either of
proc->start(process, QStringList() << "scan" << "on" ); proc->start(process, QStringList() << "--scan" << "on" );
It looks like you are "lucky" if you can get all your desired commands through as arguments to
blutetoothctl
on the command line. It is more designed as a "shell" to which you input commands. As https://stackoverflow.com/a/52012231/489865 says:I will say that bluetoothctl would be easier to use if it could be configured from the command line without needing to configure it interactively or having to resort to more complex scripting.
-
Here is "shell" output- using "terminal " I am trying top replicate .
Unfortunately the output form "bluetoothctl" , no option selected is indeed not documented anywhere .If that is correct result - how do I code "no option" as an argument ?
proc->start(process, QStringList() << "scan" << "on" );
q5@q5-desktop:~$ bluetoothctl
Agent registered
[CHG] Controller 00:15:83:15:A2:CB Pairable: yes
[bluetooth]# list
Controller 00:15:83:15:A2:CB q5-desktop [default]
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:15:83:15:A2:CB Discovering: yes
[NEW] Device 98:D3:31:F8:39:33 SPP-CA
[bluetooth]# -
@AnneRanch
Yes, this is the way that SO shows it operating. Assuming true, it means you cannot pass on the command line the commands you want it to execute. You have to do just what you show doing from the shell: namely, start it with no arguments, and then supply it with commands on its input. That means you will have towrite()
these commands to theQProcess
. It will be something like:proc->start(process, QStringList() ); // `QStringList()` is "empty/no command line arguments" proc->write("list\n"); proc->write("scan on\n"); ... proc->close(); // hoping this will end the `bluetoothctl` process
I have not dealt with looking at or waiting for responses to arrive (slot on
QProcess::readyRead()
which does aproc->readAll()
), just showing you how you need to send these commands to the process once it is running. -
@JonB Pr ogres report
I get expected response and some benefits when no arguments are used to run the process.
The attached extracts the Bluetooth address and actually reports some progress ( Waiting to connect...)Many thanks for you help.
Appreciate that.