Skip to content

Instantly share code, notes, and snippets.

@dtmilano
Last active November 29, 2023 09:29
Show Gist options
  • Save dtmilano/4537110 to your computer and use it in GitHub Desktop.
Save dtmilano/4537110 to your computer and use it in GitHub Desktop.
Script to select one connected device or emulator when running adb
#! /bin/bash
#=====================================================================
# Selects an android device
# Copyright (C) 2012-2022 Diego Torres Milano. All rights reserved.
#
# The simplest way to invoke this script is creating a function like
# this one in your shell startup file:
#
# ```
# adb ()
# {
# command adb $(android-select-device "$@") "$@"
# }
#
# ```
#
# See:
# - http://dtmilano.blogspot.ca/2013/01/android-select-device.html
# - http://dtmilano.blogspot.ca/2012/03/selecting-adb-device.html
# for details on usage.
#=====================================================================
get_adb_devices() {
command adb $ADB_SERVER devices $LONG 2>&1 | tail -n +2 | sed '/^$/d'
}
git_pull() {
[[ "$UNAME" == 'Linux' ]] && OPT=-e
ASD=$(readlink $OPT $0)
if [[ -n "$ASD" ]]
then
DIR=$(dirname $ASD)
if [[ -n "$DIR" && -d "$DIR/.git" ]]
then
(cd $DIR && git pull --no-rebase)
fi
fi
}
PROGNAME=$(basename $0)
VERSION="3.6.0"
UNAME=$(uname)
DEVICE_OPT=
LONG=
ADB_SERVER=
git_pull > /dev/null
for ((i=1; i<=$#; ++i))
do
opt="${@:i:1}"
case "$opt" in
-d|-e|-s|-t)
DEVICE_OPT=$opt
;;
-l|--long)
LONG=-l
;;
start-server|kill-server|connect|pair|devices|-help)
exit 0
;;
forward)
if [[ "${@:i+1:1}" == '--list' ]]
then
exit 0
fi
;;
-V|--version)
echo "$PROGNAME version $VERSION"
exit 0
;;
esac
done
[ -n "$DEVICE_OPT" ] && exit 0
DEV=$(get_adb_devices)
if [ -z "$DEV" ]
then
echo "$PROGNAME: ERROR: There's no locally connected devices." >&2
read -p "Do you want to connect to a remote adb server? [Y/n]: " REPLY
case "$REPLY" in
n|N)
exit 1
;;
y|Y|"")
read -p "ADB server IP: " IP
ADB_SERVER="-H $IP"
DEV=$(get_adb_devices)
;;
esac
elif grep -q 'daemon started successfully' <<<"$DEV"
then
# try again
DEV=$(get_adb_devices)
fi
N=$(wc -l <<<"$DEV" | sed 's/ //g')
case "$N" in
1)
# only one device detected
D=$DEV
;;
*)
# more than one device detected
OLDIFS=$IFS
IFS="
"
PS3="Select the device to use, <Q> to quit: "
select D in $DEV
do
[ "$REPLY" = 'q' -o "$REPLY" = 'Q' ] && exit 2
[ -n "$D" ] && break
done < /dev/tty
IFS=$OLDIFS
;;
esac
if [ -z "$D" ]
then
echo "$PROGNAME: ERROR: target device couldn't be determined" >&2
exit 1
fi
# this didn't work on Darwin
# echo "-s ${D%% *}"
printf -- '-s %s\n' "$(echo ${D} | sed 's/ .*$//')"
@KANGOD
Copy link

KANGOD commented Jun 5, 2018

Thanks! For me, it doesn't print 1), but 2) and 3) just works well.

➤ adb $(android-select-device.sh) shell dumpsys window | grep mCurrentFocus
  3) 23456789ABCDEF     device
2) xxxxxxxxxx   device
Select the device to use, <Q> to quit: 2
  mCurrentFocus=Window{xxxxxxxxxxxxxxxxxxxxxxxxxxx}

BTW, alias adbs="adb \$(/drives/d/tools/bin/not_exported/android-select-device.sh)" is cool.

@ivasandom
Copy link

I downloaded the file and put it into app-debug dir. I changed alias from adb to android-select-device but when I try to execute any adb command this message appears: bash: android-select-device: command not found. What could it be?

@dtmilano
Copy link
Author

If you execute /path/to/android-select-device.sh directly what's the result?

@ivasandom
Copy link

My fault. I put the path to android-select-device wrong. Thank you.

@KenLee12323
Copy link

Sorry, but how to find the adb ip? I tried serval ip addresses like 127.0.0.1 and the address from 'adb shell ip -f inet addr show wlan0'. but none of these work. Thanks a lot

@dtmilano
Copy link
Author

If the device is connected through adb over wifi as described at https://developer.android.com/studio/command-line/adb#connect-to-a-device-over-wi-fi-android-11+, it will show up in the list with its IP or hostname.
If this is not the case then you should have to review your settings and follow the steps provided in the page.

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