Controlling Android Tablet Screen with OpenHAB and ADB

Wouldn’t it be cool to control an android device’s wake and sleep from OpenHAB? I’ve strapped a couple tablets to the wall with command strips. I’m using them to control OpenHAB. Gotta get ’em working well enough to gain wife approval before doing anything more permanent.

I was griping about turning my tablet screens on and off in response to sensor triggers in my home automation system. My first attempt using Tasker to poll the OpenHAB Rest API was too laggy to be useful. I wasn’t making much progress when my buddy over at https://opsech.io told me about adb and sending keyevents using adb shell. He ninja’d up tabletcontrol.sh and it works beautifully. Following his example, I munged out gettabstatus.sh and it works well enough.

Requirements-Tablet:
Rooted Android device
WiFi ADB – Debug Over Air
NOTE: This app works on ROOTED devices only.

Requirements-Server:
OpenHAB Server & exec binding, tested on 1.8
linux/bash, tested on Ubuntu Server 14.04 LTS
android tools ADB
[text]sudo apt-get install android-tools-adb[/text]

OpenHAB Items
[text]Switch LaundryTablet "Laundry Tablet" (gDN) { exec=">[ON:/bin/sh@@-c@@/usr/local/bin/tabletcontrol.sh 192.168.1.120 on] >[OFF:/bin/sh@@-c@@/usr/local/bin/tabletcontrol.sh 192.168.1.120 off]" }
String LaundryTabletStatus "Laundry Tablet Status [%s]" (gDN) { exec="<[/bin/sh@@-c@@/usr/local/bin/gettabstatus.sh 192.168.1.120:15000:]" }

Switch Tablet1Control "Tablet 1" (gDN) { exec=">[ON:/bin/sh@@-c@@/usr/local/bin/tabletcontrol.sh 192.168.1.121 on] >[OFF:/bin/sh@@-c@@/usr/local/bin/tabletcontrol.sh 192.168.1.121 off]" }
Contact Tablet1Status "Tablet1 Status" (gDN) { exec="<[/bin/sh@@-c@@/usr/local/bin/gettabstatus.sh 192.168.1.121:15000:]" }[/text]

Installation:
Copy the scripts onto your server, I chose to use /usr/local/bin
Make scripts readable and executable to the same user that’s running OpenHAB, chmod 755 is usually a safe bet
Install and run WiFi ADB, allowing root permissions
Install android adb
Create OpenHAB items, reference OpenHAB Exec Binding wiki

Description:
tabletcontrol.sh wakes or sleeps the android device
gettabstatus.sh returns OPEN, CLOSED, or –
OPEN is screen on
CLOSED is screen off
– is unknown

Usage:
tabletcontrol.sh IP_Address DesiredState(ON/OFF)
tabletcontrol.sh 192.168.1.120 off

gettabstatus.sh IP_Address
gettabstatus.sh 192.168.1.120

/usr/local/bin/tabletcontrol.sh
[bash]
#!/bin/bash
set -x
DEV="$1:5555"

adb_connect() {
adb connect "$1" | grep -q ‘already connected to’
}

get_screen_status() {
local status
status="$( adb -s "$1" shell "dumpsys power | sed -r ‘/Display Power/!d;s/.*=(O[FN]F?)/1/’" | tr -dc ‘[:alpha:]’ )"
if [[ "$status" == "ON" ]]; then
return 0
else
return 1
fi
}

toggle_screen() {
adb -s "$1" shell input keyevent 26
}

echo_already() {
echo " $1 Screen is already ${2}."
}

echo_toggle() {
local a="on"
[[ "$2" == "on" ]] && a="off"
echo " $1 Screen is ${a}, turning ${2}."
}

set_screen() {
if get_screen_status "$1"; then # if screen is on
if [[ "$2" == "off" ]]; then # but we want it off
echo_toggle "$DEV" "$2"
toggle_screen "$DEV"
else
echo_already "$DEV" "$2"
fi
else # if screen is off
if [[ "$2" == "on" ]]; then # but we want it on
echo_toggle "$DEV" "$2"
toggle_screen "$DEV"
else
echo_already "$DEV" "$2"
fi
fi
}

if ! adb_connect "$DEV"; then
echo "Hold up a second."
sleep 1
fi

shift
case "$@" in
[Oo][Nn]*)
set_screen "$DEV" "on"
;;
[Oo][Ff][Ff]*)
set_screen "$DEV" "off"
;;
esac
[/bash]

/usr/local/bin/gettabstatus.sh
[bash]
#!/bin/bash
DEV="$1:5555"

adb_connect() {
adb connect "$1" | grep -q ‘already connected to’
}

adb_verify() {
adb -s "$1" shell ls 2>&1 | grep -q ‘error’
}

get_screen_status() {
local status
status="$( adb -s "$DEV" shell "dumpsys power | sed -r ‘/Display Power/!d;s/.*=(O[FN]F?)/1/’" 2>>/dev/null | tr -dc ‘[:alpha:]’ )"
if [[ "$status" == "ON" ]]; then
return 0
else
return 1
fi
}

if ! adb_connect "$DEV"; then
sleep 1
fi

if adb_verify "$DEV"; then
adb kill-server
sleep 1
if ! adb_connect "$DEV"; then
sleep 1
fi
if adb_verify "$DEV"; then
echo "-"
exit 1
fi
fi

if get_screen_status "$1"; then
echo "OPEN"
else
echo "CLOSED"
fi
[/bash]

Leave a comment