Android ADB commands to get the device properties - android

I am trying to get the device properties from ADB commands. I can how ever get those values by running sample android application. How ever I wish to get using adb shell command itself to make my life easier. Here is the way I will get through sample application but I want corresponding adb commands for
device manufacturer
device hardware
device model
Os version(integer value)
Kernel version
* Please note my device is not rooted and I have no idea of rooting the device to get these values :-) *
## Code snippet
import android.os.Build;
manufacturer = Build.MANUFACTURER;
hardware = Build.HARDWARE;
model = Build.MODEL;
oSVersion = Build.VERSION.SDK_INT;
kernelVersion = System.getProperty("os.version");
However I can able to get the os version. But then I want SDK version in integer. I want 18 in place of 4.2.2
C:\>adb shell getprop ro.build.version.release
4.2.2

adb shell getprop ro.build.version.sdk
If you want to see the whole list of parameters just type:
adb shell getprop

From Linux Terminal:
adb shell getprop | grep "model\|version.sdk\|manufacturer\|hardware\|platform\|revision\|serialno\|product.name\|brand"
From Windows PowerShell:
adb shell
getprop | grep -e 'model' -e 'version.sdk' -e 'manufacturer' -e 'hardware' -e 'platform' -e 'revision' -e 'serialno' -e 'product.name' -e 'brand'
Sample output for Samsung:
[gsm.version.baseband]: [G900VVRU2BOE1]
[gsm.version.ril-impl]: [Samsung RIL v3.0]
[net.knoxscep.version]: [2.0.1]
[net.knoxsso.version]: [2.1.1]
[net.knoxvpn.version]: [2.2.0]
[persist.service.bdroid.version]: [4.1]
[ro.board.platform]: [msm8974]
[ro.boot.hardware]: [qcom]
[ro.boot.serialno]: [xxxxxx]
[ro.build.version.all_codenames]: [REL]
[ro.build.version.codename]: [REL]
[ro.build.version.incremental]: [G900VVRU2BOE1]
[ro.build.version.release]: [5.0]
[ro.build.version.sdk]: [21]
[ro.build.version.sdl]: [2101]
[ro.com.google.gmsversion]: [5.0_r2]
[ro.config.timaversion]: [3.0]
[ro.hardware]: [qcom]
[ro.opengles.version]: [196108]
[ro.product.brand]: [Verizon]
[ro.product.manufacturer]: [samsung]
[ro.product.model]: [SM-G900V]
[ro.product.name]: [kltevzw]
[ro.revision]: [14]
[ro.serialno]: [e5ce97c7]

You should use adb shell getprop command and grep specific info about your current device, For additional information you can read documentation:
Android Debug Bridge documentation
I added some examples below:
language - adb shell getprop | grep language
[persist.sys.language]: [en]
[ro.product.locale.language]: [en]
boot complete ( device ready after reset) - adb shell getprop | grep boot_completed
[sys.boot_completed]: [1]
device model - adb shell getprop | grep model
[ro.product.model]: [Nexus 4]
sdk version - adb shell getprop | grep sdk
[ro.build.version.sdk]: [22]
time zone - adb shell getprop | grep timezone
[persist.sys.timezone]: [Asia/China]
serial number - adb shell getprop | grep serialno
[ro.boot.serialno]: [1234567]

For Power-Shell
./adb shell getprop | Select-String -Pattern '(model)|(version.sdk)|(manufacturer)|(platform)|(serialno)|(product.name)|(brand)'
For Linux (burrowing answer from #0x8BADF00D)
adb shell getprop | grep "model\|version.sdk\|manufacturer\|hardware\|platform\|revision\|serialno\|product.name\|brand"
For single string find in power shell
./adb shell getprop | Select-String -Pattern 'model'
or
./adb shell getprop | Select-String -Pattern '(model)'
For multiple
./adb shell getprop | Select-String -Pattern '(a|b|c|d)'

Related

Get the exact value from android getprop

I wish to get specific value of properties from adb command getprop
To get specific value I use:
adb shell getprop ro.product.display_name
adb shell getprop ro.bootloader
adb shell getprop ro.serialno
adb shell getprop ro.product.model
Result example:
Galaxy Watch5
RFT4JD6GHK
RGHJKABVGTS
SM-R860
But this is not good, cos for each value, I need to do getprop each time. I wish to ask it once per device and then parse the result to get values.
Something like:
adb shell getprop
and then what I get from stdout is long list with all properties and values
...
[ro.build.version.release]: [4.2.2]
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.hardware]: [qcom]
[ro.opengles.version]: [196108]
[ro.product.brand]: [Verizon]
[ro.product.manufacturer]: [samsung]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
...
How to parse this big list end get same result as separate commands? I try bellow but I get the whole thing with brackets:
adb shell getprop | grep "ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model"
Result:
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
I solve it, but I'm not sure if there is easier solution:
adb shell getprop | grep 'ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model' | cut -d ":" -f 2 | sed 's:^..\(.*\).$:\1:'
Using awk and defining an array with the keys you want (ro.product.display_name used as an example here), you can do
adb shell getprop | awk -F '[][:]' 'BEGIN {keys["ro.product.display_name"]++} $2 in keys {print $5}'

adb.exe: unknown command am adb.exe: unknown command sleep error keeps poping up when am trying to run the shell script on my rooted android device

Hi guys am new to the shell scripting.. please do not mind if acted like a noob.
The following error keeps poping up when am trying to run the shell script on my android device
#!/bin/bash
for i in $(seq 100)
do
echo "Time: $i"
adb -s $* shell am start com.android.camera/.Camera -W -S
adb -s $* shell sleep 3
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
count=`adb -s $* shell ls /sdcard/DCIM/Camera/ | wc |awk '{print $1}'`
echo Count: $count
if [[ count -gt 500 ]]; then
break
fi
adb -s $* reboot
adb -s $* wait-for-device
adb -s $* shell sleep 40
adb -s $* shell input tap 500 1200
done
Result:
adb.exe: unknown command am
adb.exe: unknown command sleep
you can't run adb commands on your android device. instead the adb binary must installed on host. the script is a linux script and can not run from windows. prepare bootable usb flash drive and run this script from any linux terminal
furthermore some of your commands requires root permissions. therefore sush must invited (and permissions granted on superuser app on touchscreen)
adb shell su -c "am start com.android.camera/.Camera -W -S"
awk is not available on android. to make it more clear that these pipes are actually running on host, quote the android commands (or even better avoid awk at all)
count=$(adb shell "ls -1 /sdcard/DCIM/Camera" | wc | awk '{print $1}')
count=$(adb shell "ls -1 /sdcard/DCIM/Camera | wc -l")
am is actually a shell script pointing to am.jar this requires full booted android. if you try to run in recovery mode you should at least run the framework

adb: Find PID from the adb shell

I am trying to get the PID of the process INSIDE adb shell. So, I am doing adb shell which gets me to the android shell. Now, if I were to get the PID using a regular shell I would use
adb shell ps | grep android.process.acore | sed 's/\s\s*/ /g' | cut -d ' ' -f 2
OR
adb shell ps | grep android.process.acore | awk '{ print $2 }'
I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore) output.
However, if I run the above commands inside android shell(after doing adb shell), I get /system/bin/sh: sed: not found and /system/bin/sh: awk: not found errors respectively. Which means, these commands are not available inside adb shell. However, grep works.
The output of the ps | grep android.process.acore inside adb shell is:
XXX_x21 11826 441 502296 39028 ffffffff 4010ff6c S android.process.acore
I am looking for the number 11826.
How can I extract it inside adb shell?
Also, please help if there is a direct way to get the PID inside the adb shell.
Regards,
Rumit
Android versions starting with 6.0 already include pidof utility:
usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
Print the PIDs of all processes with the given names.
-s single shot, only return one pid.
-o omit PID(s)
Not sure if you can get the PID directly however you can try the following
set `ps |grep android.process.acore`
echo $2
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2
I tried this one and it seems to work:
adb shell "set "ps | grep android.process.media"; kill -9 $2"
adb shell pidof [package name]
or
adb shell pidof -s [package name]
-s option is for single shot, returning only one pid.
I tried this one and it seems to work:
adb shell
ps -A | grep "android.process.acore"

Bash: enumerate all the attached devices

I plugged several android devices to my laptop. And I can list their SN by
adb devices
output:
List of devices attached
015d4a826e0ffb0f device
015d4a826e43fb16 device
015d41d830240b11 device
015d2578a7280b02 device
I want to perform some operations on every device, like
adb -s $device install foo.apk
But I don't know how to let variable device iterate all the devices obtained by adb devices.
One way to do it in bash. Read the output of your command and iterate it on the second column using a while loop.
while read sn device; do
adb -s "$sn" install foo.apk
done < <(adb devices | sed '1d')
Main trick is to separate serial of device from other output. You need to cut off header and second column. Something like this would work:
for DEVICE in `adb devices | grep -v "List" | awk '{print $1}'`
do
adb -s $DEVICE install foo.apk
done
You could use xargs and awk:
adb devices | awk 'NR>1{print $1}' | xargs -n1 -I% adb -s % install foo.apk
Demo:
I put your input into a file and using echo to check the ouput produces:
$ awk 'NR>1{print $1}' file | xargs -n1 -I% echo adb -s % install foo.apk
adb -s 015d4a826e0ffb0f install foo.apk
adb -s 015d4a826e43fb16 install foo.apk
adb -s 015d41d830240b11 install foo.apk
adb -s 015d2578a7280b02 install foo.apk

How to use ADB Shell when Multiple Devices are connected? Fails with "error: more than one device and emulator"

$ adb --help
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
$ adb devices
List of devices attached
emulator-5554 device
7f1c864e device
$ adb shell -s 7f1c864e
error: more than one device and emulator
Use the -s option BEFORE the command to specify the device, for example:
adb -s 7f1c864e shell
For multiple Emulator, use the process's IP and port as the id, like:
adb -s 192.168.232.2:5555 <command>
See How to get the Android Emulator's IP address?
But if there is only a single Emulator, try:
adb -e <command>
See also http://developer.android.com/tools/help/adb.html#directingcommands
adb -d shell (or adb -e shell).
This command will help you in most of the cases, if you are too lazy to type the full ID.
From http://developer.android.com/tools/help/adb.html#commandsummary:
-d - Direct an adb command to the only attached USB device. Returns an error when more than one USB device is attached.
-e - Direct an adb command to the only running emulator. Returns an error when more than one emulator is running.
Another alternative would be to set environment variable ANDROID_SERIAL to the relevant serial, here assuming you are using Windows:
set ANDROID_SERIAL=7f1c864e
echo %ANDROID_SERIAL%
"7f1c864e"
Then you can use adb.exe shell without any issues.
To install an apk on one of your emulators:
First get the list of devices:
-> adb devices
List of devices attached
25sdfsfb3801745eg device
emulator-0954 device
Then install the apk on your emulator with the -s flag:
-> adb -s "25sdfsfb3801745eg" install "C:\Users\joel.joel\Downloads\release.apk"
Performing Streamed Install
Success
Ps.: the order here matters, so -s <id> has to come before install command, otherwise it won't work.
Hope this helps someone!
I found this question after seeing the 'more than one device' error, with 2 offline phones showing:
C:\Program Files (x86)\Android\android-sdk\android-tools>adb devices
List of devices attached
SH436WM01785 offline
SH436WM01785 offline
SH436WM01785 sideload
If you only have one device connected, run the following commands to get rid of the offline connections:
adb kill-server
adb devices
The best way to run shell on any particular device is to use:
adb -s << emulator UDID >> shell
For Example:
adb -s emulator-5554 shell
As per https://developer.android.com/studio/command-line/adb#directingcommands
What worked for my testing:
UBUNTU BASH TERMINAL:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ export ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL
646269f0
$ adb reboot bootloader
WINDOWS COMMAND PROMPT:
$ adb devices
List of devices attached
646269f0 device
8a928c2 device
$ set ANDROID_SERIAL=646269f0
$ echo $ANDROID_SERIAL$
646269f0
$ adb reboot bootloader
This enables you to use normal commands and scripts as if there was only the ANDROID_SERIAL device attached.
Alternatively, you can mention the device serial every time.
$ adb -s 646269f0 shell
This gist will do most of the work for you showing a menu when there are multiple devices connected:
$ adb $(android-select-device) shell
1) 02783201431feeee device 3) emulator-5554
2) 3832380FA5F30000 device 4) emulator-5556
Select the device to use, <Q> to quit:
To avoid typing you can just create an alias that included the device selection as explained here.
User #janot has already mentioned this above, but this took me some time to filter the best solution.
There are two Broad use cases:
1) 2 hardware are connected, first is emulator and other is a Device.
Solution : adb -e shell....whatever-command for emulator and adb -d shell....whatever-command for device.
2) n number of devices are connected (all emulators or Phones/Tablets) via USB/ADB-WiFi:
Solution:
Step1) run adb devices THis will give you list of devices currently connected (via USB or ADBoverWiFI)
Step2) now run adb -s <device-id/IP-address> shell....whatever-command
no matter how many devices you have.
Example to clear app data on a device connected on wifi ADB I would execute:
adb -s 172.16.34.89:5555 shell pm clear com.package-id
to clear app data connected on my usb connected device I would execute:
adb -s 5210d21be2a5643d shell pm clear com.package-id
For Windows, here's a quick 1 liner example of how to install a file..on multiple devices
FOR /F "skip=1" %x IN ('adb devices') DO start adb -s %x install -r myandroidapp.apk
If you plan on including this in a batch file, replace %x with %%x, as below
FOR /F "skip=1" %%x IN ('adb devices') DO start adb -s %%x install -r myandroidapp.apk
Create a Bash (tools.sh) to select a serial from devices (or emulator):
clear;
echo "====================================================================================================";
echo " ADB DEVICES";
echo "====================================================================================================";
echo "";
adb_devices=( $(adb devices | grep -v devices | grep device | cut -f 1)#$(adb devices | grep -v devices | grep device | cut -f 2) );
if [ $((${#adb_devices[#]})) -eq "1" ] && [ "${adb_devices[0]}" == "#" ]
then
echo "No device found";
echo "";
echo "====================================================================================================";
device=""
// Call Main Menu function fxMenu;
else
read -p "$(
f=0
for dev in "${adb_devices[#]}"; do
nm="$(echo ${dev} | cut -f1 -d#)";
tp="$(echo ${dev} | cut -f2 -d#)";
echo " $((++f)). ${nm} [${tp}]";
done
echo "";
echo " 0. Quit"
echo "";
echo "====================================================================================================";
echo "";
echo ' Please select a device: '
)" selection
error="You think it's over just because I am dead. It's not over. The games have just begun.";
// Call Validation Numbers fxValidationNumberMenu ${#adb_devices[#]} ${selection} "${error}"
case "${selection}" in
0)
// Call Main Menu function fxMenu;
*)
device="$(echo ${adb_devices[$((selection-1))]} | cut -f1 -d#)";
// Call Main Menu function fxMenu;
esac
fi
Then in another option can use adb -s (global option -s use device with given serial number that overrides $ANDROID_SERIAL):
adb -s ${device} <command>
I tested this code on MacOS terminal, but I think it can be used on windows across Git Bash Terminal.
Also remember configure environmental variables and Android SDK paths on .bash_profile file:
export ANDROID_HOME="/usr/local/opt/android-sdk/"
export PATH="$ANDROID_HOME/platform-tools:$PATH"
export PATH="$ANDROID_HOME/tools:$PATH"
Running adb commands on all connected devices
Create a bash (adb+)
adb devices | while read line
do
if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
then
device=`echo $line | awk '{print $1}'`
echo "$device $# ..."
adb -s $device $#
fi
done
use it with
adb+ //+ command
you can use this to connect your specific device :
* adb devices
--------------
List of devices attached
9f91cc67 offline
emulator-5558 device
example i want to connect to the first device "9f91cc67"
* adb -s 9f91cc67 tcpip 8080
---------------------------
restarting in TCP mode port: 8080
then
* adb -s 9f91cc67 connect 192.168.1.44:8080
----------------------------------------
connected to 192.168.1.44:8080
maybe this help someone
Here's a shell script I made for myself:
#! /bin/sh
for device in `adb devices | awk '{print $1}'`; do
if [ ! "$device" = "" ] && [ ! "$device" = "List" ]
then
echo " "
echo "adb -s $device $#"
echo "------------------------------------------------------"
adb -s $device $#
fi
done
For the sake of convenience, one can create run configurations, which set the ANDROID_SERIAL:
Where the adb_wifi.bat may look alike (only positional argument %1% and "$1" may differ):
adb tcpip 5555
adb connect %1%:5555
The advance is, that adb will pick up the current ANDROID_SERIAL.
In shell script also ANDROID_SERIAL=xyz adb shell should work.
This statement is not necessarily wrong:
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
But one can as well just change the ANDROID_SERIAL right before running the adb command.
One can even set eg. ANDROID_SERIAL=192.168.2.60:5555 to define the destination IP for adb.
This also permits to run adb shell, with the command being passed as "script parameters".

Categories

Resources