Skip to content

Instantly share code, notes, and snippets.

@thecyberd3m0n
Created June 29, 2022 23:10
Show Gist options
  • Save thecyberd3m0n/f7ee122c51aabc871c4719b849efe9ce to your computer and use it in GitHub Desktop.
Save thecyberd3m0n/f7ee122c51aabc871c4719b849efe9ce to your computer and use it in GitHub Desktop.
Bash: detect iBeacons
#!/bin/bash
# iBeacon Scan by Radius Networks, modified by Dmitrij Rysanow (https://gitlab.com/thecyberd3m0n)
# will detect all iBeacon devices around and measure respective RSSI.
# Requires Bluetooth. If not works, run sudo hciconfig hci0 down; sudo hciconfig hci0 up;
# https://stackoverflow.com/questions/21733228/can-raspberrypi-with-ble-dongle-detect-ibeacons/21790504#21790504
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
if [[ $1 == "parse" ]]; then
echo "enabling scanning"
echo "reading ibeacons"
packet=""
capturing=""
count=0
while read line
do
count=$[count + 1]
if [ "$capturing" ]; then
if [[ $line =~ ^[0-9a-fA-F]{2}\ [0-9a-fA-F] ]]; then
packet="$packet $line"
else
if [[ $packet =~ ^04\ 3E\ 2A\ 02\ 01\ .{26}\ 02\ 01\ .{14}\ 02\ 15 ]]; then
UUID=`echo $packet | sed 's/^.\{69\}\(.\{47\}\).*$/\1/'`
MAJOR=`echo $packet | sed 's/^.\{117\}\(.\{5\}\).*$/\1/'`
MINOR=`echo $packet | sed 's/^.\{123\}\(.\{5\}\).*$/\1/'`
POWER=`echo $packet | sed 's/^.\{129\}\(.\{2\}\).*$/\1/'`
UUID=`echo $UUID | sed -e 's/\ //g' -e 's/^\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\)$/\1-\2-\3-\4-\5/'`
MAJOR=`echo $MAJOR | sed 's/\ //g'`
MAJOR=`echo "ibase=16; $MAJOR" | bc`
MINOR=`echo $MINOR | sed 's/\ //g'`
MINOR=`echo "ibase=16; $MINOR" | bc`
POWER=`echo "ibase=16; $POWER" | bc`
POWER=$[POWER - 256]
RSSI=`echo $packet | sed 's/^.\{132\}\(.\{2\}\).*$/\1/'`
RSSI=`echo "ibase=16; $RSSI" | bc`
RSSI=$[RSSI - 256]
if [[ $2 == "-b" ]]; then
echo "$UUID $MAJOR $MINOR $POWER $RSSI"
else
echo "UUID: $UUID MAJOR: $MAJOR MINOR: $MINOR POWER: $POWER RSSI: $RSSI"
fi
fi
capturing=""
packet=""
fi
fi
if [ ! "$capturing" ]; then
if [[ $line =~ ^\> ]]; then
packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
capturing=1
fi
fi
done
else
echo "run hcitool lescan"
hcitool lescan --duplicates 1>/dev/null &
if [ "$(pidof hcitool)" ]; then
echo "pid $(pidof hcitool)"
sudo hcidump --raw | ./$0 parse $1
else
echo "ERROR: hcitool not running"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment