Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imjyotiraditya/54e0187287edf7da9b538dfd64fa9e55 to your computer and use it in GitHub Desktop.
Save imjyotiraditya/54e0187287edf7da9b538dfd64fa9e55 to your computer and use it in GitHub Desktop.
function nconn() {
local ADB_LAST_IP_FILE="$HOME/.adb_last_known_ip"
if adb devices | grep -q ":5555\s*offline"; then
echo "Device offline. Attempting to reconnect..."
adb tcpip 5555 && sleep 1 && adb connect "$(cat "$ADB_LAST_IP_FILE"):5555"
echo "Reconnection failed. Ensure the device is on the same network."
return
elif adb devices | grep -q ":5555\s*device"; then
echo "Device already connected over Wi-Fi."
return
fi
local ip=$(adb shell "ip -4 addr show wlan0" | grep -oP "(?<=inet\s)\d+(\.\d+){3}")
if [ -z "$ip" ]; then
echo "Failed to obtain IP address. Is the device connected via USB?"
return 1
fi
echo "$ip" >| "$ADB_LAST_IP_FILE"
adb tcpip 5555 && sleep 1 && adb connect "$ip:5555"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment