hcitool lescan Timeout

As you might have noticed the “hcitool lescan” command doesn’t have a timeout argument. This limitation makes the Bluetooth Low Energy scan last forever until a CTRL+C breaks the execution.

To be able to scan for Bluetooth Low Energy devices in range for a given period of time and store the output of the scan in a file I’ve created the following script (lescan.sh):

#!/bin/sh
hciconfig hci0 down
hciconfig hci0 up
hcitool lescan > $2 &
pid=$!
sleep $1
kill -INT $pid
wait $pid


To scan for 10 seconds and store the output in a file named output.txt execute the following:

source lescan.sh 10s output.txt

Leave a Reply

Your email address will not be published. Required fields are marked *