Skip to content

Instantly share code, notes, and snippets.

@bartwell
Created January 26, 2025 10:58
Show Gist options
  • Save bartwell/05cb8f4e2a913229ea652fe1eed956e7 to your computer and use it in GitHub Desktop.
Save bartwell/05cb8f4e2a913229ea652fe1eed956e7 to your computer and use it in GitHub Desktop.
Script for quick connection using `adb pair` and `adb connect` on Mac
#!/bin/bash
show_help() {
cat <<EOF
Usage: $0 [IP_ADDRESS] [PAIR_PORT] [PAIR_CODE] [CONNECT_PORT] [--restart-server]
Arguments:
IP_ADDRESS Phone's IP address (e.g., 192.168.0.101)
PAIR_PORT Port for pairing (e.g., 46155)
PAIR_CODE Pairing code (e.g., 830119)
CONNECT_PORT Port for connection (e.g., 35963)
Options:
-h, --help Show this help message and exit
--restart-server Restart the ADB server before executing commands
EOF
}
get_param() {
local value="$1"
local prompt="$2"
if [ -z "$value" ]; then
read -p "$prompt" value
fi
echo "$value"
}
restart_adb_server() {
echo "Restarting ADB server..."
adb kill-server
adb start-server -a
echo "ADB server restarted."
}
# Check for help flag or restart-server flag
restart_flag=false
for arg in "$@"; do
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
show_help
exit 0
elif [[ "$arg" == "--restart-server" ]]; then
restart_flag=true
fi
done
# Restart the ADB server if the flag is set
if $restart_flag; then
restart_adb_server
fi
ip=$(get_param "$1" "Enter the phone's IP address (e.g., 192.168.0.101): ")
pair_port=$(get_param "$2" "Enter the pairing port (e.g., 46155): ")
pair_code=$(get_param "$3" "Enter the pairing code (e.g., 830119): ")
adb pair "$ip:$pair_port" "$pair_code"
if [ $? -eq 0 ]; then
echo "Pairing successful."
connect_port=$(get_param "$4" "Enter the connection port (e.g., 35963): ")
adb connect "$ip:$connect_port"
if [ $? -eq 0 ]; then
echo "Connection successful."
else
echo "Error during connection."
fi
else
echo "Error during pairing."
fi
@bartwell
Copy link
Author

Usage:

./ConnectWiFiAdb.sh 192.168.0.101
./ConnectWiFiAdb.sh 192.168.0.101 --restart-server
./ConnectWiFiAdb.sh -h

Go to Settings / Developer options / Wireless Debugging / Connect with code to obtain IP, pair port and another parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment