blueproximity 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/bin/bash
  2. #set -o verbose sh -v
  3. # Copied from Steven on http://gentoo-wiki.com/Talk:TIP_Bluetooth_Proximity_Monitor
  4. # Modified By Jamie Paton
  5. # Modified By Michele Marcucci http://www.michelem.org
  6. # Modified By Mone http://www.simonefabiano.com
  7. # Modified by ikko http://1kko.com https://github.com/1kko/blueproximity
  8. CONFIG_FILE="$HOME/.blueproximity/blueproximity.conf"
  9. HCITOOL="/usr/bin/hcitool"
  10. RFCOMM="/usr/bin/rfcomm"
  11. L2PING="/usr/bin/l2ping"
  12. if [ ! -f "$CONFIG_FILE" ]; then
  13. [ -d `dirname $CONFIG_FILE` ] || mkdir `dirname $CONFIG_FILE`
  14. echo "Creating Default Profile..."
  15. hcitool scan
  16. read -p "Please Enter your Device's Bluetooth MAC Address: " device_mac_addr
  17. #Mac Address Validation from http://raamdev.net/2009/mac-address-validation-regex-with-egrep/
  18. if [[ -z `echo $device_mac_addr | egrep "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"` ]]; then
  19. echo "Invalid Bluetooth MAC Address"
  20. fi
  21. # Do NOT EDIT following line
  22. # Instead edit $CONFIG_FILE
  23. cat > $CONFIG_FILE << CEOF
  24. # Blueproximity Config File.
  25. # https://github.com/1kko/blueproximity
  26. # Set level of debuglog.
  27. # 0: no log, 1: log to file, 2: log to both file and stdout
  28. DEBUG_LEVEL=1
  29. # Debug logs will be stored into following file.
  30. DEBUG="/tmp/btproximity.log"
  31. # You'll need to use the MAC address of your phone here
  32. # Use "hcitool scan" to find the MAC of your device
  33. DEVICE="$device_mac_addr"
  34. # How often to check the distance between phone and computer in seconds
  35. # When it's far it will check interval of \$CHECK_INTERVAL_FAR seconds,
  36. # and if it's near it will check \$CHECK_INTERVAL_NEAR seconds.
  37. # because recessive ping will drain your phone's battery.
  38. CHECK_INTERVAL_FAR=5
  39. CHECK_INTERVAL_NEAR=60
  40. # The RSSI threshold at which a phone is considered far or near
  41. THRESHOLD=-15
  42. # Number of failed connections before declaring the device "far"
  43. FAILURES_THRESHOLD=2
  44. # Connection method can be used : [rfcomm|hcitool]. default "rfcomm"
  45. CONNECTION_METHOD=rfcomm
  46. # Remote PC IP Address.
  47. # Requires ssh key authorized previously using ssh-keygen
  48. # Reference: https://help.ubuntu.com/community/SSH/OpenSSH/Keys
  49. # Comment out if you don't need one
  50. #REMOTE_PC=192.168.0.1
  51. CEOF
  52. echo "your config is stored to $CONFIG_FILE"
  53. echo "to check out configuration, type:"
  54. echo " cat $CONFIG_FILE"
  55. echo ""
  56. echo "Configuration Finished"
  57. echo "now you can edit or run this script again in background."
  58. exit 0
  59. fi
  60. source $CONFIG_FILE
  61. CHECK_INTERVAL=$CHECK_INTERVAL_FAR
  62. connected=0
  63. state="near"
  64. failures=0
  65. # The commands to run when your phone gets too far away
  66. function far_execution {
  67. #lock the pc
  68. sflock
  69. #lock the remote pc
  70. # [ $REMOTE_PC ] && ssh $USER@$REMOTE_PC DISPLAY=:0 gnome-screensaver-command -l
  71. }
  72. # The command to run when your phone is close again
  73. function near_execution {
  74. #show the login window
  75. #/opt/gnome/bin/gnome-screensaver-command --poke > /dev/null 2>&1
  76. #unlock the pc
  77. killall sflock
  78. #unlock the remote pc
  79. # [ $REMOTE_PC ] && ssh $USER@$REMOTE_PC DISPLAY=:0 gnome-screensaver-command -d
  80. #FIXME: Redirect Phone's Bluetooth Audio to PC
  81. # currently it doesn't seem to work when using rfcomm method.
  82. # reference: http://askubuntu.com/questions/2573/can-i-use-my-computer-as-an-a2dp-receiver/109533#109533
  83. # and also : http://blog.cyphermox.net/2012/03/call-for-testing-bluez-a2dp-and-hsphfp.html
  84. #local BTSOURCE=\`pactl list short sources | grep bluez_source | awk '{ print \$2; }'\`
  85. #local SINK=\`pactl list short sinks | grep -v Monitor | grep alsa_output.pci | awk '{ print \$2; }'\`
  86. #pactl load-module module-loopback source=\$BTSOURCE sink=\$SINK
  87. }
  88. # The command to run when your phone is near.
  89. #function proximity_execution {
  90. # usually used for simulating you are near.
  91. # msg "Simulating User Activity"
  92. # gnome-screensaver-command -d
  93. # following line came from http://chadarius.com/node/153
  94. # dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
  95. # [ $REMOTE_PC ] && ssh $USER@$REMOTE_PC DISPLAY=:0 gnome-screensaver-command -d
  96. # [ $REMOTE_PC ] && ssh $USER@$REMOTE_PC DISPLAY=:0 dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
  97. #}
  98. function msg {
  99. case $DEBUG_LEVEL in
  100. 1)
  101. # only to file
  102. echo "$@" >> "$DEBUG"
  103. ;;
  104. 2)
  105. # both file and stdout
  106. echo "$@" | tee "$DEBUG"
  107. ;;
  108. 0|*)
  109. # no debug
  110. echo -n ""
  111. ;;
  112. esac
  113. }
  114. function check_connection {
  115. connected=0;
  116. found=0
  117. for s in `$HCITOOL con`; do
  118. if [[ `echo $s |grep $DEVICE` ]]; then
  119. found=1;
  120. fi
  121. done
  122. if [[ $found == 1 ]]; then
  123. connected=1;
  124. else
  125. msg 'Attempting connection...'
  126. case $CONNECTION_METHOD in
  127. hcitool)
  128. $HCITOOL cc $DEVICE
  129. CONNECTION_RESULT="`$HCITOOL con | grep $DEVICE`"
  130. ;;
  131. rfcomm|*)
  132. setsid $RFCOMM connect 0 $DEVICE 1&
  133. sleep 6
  134. CONNECTION_RESULT="`$RFCOMM show * | grep $DEVICE | grep connect`"
  135. ;;
  136. esac
  137. if [ -n "$CONNECTION_RESULT" ]; then
  138. msg 'Connected'
  139. connected=1;
  140. else
  141. if [ -z "`$L2PING -c 1 -t 2 -i $DEVICE 2>&1`" ]; then
  142. if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
  143. msg 'Ping is good!'
  144. connected=1;
  145. else
  146. msg "ERROR: Could not connect to device $DEVICE."
  147. connected=0;
  148. fi
  149. fi
  150. fi
  151. fi
  152. }
  153. function is_far {
  154. if [[ "$state" == "near" ]]; then
  155. let "failures += 1"
  156. msg "*** Device \"$dev_name\" [$DEVICE] has left proximity, failures = $failures"
  157. if [[ $failures -ge FAILURES_THRESHOLD ]]; then
  158. #i had problems because sometimes the connection fails even if the mobile is near. Let the connection fail more times before declaring the mobile as "far"
  159. state="far"
  160. far_execution
  161. fi
  162. CHECK_INTERVAL=$CHECK_INTERVAL_FAR
  163. msg "Current Interval=$CHECK_INTERVAL"
  164. fi
  165. }
  166. function is_near {
  167. #reset the number of failures
  168. failures=0
  169. if [[ "$state" == "far" ]]; then
  170. msg "*** Device \"$dev_name\" [$DEVICE] is within proximity"
  171. state="near"
  172. near_execution
  173. fi
  174. CHECK_INTERVAL=$CHECK_INTERVAL_NEAR
  175. msg "Current Interval=$CHECK_INTERVAL"
  176. proximity_execution
  177. }
  178. #don't start the lock-unlock mechanism until we don't connect for the first time
  179. while [[ $connected -eq 0 ]]; do
  180. msg "init loop"
  181. check_connection
  182. sleep $CHECK_INTERVAL
  183. done
  184. dev_name=`$HCITOOL name $DEVICE`
  185. msg "Monitoring proximity of \"$dev_name\" [$DEVICE]";
  186. while /bin/true; do
  187. msg "execution loop"
  188. check_connection
  189. if [[ $connected -eq 1 ]]; then
  190. #we're connected
  191. rssi=$($HCITOOL rssi $DEVICE | sed -e 's/RSSI return value: //g')
  192. if [[ $rssi -le $THRESHOLD ]]; then
  193. #we're connected but far
  194. is_far
  195. else
  196. #we're connected and near
  197. is_near
  198. fi
  199. msg "state = $state, RSSI = $rssi"
  200. else
  201. #we're no more connected
  202. is_far
  203. msg "not connected"
  204. fi
  205. sleep $CHECK_INTERVAL
  206. done