Scan bluetooth low energy using hcitool? - linux

When I run this command which makes the ble device scanning for just 5 seconds only:
$ sudo timeout 5s hcitool -i hci0 lescan
the output is shown in the terminal screen.
But when I redirect the output to a file to save the addresses of the advertising devices, every time I run the command I find the file is empty and the output isn't visible in the terminal nor in the file.
The command I used:
$ sudo timeout 5s hcitool -i hci0 lescan > file.txt
What do I have to do in order to make hcitool correctly redirect its ouput to the file?

timeout by default sends a SIGTERM to the program. Looks like hcitool doesn't handle that gracefully. Instead use SIGINT (equivalent to ctrl-c).
sudo timeout -s SIGINT 5s hcitool -i hci0 lescan > file.txt

Related

Bluetooth, btmgmt tool how to set Link Mode

I try to understand how btmgmt work. I search command equal to:
sudo hciconfig -a hci0 lm master
I have try to use sudo btmon for dumping hci command but I have only this result:
# RAW Open: hciconfig (privileged) version...
with no op-code output, this not realy help me. Writing output to btsnoop format reveal some unknown op codes (btmon -a /tmp/btsnoop). With wireshark I have no HCI events, I don't understand.
Using sudo strace hciconfig... reveal socket and ioctl call with ioctl(ctl, HCISETLINKMODE, dr)
I read HCISETLINKMODE value and hciconfig function
but nothing about op-code to put the interface in MASTER or SLAVE mode. For some others btmgmt commands I use btmon output and the command op-code and I use op-code to search in mgmt-api.txt.
After reading that hciconfig would be deprecated, I'm trying to migrate commands using btmgmt and i just miss this one.
Any help is welcome. Thank you

bluez: scan and connect in parallel

what I want to achieve in a test setup is:
running:
hcitool -i hci0 lescan --duplicates --passive
on one shell
and in parallel connect on another via
hcitool -i hci0 lecc <BTADDR>
The moment I issue the connect command the scan process exits.
Why is that? I thought choosing to scan --passive will allow a scan output while managing a connection?
Best,
If you are on kernel 3.2 and above use gatttool or bluetoothctl for LE connections. hcitool is deprecated and you should not be using them anymore. Compile and install latest bluez and use gatttool for LE connections. If your LE device is in the vicinity, you should be able to connect, read and write using gatttool directly:
say, your local BT adapter is hci0 and the remote device BD_ADDR is 12:22:33:44:55:66 then:
gatttool -i hci0 -b 12:22:33:44:55:66 -I
[12:22:33:44:55:66][LE]>connect
Attempting to connect to 12:22:33:44:55:66
Connection successful
[12:22:33:44:55:66][LE]>characteristics
Read manual and help to know more about gattool.

Cant terminate QProcess running hcitool on linux?

I am trying to use the Linux/Bluez tool: hcitool, to make a BLE scan from QT.
I use the following to open the process:
QString program = "sudo stdbuf -oL hcitool -i hci0 lescan";
hcitool = new QProcess();
connect(hcitool, SIGNAL(started()), this, SLOT(hcitool_started()));
connect(hcitool, SIGNAL(finished(int)), this, SLOT(hcitool_finished(int)));
connect(this, SIGNAL(kill_hcitool()), hcitool, SLOT(kill()));
connect(this, SIGNAL(terminate_hcitool()), hcitool, SLOT(terminate()));
hcitool->start(program, QProcess::Unbuffered | QProcess::ReadWrite);
Then afterwards I continously read from the process to store all the results:
QString result = hcitool->readLine();
After a couple of seconds, I want to stop the hcitool process, and this is where things goes wrong. I've tried the terminate signal, the kill signal, writing ^C to the process, but nothing works.
Actually the kill signal finishes the process. But not in a good way because afterwards my hci0 is hold up, and I can't start hcitool again before the hci0 has been reset with:
hciconfig hci0 down
hciconfig hci0 up
So any ideas to how I can terminate this process right?
(moving from the comment)
A trick that usually work is to just close() the stream, that should give a SIGHUP or SIGPIPE to the child process, which is often handled more gracefully.

echo command to serial port in Linux

I want to echo something from my Ubuntu host to some device.
It works well if I use putty or minicom.
However, it doesn't work if I do echo from the shell terminal:
echo "cmd" > /dev/ttyUSB0
From my device, I saw that the first letter of the cmd is received correctly while the second one received is ASCII bigger than 200. I also have tried to use the "stty" command to adjust the serial communication settings but didn't help. Does anythone know why?
Thanks,
First You need to set the tty device settings and then you need to transmit the data whatever you want
stty -F /dev/ttyUSB0 9600 -parity cs8 -cstopb
OR
stty -speed 9600 < /dev/ttyUSB0
Now Send data:
echo "cmd" > /dev/ttyUSB0

Raspberry Pi & Blend Micro : can't connect with Bluetooth

Recently got a Blend Micro for a project and I need a raspberry pi to initiate a bluetooth connection with it.
For that I installed BlueZ 5.2 on raspberry and i used this command to detect the Blend Micro :
sudo hcitool -i hci0 lescan
I correctly detect the Blend micro then i use the gatttool command :
sudo gatttool -i hcitool hci0 -b XX:XX:XX:XX:XX:XX -I
(where the XX corresponds to the Blend MAC address)
But when I initiate the connection by typing connect it says "Host is down" :
$ sudo gatttool -i hci0 -b XX:XX:XX:XX:XX:XX -I
[ ][XX:XX:XX:XX:XX:XX][LE]> connect
Connecting... connect error: Host is down (112)
[ ][XX:XX:XX:XX:XX:XX][LE]>
Do you have any idea on how to fix that ? Tried many things without any results though...
Regards, RaZZeR
When you are connecting, you need to add "-t random" to your command line.

Resources