Skip to content

Instantly share code, notes, and snippets.

@Amareis
Created March 23, 2021 10:32
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 Amareis/a39e0fa39260a7d23abb7908fc295226 to your computer and use it in GitHub Desktop.
Save Amareis/a39e0fa39260a7d23abb7908fc295226 to your computer and use it in GitHub Desktop.
Simple bash script to switch connected Android phone to wi-fi adb mode and optionally forward port from computer to phone
#!/bin/bash
if ! command -v adb &> /dev/null
then
echo "adb is not globally installed, using android sdk version"
#you can just set your path to android platform tools
export PATH="$PATH:~/Android/Sdk/platform-tools"
fi
adb disconnect
adb usb
sleep 1
echo "Wait for USB device..."
adb wait-for-usb-device
adb devices
IP=$(adb shell ip route | cut -d' ' -f9)
echo "IP: $IP"
echo "Switch to tcpip"
adb tcpip 5555
echo "Connecting..."
while true; do
res=$(adb connect $IP | cut -d' ' -f1)
[[ "$res" == "connected" ]] && break
sleep 1
done
echo "Connected!"
PORT=$1
if [ -n "$PORT" ]
then
echo "Forward port $PORT"
sleep 2
adb -e reverse tcp:$PORT tcp:$PORT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment