#!/bin/bash
# beacon-scan.sh
# Displays beacons including duplicates in real time.
# Uses expect to automate interaction with bluetoothctl. -> sudo apt-get install expect
# Uses sed to remove bluetoothctl colorization escape characters.
# Uses grep to filter out beacon manufacturer data logging.
# Uses Perl to prefix each beacon with a timestamp.
if [ "$(id -u)" != "0" ]; then
echo "ERROR: must run as root"
exit 1
fi
(cat <<'END' | /usr/bin/expect
set Prompt "#"
set timeout -1
spawn bluetoothctl
expect -re $Prompt
send "scan off\r"
expect -re $Prompt
send "remove *\r"
expect -re $Prompt
send "set-scan-filter-clear\r"
expect -re $Prompt
send "set-scan-filter-transport le\r"
expect -re $Prompt
send "scan on\r"
trap {
expect -re $Prompt
send "scan off\r"
expect -re $Prompt
send "remove *\r"
expect -re $Prompt
send "quit\r"
} SIGINT
expect eof
END
) | sed --unbuffered --quiet --expression 's/^.*Device //p' \
| grep --line-buffered -v ManufacturerData \
| perl -nle 'print scalar(localtime), " ", $_'
|