Skip to content

Instantly share code, notes, and snippets.

@nothingrealhappen
Created March 20, 2026 03:39
Show Gist options
  • Select an option

  • Save nothingrealhappen/2f5bb3ad772c78750f36c0d9366f1ca2 to your computer and use it in GitHub Desktop.

Select an option

Save nothingrealhappen/2f5bb3ad772c78750f36c0d9366f1ca2 to your computer and use it in GitHub Desktop.
Adb auto connect via wireless debugging(without USB cable) for android 11+
#!/bin/bash
# Auto-connect to Android device and optionally launch scrcpy
# Usage: adb-auto-connect [--scrcpy]
DEVICE_IP="192.168.1.xxx"
PORTS=(5555 38000 39000 40000 41000 42000 43000 44000 45000)
SCRCPY=false
if [[ "${1:-}" == "--scrcpy" ]]; then
SCRCPY=true
fi
# Check if already connected
if adb devices 2>/dev/null | grep -q "$DEVICE_IP"; then
CONNECTED=$(adb devices | grep "$DEVICE_IP" | awk '{print $1}')
echo "Already connected: $CONNECTED"
if $SCRCPY; then
exec scrcpy -s "$CONNECTED"
fi
exit 0
fi
echo "Trying ${DEVICE_IP}..."
# Try port 5555 first (adb tcpip mode)
if nc -z -w1 "$DEVICE_IP" 5555 2>/dev/null; then
echo "Connecting to ${DEVICE_IP}:5555..."
adb connect "${DEVICE_IP}:5555"
if $SCRCPY; then
exec scrcpy -s "${DEVICE_IP}:5555"
fi
exit 0
fi
# Scan for wireless debugging port (Android 11+ uses random high ports)
echo "Port 5555 not available. Scanning for wireless debugging port..."
PORT=$(adb mdns services 2>/dev/null | grep "adb-tls-connect" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+' | head -1)
if [[ -n "$PORT" ]]; then
echo "Found via adb mdns: $PORT"
adb connect "$PORT"
if $SCRCPY; then
exec scrcpy -s "$PORT"
fi
exit 0
fi
# Brute scan common high port range for wireless debugging
echo "Scanning high ports on ${DEVICE_IP}..."
FOUND_PORT=""
for port in $(seq 37000 45000); do
if nc -z -w0 "$DEVICE_IP" "$port" 2>/dev/null; then
FOUND_PORT="$port"
break
fi
done
if [[ -n "$FOUND_PORT" ]]; then
echo "Found open port: ${DEVICE_IP}:${FOUND_PORT}"
adb connect "${DEVICE_IP}:${FOUND_PORT}"
if $SCRCPY; then
exec scrcpy -s "${DEVICE_IP}:${FOUND_PORT}"
fi
exit 0
fi
echo "No ADB port found on ${DEVICE_IP}."
echo "Make sure either:"
echo " - 'Wireless debugging' is ON (Developer Options), or"
echo " - Run 'adb tcpip 5555' while USB is connected"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment