Skip to content

Instantly share code, notes, and snippets.

@djeraseit
Forked from mikedamage/usb-phone-control.zsh
Created September 7, 2024 23:35
Show Gist options
  • Save djeraseit/e53273b62fb74aaf3e1527389adab200 to your computer and use it in GitHub Desktop.
Save djeraseit/e53273b62fb74aaf3e1527389adab200 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