#!/bin/bash
export INTERFACE="00:01:E3:45:31:73"
STATE="away"

while true; do
  hcitool cc ${INTERFACE} 2>/dev/null
  if hcitool con|grep -q ${INTERFACE}; then
    Signal="`hcitool lq ${INTERFACE}`"
    hcitool dc ${INTERFACE}
    echo Signal: $Signal

    if [ "${Signal##*: }" -lt 128 ]; then
      if [ "$STATE" != "away" ]; then
        STATE="away"
        export ACTION="unregister"
        /sbin/hotplug user
      fi
    else
      if [ "$STATE" != "near" ]; then
        STATE="near"
        export ACTION="register"
        /sbin/hotplug user
      fi
    fi
  else
    if [ "$STATE" != "away" ]; then
      STATE="away"
      export ACTION="unregister"
      /sbin/hotplug user
    fi
  fi
  sleep 5m
done
