0

I use a script found here detect nearby beacons with Raspberry Pi. This is the script code

#!/bin/bash
# iBeacon Scan by Radius Networks

if [[ $1 == "parse" ]]; then
  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]
          if [[ $2 == "-b" ]]; then
        echo "$UUID $MAJOR $MINOR $POWER"
          else
            echo "UUID: $UUID MAJOR: $MAJOR MINOR: $MINOR POWER: $POWER"
          fi
        fi
        capturing=""
        packet=""
      fi
    fi

    if [ ! "$capturing" ]; then
      if [[ $line =~ ^\> ]]; then
        packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
        capturing=1
      fi
    fi
  done
else
  sudo hcitool lescan --duplicates 1>/dev/null &
  if [ "$(pidof hcitool)" ]; then
    sudo hcidump --raw | ./$0 parse $1
  fi
fi

The script works fine and show the UUID, Major, Minor and Power values. However I would also like to get the MAC address of each beacon. I am almost sure that the value is there because the command used in the script sudo hcitool lescan --duplicates show me the MAC dresses but I was not able to add it to this line

echo "$UUID $MAJOR $MINOR $POWER"

Do someone has an idea on how to get the value ?

Community
  • 1
  • 1
Doe.J
  • 1
  • 5

1 Answers1

0

you can do it with these lines of code

# parse line from 21 and 17 characters (including spaces), then remove the spaces
REVERSE_MAC=`echo $packet | sed 's/^.\{21\}\(.\{17\}\).*$/\1/' | sed 's/\ //g'`
# the MAC was reversed so I have to put it in order, always by 2 characters
MAC=`echo "${REVERSE_MAC:10:2}:${REVERSE_MAC:8:2}:${REVERSE_MAC:6:2}:${REVERSE_MAC:4:2}:${REVERSE_MAC:2:2}:${REVERSE_MAC:0:2}"`

so the whole code is:

#!/bin/bash
# iBeacon Scan by Radius Networks

if [[ $1 == "parse" ]]; then
  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]
          # CAPTURE MAC ADDRESS
          # parse line from 21 and 17 characters (including spaces), then remove the spaces
          REVERSE_MAC=`echo $packet | sed 's/^.\{21\}\(.\{17\}\).*$/\1/' | sed 's/\ //g'`
          # the MAC was reversed so I have to put it in order, always by 2 characters
          MAC=`echo "${REVERSE_MAC:10:2}:${REVERSE_MAC:8:2}:${REVERSE_MAC:6:2}:${REVERSE_MAC:4:2}:${REVERSE_MAC:2:2}:${REVERSE_MAC:0:2}"`
          if [[ $2 == "-b" ]]; then
            echo "$UUID $MAJOR $MINOR $POWER"
          else
            echo "UUID: $UUID MAJOR: $MAJOR MINOR: $MINOR POWER: $POWER MAC: $MAC"
          fi
        fi
        capturing=""
        packet=""
      fi
    fi

    if [ ! "$capturing" ]; then
      if [[ $line =~ ^\> ]]; then
        packet=`echo $line | sed 's/^>.\(.*$\)/\1/'`
        capturing=1
      fi
    fi
  done
else
  sudo hcitool lescan --duplicates 1>/dev/null &
  if [ "$(pidof hcitool)" ]; then
    sudo hcidump --raw | ./$0 parse $1
  fi
fi
Mazel Tov
  • 2,064
  • 14
  • 26