【问题标题】:Get nearby beacons and their MAC adresses获取附近的信标及其 MAC 地址
【发布时间】:2017-02-24 21:19:07
【问题描述】:

我使用找到的脚本here 使用 Raspberry Pi 检测附近的信标。这是脚本代码

#!/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

脚本运行良好,并显示 UUID、Major、Minor 和 Power 值。但是我也想获取每个信标的 MAC 地址。我几乎可以肯定该值存在,因为 脚本sudo hcitool lescan --duplicates 给我看 MAC 连衣裙,但我无法将它添加到这一行

echo "$UUID $MAJOR $MINOR $POWER"

有人知道如何获得价值吗?

【问题讨论】:

    标签: ibeacon bluez ibeacon-android


    【解决方案1】:

    你可以用这几行代码做到这一点

    # 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}"`
    

    所以整个代码是:

    #!/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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-21
      • 2010-12-21
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多