Skip to content

Instantly share code, notes, and snippets.

@canabady
Created August 21, 2024 09:36
Show Gist options
  • Save canabady/310c73655c24f4e30b132a89e8f40f76 to your computer and use it in GitHub Desktop.
Save canabady/310c73655c24f4e30b132a89e8f40f76 to your computer and use it in GitHub Desktop.
Bash script to remote control Android TV from PC terminal
# /bin/bash
# TVC Android TV - control script via adb shell
__ADB_DEVICE="192.168.1.128:5555"
declare -A charcode=(
[A]='29'
[B]='30'
[C]='31'
[D]='32'
[E]='33'
[F]='34'
[G]='35'
[H]='36'
[I]='37'
[J]='38'
[K]='39'
[L]='40'
[M]='41'
[N]='42'
[O]='43'
[P]='44'
[Q]='45'
[R]='46'
[S]='47'
[T]='48'
[U]='49'
[V]='50'
[W]='51'
[X]='52'
[Y]='53'
[Z]='54'
[#]='62'
[0]='7'
[1]='8'
[2]='9'
[3]='10'
[4]='11'
[5]='12'
[6]='13'
[7]='14'
[8]='15'
[9]='16'
# [*]='17'
# [#]='18'
# [,]='55'
# [.]='56'
# [;]='74'
# [']='75'
# [/]='76'
# [-]='69'
# [=]='70'
# [+]='81'
)
if [ $# -eq 0 ]; then
echo "Usage: tv connect / disconnect"
echo " on / off"
echo " mute / unmute"
echo " play / pause"
echo " skip [+number] / [-number]"
echo " vol [+number] / [-number]"
echo " key [ok][enter][next][prev][up][down][left][right]"
echo " key [key_code]"
echo " search [text]"
exit -1;
fi
_action=$1
_param=$2
_argument=$3
if [ "$_action" == "connect" ]; then
adb connect "${__ADB_DEVICE}"
elif [ "$_action" == "disconnect" ]; then
adb disconnect
elif [ "$_action" == "on" ] || [ "$_action" == "off" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 26
elif [ "$_action" == "mute" ] || [ "$_action" == "unmute" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 164
elif [ "$_action" == "pause" ] || [ "$_action" == "play" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 85
elif [ "$_action" == "home" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 3
elif [ "$_action" == "end" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 6
elif [ "$_action" == "ok" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 23
elif [ "$_action" == "back" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 4
elif [ "$_action" == "enter" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 66
elif [ "$_action" == "browser" ]; then
adb -s "${__ADB_DEVICE}" shell input keyevent 64
elif [ "$_action" == "sunlife" ]; then
# Goto Sun Life channel
_keys_array=(home_2 enter sleep_5 enter sleep_5 enter back down enter sleep_5 right_5 enter)
for _key_seq in ${_keys_array[@]}; do
_key_array=(${_key_seq//_/ })
tv key "${_key_array[0]}" "${_key_array[1]}"
done
elif [ "$_action" == "adhitya" ]; then
# Goto Adhitya channel
_keys_array=(home_2 enter sleep_5 enter sleep_5 enter back down enter sleep_5 right_6 enter)
for _key_seq in ${_keys_array[@]}; do
_key_array=(${_key_seq//_/ })
tv key "${_key_array[0]}" "${_key_array[1]}"
done
elif [ "$_action" == "search" ]; then
# TODO search in smart-tube
tv home
tv home
tv key right
tv key enter
tv back
tv key up
tv enter
# Send search key
#adb -s "${__ADB_DEVICE}" shell input keyevent 84
# Replace space with ##
_param=${_param// /##}
read -a char_array <<< $(echo "${_param}" | sed 's/./& /g')
for char in "${char_array[@]}";
do
adb -s "${__ADB_DEVICE}" shell input keyevent ${charcode[${char^^}]};
done
# Send enter key
adb -s "${__ADB_DEVICE}" shell input keyevent 66
elif [ "$_action" == "key" ]; then
if [ "$_argument" != "" ]; then
_count=$(($_argument + 0))
else
_count=1
fi
if [ "$_param" == "enter" ]; then
_key_code=66
elif [ "$_param" == "home" ]; then
_key_code=3
elif [ "$_param" == "back" ]; then
_key_code=4
elif [ "$_param" == "ok" ]; then
_key_code=23
elif [ "$_param" == "next" ]; then
_key_code=87
elif [ "$_param" == "prev" ]; then
_key_code=88
elif [ "$_param" == "up" ]; then
_key_code=19
elif [ "$_param" == "down" ]; then
_key_code=20
elif [ "$_param" == "left" ]; then
_key_code=21
elif [ "$_param" == "right" ]; then
_key_code=22
else
_key_code="${_param}"
fi
if [ "$_param" == "sleep" ]; then
sleep "${_count}"
else
for _loop in $(seq 1 $_count); do
adb -s "${__ADB_DEVICE}" shell input keyevent "${_key_code}"
done
fi
elif [ "$_action" == "vol" ]; then
_param_length=${#_param}
if [ "$_param" != "" ]; then
_first_char=${_param:0:1}
_count=${_param:1:_param_length-1}
_count=$(($_count + 0))
if [ "$_first_char" == "-" ]; then
for _iter in $(seq 1 ${_count}); do
adb -s "${__ADB_DEVICE}" shell input keyevent 25
done
elif [ "$_first_char" == "+" ]; then
for _iter in $(seq 1 ${_count}); do
adb -s "${__ADB_DEVICE}" shell input keyevent 24
done
fi
fi
elif [ "$_action" == "skip" ]; then
_param_length=${#_param}
if [ "$_param" != "" ]; then
_first_char=${_param:0:1}
_count=${_param:1:_param_length-1}
_count=$(($_count + 0))
if [ "$_first_char" == "-" ]; then
for _iter in $(seq 1 ${_count}); do
adb -s "${__ADB_DEVICE}" shell input keyevent 89
done
elif [ "$_first_char" == "+" ]; then
for _iter in $(seq 1 ${_count}); do
adb -s "${__ADB_DEVICE}" shell input keyevent 90
done
fi
fi
else
echo "Invalid action"
exit -1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment