Advertisement
Guest User

bt-sleep-monitor.sh

a guest
Feb 3rd, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## VARIABLES
  4.  
  5. # This value defines the default wakeup time (in seconds) when 'dispaly on' is triggered
  6. btactive=120
  7. # Default names for lock/log files
  8. lockfile=/dev/shm/bt-sleep-monitor.lock
  9. logfile=/dev/shm/bt-sleep-monitor.log
  10.  
  11.  
  12. ## PRESETS
  13.  
  14. # Create lock/log file
  15. echo "0" > $lockfile
  16. echo "$(date) log started" > $logfile
  17. echo "1" > /proc/bluetooth/sleep/lpm
  18. # Make logfile readable for anyone
  19. chmod 777 $logfile
  20.  
  21.  
  22. ## FUNCTIONS
  23.  
  24. # Wakup Bluetooth (disable 'sleep mode' and wait for connection) / Go to 'sleep mode' again
  25. wakeup() {
  26. # We check if the script is in 'locked' state
  27. if [ "$(cat $lockfile)" == "0" ] ; then
  28. # We wake up bluetooth for certain time and set a 'lock' until we go in 'sleep mode' again.
  29. echo "$(date) Bluetooth wakeup for 2 minutes" >> $logfile
  30. echo "0" > /proc/bluetooth/sleep/lpm
  31. echo "1" > $lockfile
  32. sleep $btactive
  33. # We check if a device is connetced. Otherwise we go to sleep and release the 'lock'.
  34. if [ "$(hcitool con | grep \>)" == "" ] ; then
  35. echo "1" > /proc/bluetooth/sleep/lpm
  36. echo "0" > $lockfile
  37. echo "$(date) Sleep after active and release lock" >> $logfile
  38. # Call listener again
  39. killall dbus-monitor
  40. echo $(defaultlistener)
  41. else
  42. # We have a connection. If device disconnects, we go to sleep and release the 'lock'.
  43. echo "$(date) Device connected" >> $logfile
  44. dbus-monitor --system "interface='org.bluez.Control',member='PropertyChanged'" |
  45. while read -r line; do
  46. if [ "$(echo $line | awk 'END {print $NF}')" == "false" ]; then
  47. echo "1" > /proc/bluetooth/sleep/lpm
  48. echo "0" > $lockfile
  49. echo "$(date) Device disconnected" >> $logfile
  50. # Call listener again
  51. killall dbus-monitor
  52. echo $(defaultlistener)
  53. fi
  54. done
  55. fi
  56. fi
  57. }
  58.  
  59. # Main listener (always started again after bluetooth is goes into 'sleep mode').
  60. defaultlistener() {
  61. # We listen for 'Disply on' DBus events.
  62. dbus-monitor --system "interface='com.nokia.mce.signal',member='display_status_ind'" |
  63. while read -r line; do
  64. if [ "$(echo $line | awk -F\" '{print $2}')" == "on" ]; then
  65. # Call wakeup function
  66. killall dbus-monitor
  67. echo $(wakeup)
  68. fi
  69. done
  70. }
  71.  
  72.  
  73. ## MAIN (start defaultlistener)
  74. defaultlistener
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement