Skip to content

Instantly share code, notes, and snippets.

@AndreasBackx
Last active February 3, 2017 09:42
Show Gist options
  • Save AndreasBackx/a910d11ad86be5e1154c to your computer and use it in GitHub Desktop.
Save AndreasBackx/a910d11ad86be5e1154c to your computer and use it in GitHub Desktop.
Connect to your Android device over your LAN. Asks to plug in so it can enable tcpip mode on adb. Requires adb to be in PATH.
connectAndroid() {
port=5555
host=192.168.1.99
wifiConnected=$(adb devices | grep "$host:$port\tdevice")
if [ -n "$wifiConnected" ]; then
echo "Your device is already connected via port $port."
else
wifiStartConnectionCommand="adb connect $host | grep 'connected to $host:$port'"
wifiStartConnection=$(eval $wifiStartConnectionCommand)
if [ -n "$wifiStartConnection" ]; then
echo "Sucessfully connected to your device via port $port."
else
usbConnected=`adb devices | grep '\tdevice'`
if [ -n "$usbConnected" ]; then
portopen=`adb tcpip $port > /dev/null`
wifiStartConnection=-$(eval $wifiStartConnectionCommand)
if [ -n "$wifiStartConnection" ]; then
echo "Successfully connected to your device after opening port $port."
else
echo "Could not connect to your device even after opening port $port."
fi
else
echo "Please connect your device so I can open port $port."
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment