Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Created April 25, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikedamage/a9db73102d47bdc15134c752c5704165 to your computer and use it in GitHub Desktop.
Save mikedamage/a9db73102d47bdc15134c752c5704165 to your computer and use it in GitHub Desktop.
Control an Android phone over USB and toggle tethering
typeset -A keys
keys=(
power 26
home 3
up 19
down 20
left 21
right 22
enter 66
)
function key() {
adb shell input keyevent "${keys[$1]}"
}
function swipe-up() {
adb shell input swipe 0 1000 0 0
}
function unlock() {
key power
swipe-up
}
function lock() {
key power
}
function tethering-enabled() {
if adb shell getprop sys.usb.config | grep rndis > /dev/null; then
return 0
else
return 1
fi
}
function open-tethering() {
adb shell am start -n com.android.settings/.TetherSettings
}
function toggle-tethering() {
echo -n "Toggling USB tethering status..."
unlock
open-tethering
key up
key down
key down
key down
key enter
sleep 2
key home
lock
echo "Done."
}
function enable-tethering() {
if tethering-enabled; then
echo "USB tethering already enabled"
return 0
fi
toggle-tethering
}
function disable-tethering() {
if ! tethering-enabled; then
echo "USB tethering already disabled"
return 0
fi
toggle-tethering
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment