Last active
January 13, 2017 20:56
-
-
Save anton-novikau/ce6515527a70a88179bd3d20188ecfbe to your computer and use it in GitHub Desktop.
Bash script to obtain android device ids with human readable names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2016 Anton Novikau | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
set -e | |
get_info(){ | |
# run 'adb devices' to obtain all connected android devices | |
local devices=$(adb devices | grep -v offline | tail -n +2 | cut -sf 1) | |
for d in ${devices[*]}; do | |
# iterate over all connected devices and for each of them check if this is an emulator or real device. | |
# Some emulators have characteristics 'default' that doesn't identify them as emulators. | |
# In this case we try to check if qemu is active on such device (may not work properly for arm emulator). | |
local char=$(adb -s ${d} shell getprop 'ro.build.characteristics' | tr -d '\r') | |
local is_qemu=$(adb -s ${d} shell getprop 'ro.kernel.qemu' | tr -d '\r') | |
# genymotion is qemu based emulator but has all its characteristics as a real device | |
# so no need to telnet it to obtain a name | |
local is_geny=$(adb -s ${d} shell getprop 'ro.genymotion.version' | tr -d '\r') | |
if [ "$char" == "emulator" ] || ([ "$is_qemu" == "1" ] && [ -z "$is_geny" ]); then | |
# android emulator name always contains a port (eg. emulator-5554) | |
local port=$(echo "$d"|sed 's/emulator-//g') | |
# to get emulator name it is necessary to connect to this device using telnet and parse the response | |
local name=`(echo 'avd name';echo 'quit') | curl "telnet://localhost:${port}" 2>/dev/null | grep -B1 'OK' | tail -2 | head -1` | |
else | |
# all real devices contain the neccessary info in the following system properties: | |
# ro.product.manufacturer (eg. Samsung, LGE, Motorolla, etc.) and ro.product.model (Nexus 5, Nexus 5X, etc.) | |
local manufact=$(adb -s ${d} shell getprop 'ro.product.manufacturer' | tr -d '\r' | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1') | |
local model=$(adb -s ${d} shell getprop 'ro.product.model' | tr -d '\r') | |
local name="${manufact} ${model}" | |
fi | |
# print device id and human readable name | |
printf '%-30s %s\n' "$d" "$name" | |
done | |
} | |
INFO="$(get_info)" | |
if [ -n "$INFO" ]; then | |
# print devices info as a table with columns: ID and NAME | |
printf '\e[4m%s\e[0m%-29s\e[4m%s\n\e[0m' "ID" " " "NAME" | |
echo | |
echo -e "$INFO" | |
else | |
echo "No connected devices found." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment