Skip to content

Instantly share code, notes, and snippets.

@woutervanwijk
Last active April 5, 2025 21:29
Show Gist options
  • Save woutervanwijk/71c9d36cf38544c99f4b5399ca80fea3 to your computer and use it in GitHub Desktop.
Save woutervanwijk/71c9d36cf38544c99f4b5399ca80fea3 to your computer and use it in GitHub Desktop.
auto connect to Android wireless debugging port and save it for the next time (for MacOS)
#!/bin/bash -x
# ip (static) for Android
# ANDROID_DEVICE=192.168.1.45
# ANDROID_DEVICE=192.168.1.85
FILE=~/.adb_port
ANDROID_DEVICE=192.168.1.45
#first test old port
PORT=$(cat "$FILE")
if [[ "$PORT" = "" ]]; then
PORT="30001"
fi
echo "\nRestored port: $PORT\n"
ADB=$(adb connect $ANDROID_DEVICE:$PORT)
if [[ "$ADB" =~ connected ]]; then
echo "Already connected to wireless ADB on $ANDROID_DEVICE:$PORT\n"
exit 0
fi
# start scan
echo "Not connected. Scanning $ANDROID_DEVICE\n"
NMAP=$(nmap -sT $ANDROID_DEVICE -p30001-49999)
echo NMAP result:"\n$NMAP"
PORT=$(echo "$NMAP" | awk -F/ '/tcp open/{print $1}')
echo "Found port: $PORT. Connecting to $ANDROID_DEVICE:$PORT\n"
echo "$PORT" > $FILE
adb connect $ANDROID_DEVICE:$PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment