Skip to content

Instantly share code, notes, and snippets.

@davethomas11
Last active October 4, 2017 16:34
Show Gist options
  • Save davethomas11/19ebb3b28e6c6ef91a7a29ba7e577eba to your computer and use it in GitHub Desktop.
Save davethomas11/19ebb3b28e6c6ef91a7a29ba7e577eba to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# ADB Connect android device for building via wifi
# Based on stack over flow answer:
# http://stackoverflow.com/questions/4893953/run-install-debug-android-applications-over-wi-fi
#
# Usage:
#
# 1. Make sure adb command is in executable path
#
# 2. Connect device by USB and run:
# adb_wifi
#
# 3. To disconnect run:
# adb_wifi -s
#
#
# 4. Optional to make this command executable anywhere
# - make script executable: chmod u+x adb_wifi
# - put script in executable path
#
#
# Optional usage:
#
# if connection disconnects due to device sleep,
# then we can reconnect wirelessly using cached ip address
#
# Connect to last used ip ( in last_adb_wifit.txt ):
# adb_wifi -l
#
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cache="$DIR/last_adb_wifi.txt"
PORT=6767
stop=false
# Parse command options
while getopts sl opt; do
case $opt in
s)
connected=$(adb devices -l | grep -E "[0-9\.:]{12,}" -o)
if [ $? -gt 0 ]; then
echo "No wifi connected devices"
exit 1
fi
adb -s $connected usb
exit
;;
l)
if [ -e "$cache" ]; then
adb connect $(cat $cache); exit
else
echo "No cached IP address"
exit 1
fi
esac
done
adb -d shell echo "test" > /dev/null
if [ $? -gt 0 ]; then
echo "Connect device to USB"
exit 1
fi
ipaddress=$(adb -d shell ifconfig | grep -E "inet addr:[0-9\.]+" -o | sed -e 's/inet addr.\(.*\)/\1/' -e '/127.0.0.1/d')
adb -d tcpip $PORT
adb connect ${ipaddress}:$PORT
echo ${ipaddress}:$PORT > "$cache"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment