Skip to content

Instantly share code, notes, and snippets.

@djfm
Created July 27, 2021 13:52
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 djfm/b3e6fb76f644b3eed49829b771937613 to your computer and use it in GitHub Desktop.
Save djfm/b3e6fb76f644b3eed49829b771937613 to your computer and use it in GitHub Desktop.
a very simple bash script that helps connect an Android phone to adb when the USB connection is flaky
#!/usr/bin/env bash
nDevices="0"
nTries="0"
echo "Looking for connected Android devices..."
echo ""
while [ $nDevices -lt 1 ]
do
nDevices="$(($(adb devices | sed '/^\s*$/d' | wc -l)-1))"
if [ $nDevices -eq 0 ]
then
echo "No Android devices found yet,"
echo "make sure the USB cable(s) is / are plugged correctly,"
echo "that the developer options are enabled on the device(s),"
echo "that USB debugging is enabled on the device(s)"
echo "within the developer options you have unlocked,"
echo "and that you accept the authorization request to allow"
echo "your computer to debug the device(s) - it will hopefully"
echo "pop on the device(s) once they are detected."
echo ""
echo "If all fails, try different USB ports,"
echo "then different USB cables..."
echo ""
nTries=$(($nTries + 1))
echo "Sleeping a bit in order not to kill the CPU."
echo "This was attempt number #$nTries"
sleep 1
echo "let's go again..."
echo ""
fi
done
if [ $nDevices -eq 1 ]
then
echo "Yay, found 1 Android device!"
elif [ $nDevices -gt 1 ]
then
echo "Wow, found $nDevices Android devices!"
fi
echo ""
echo "Now enabling adb debugging over WiFi, which is"
echo "way, way more convenient."
adb tcpip 5555
echo ""
echo "Now all you have to do to start a remote debugging session"
echo "is to UNPLUG YOUR USB CABLE then"
echo "write 'adb connect <ip>:5555' in a terminal,"
echo "where <ip> is the IP of your phone on your local network."
echo ""
echo "The phone will forget about this setup every once in a while,"
echo "so just re-run this tool."
echo ""
echo "Now UNPLUG USB CABLE"
echo "And happy debugging!"
# this is the static IP of my phone on my own wifi,
# customize to your needs!
sleep 5
adb connect "192.168.0.31:5555"
@djfm
Copy link
Author

djfm commented Jul 27, 2021

Yeah, last time it took 101 tries to establish the connection... glad I'm not doing it manually.

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