Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## VARIABLES
- # This value defines the default wakeup time (in seconds) when 'dispaly on' is triggered
- btactive=120
- # Default names for lock/log files
- lockfile=/dev/shm/bt-sleep-monitor.lock
- logfile=/dev/shm/bt-sleep-monitor.log
- ## PRESETS
- # Create lock/log file
- echo "0" > $lockfile
- echo "$(date) log started" > $logfile
- echo "1" > /proc/bluetooth/sleep/lpm
- # Make logfile readable for anyone
- chmod 777 $logfile
- ## FUNCTIONS
- # Wakup Bluetooth (disable 'sleep mode' and wait for connection) / Go to 'sleep mode' again
- wakeup() {
- # We check if the script is in 'locked' state
- if [ "$(cat $lockfile)" == "0" ] ; then
- # We wake up bluetooth for certain time and set a 'lock' until we go in 'sleep mode' again.
- echo "$(date) Bluetooth wakeup for 2 minutes" >> $logfile
- echo "0" > /proc/bluetooth/sleep/lpm
- echo "1" > $lockfile
- sleep $btactive
- # We check if a device is connetced. Otherwise we go to sleep and release the 'lock'.
- if [ "$(hcitool con | grep \>)" == "" ] ; then
- echo "1" > /proc/bluetooth/sleep/lpm
- echo "0" > $lockfile
- echo "$(date) Sleep after active and release lock" >> $logfile
- # Call listener again
- killall dbus-monitor
- echo $(defaultlistener)
- else
- # We have a connection. If device disconnects, we go to sleep and release the 'lock'.
- echo "$(date) Device connected" >> $logfile
- dbus-monitor --system "interface='org.bluez.Control',member='PropertyChanged'" |
- while read -r line; do
- if [ "$(echo $line | awk 'END {print $NF}')" == "false" ]; then
- echo "1" > /proc/bluetooth/sleep/lpm
- echo "0" > $lockfile
- echo "$(date) Device disconnected" >> $logfile
- # Call listener again
- killall dbus-monitor
- echo $(defaultlistener)
- fi
- done
- fi
- fi
- }
- # Main listener (always started again after bluetooth is goes into 'sleep mode').
- defaultlistener() {
- # We listen for 'Disply on' DBus events.
- dbus-monitor --system "interface='com.nokia.mce.signal',member='display_status_ind'" |
- while read -r line; do
- if [ "$(echo $line | awk -F\" '{print $2}')" == "on" ]; then
- # Call wakeup function
- killall dbus-monitor
- echo $(wakeup)
- fi
- done
- }
- ## MAIN (start defaultlistener)
- defaultlistener
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement