Check if adb device connected condition - android

How can I check adb device is connected in batch files?
I wrote "adb devices" in the batch file, but I want it as a condition so the app working smoothly and automatically
So if the user is not connected his device print no device and exit the app,
Otherwise resume the app.

Pipe the output to find and analyse the errorlevel:
adb devices -l | find "device product:" >nul
if errorlevel 1 (
echo No connected devices
) else (
echo Found!
..............
)

For user in bash or similar shell environment, try
$ adb get-state 1>/dev/null 2>&1 && echo 'device attached' || echo 'no device attached'

This is the easiest and is most accurate since it'll only find "device" and not other words containing the string such as "devices":
adb devices | findstr "\<device\>"
Take note that findstr works on Windows but not in Unix terminals. So the equivilent would be to use find and grep together. John explains this well in this page Using find and grep to Mimic findstr Command

Related

Reboot to Bootloader with shell script to multiple devices hangs if more than one device attached

Full disclosure: I'm new to both Android development and Linux.
I have a shell script to loop through connected devices and perform ADB commands.
while read -r udid device usb product model device2; do
echo ""
echo $usb
echo ""
# REBOOT
adb -s "$usb" reboot-bootloader
# UNLOCK OEM
fastboot -s "$usb" oem unlock
# REBOOT
fastboot -s "$usb" reboot
done < <(adb devices -l | sed "1d; $ d")
This works great on one device. If I have two devices the first device will work but the second will hang at:
< waiting for usb:1-2 >
On three devices the first device will reboot to bootloader and then hang like I said above.
The endgame here is to execute this script (and several others) on as many android devices as possible at one time. This loop works great for pushing our APK to each device but to unlock the bootloader, to run this code AND for another script to boot to recovery it hangs.
I found this stackoverflow and made sure my udev rules were correct. As I said it works on one device no problem.
I also referenced this post that recommended I change my while loop to a for loop and that has the same result.
These devices are Mediatek phones running Android 6.0 Marshmallow. So all the serial number are the same (0123456789ABCDEF) if that's an issue. I'm using Ubuntu 8.10.
So I ended up not using linux and the issue was with the devices all having the same serial number. What I did do was create to bash scripts to run on PC.
The first script looped through the device by the TRANSIT ID to apply the second script which was full of adb commands.
EXAMPLE:
SCRIPT 1:
#echo off
cd C:\adb
FOR /f "tokens=5 delims=:" %%G IN ('adb devices -l') DO (
start cmd.exe /c "call c:\srcmd\esc-cmd.bat %%G"
)
Calls SCRIPT 2:
#echo off
cd C:\adb
echo ##OPEN AND INSTALL SETTINGS APP##
echo ""
echo ""
echo ""
adb -t %~1 shell input keyevent KEYCODE_WAKEUP
adb -t %~1 shell input keyevent 111
echo ""
echo ""
echo ""
echo ##DONE##
With this system I can run custom adb scripts on multiple devices at one time. I'm running up to 20 at a time at the moment. Works like a charm!

ADB: How to install apk on multiples devices simultaneously using adb commands

How do I install apk on multiple devices simultaneously using adb command line instructions?
I want to install apk on two or more devices with single command, Is it possible?
I have tried using, below command.
"adb -s install .apk path" --> this will install the apk on either one of the connected device but not on both the connected devices.
Please help....
There is no "out of the box" solution. You have to put some time in there:
You can query all attached devices via 'adb devices' and have to process the output to install the apk on all attached devices ( do a 'adb install' for every connected device )
Maybe there is already a tool for this outside in the web, but i haven't found one.
See http://developer.android.com/tools/help/adb.html#devicestatus for further information.
Regards
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

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".

AT command in Android

I want to use AT command in my application to set some order to GSM modem.
I searched Google but i could not find any good answer!
Do you have any solution?
and can i use ADB to send AT command to android?
first you have to root the phone then in adb shell
su
echo -e "AT\r" > /dev/smd0
if you want to see answer use
cat /dev/smd0
i've test this command in samsung mini,cooper,s+ and it works.
if you use htc (htc rhyme tested) try to adb shell and type this command "radiooptions 13 AT" if you want to see answer type "logcat -b radio"
try echo to /dev/smd0 for other devices
*you can use this command in sdk java code by using Runtime.exec (require su)
example : echo -e "ATD123456789;\r" > /dev/smd0 ----> (call to number 123456789)

Can I use adb.exe to find a description of a phone?

If I call the command "adb.exe devices" I get a list of devices with a unique ID for each. These IDs are perfect for programming but not very human readable. Is there any way I can link one of these IDs to a (not necessarily unique) description of the phone? For example, if I have a device with an ID 1234567890abcdef is there any way I can figure that in real life it is a Motorola Droid X?
In Android there is a Model number entry in settings that shows phone name.
There is a way to quickly see this via command line:
adb shell cat /system/build.prop | grep "product"
This is what's shown for Samsung Galaxy S 4G:
ro.product.model=SGH-T959V
ro.product.brand=TMOUS
ro.product.name=SGH-T959V
ro.product.device=SGH-T959V
ro.product.board=SGH-T959V
ro.product.cpu.abi=armeabi-v7a
ro.product.cpu.abi2=armeabi
ro.product.manufacturer=Samsung
ro.product.locale.language=en
ro.product.locale.region=US
# ro.build.product is obsolete; use ro.product.device
ro.build.product=SGH-T959V
On a HTC Desire, the output looks like this:
ro.product.model=HTC Desire
ro.product.brand=htc_wwe
ro.product.name=htc_bravo
ro.product.device=bravo
ro.product.board=bravo
ro.product.cpu.abi=armeabi-v7a
ro.product.cpu.abi2=armeabi
ro.product.manufacturer=HTC
ro.product.locale.language=hdpi
ro.product.locale.region=
You can refine your query to show only one line:
adb shell cat /system/build.prop | grep "ro.product.device"
or
adb shell cat /system/build.prop | grep "ro.product.model"
With newer devices, you can run
adb devices -l
This will list some devices in a more human readable form
Example:
04d9a601ce516d53 device usb:1A134500 product:occam model:Nexus_4 device:mako
015d4b3406280a07 device usb:1A134700 product:nakasi model:Nexus_7 device:grouper
01466E620900F005 device usb:1A134600 product:mysid model:Galaxy_Nexus device:toro
Yes, adb shell cat /system/build.prop is wonderful, but also as #Matt said, it sometimes differs because of different manufacture.
IMHO, the most reliable approach is the built-in command: adb shell getprop
======================================================================
Here is a comparison for an exception (Genymotion emulator):
By adb shell cat /system/build.prop you'll get
ro.product.brand=generic
ro.product.name=vbox86p
ro.product.device=vbox86p
ro.product.board=
ro.product.cpu.abi=x86
ro.product.manufacturer=Genymotion
ro.product.locale.language=en
ro.product.locale.region=US
So there is no model value :(
By adb shell getprop you'll get
[ro.product.manufacturer]: [Genymotion]
[ro.product.model]: [Nexus422ForAutomation]
[ro.product.name]: [vbox86p]
Here you get the model name: Nexus422ForAutomation :)
Unfortunately, there is no reliable way to do this since each manufacturer tweaks their build properties
If you type "adb help" into the command line you can see a list of the adb commands and a description for each.
I'm using this script (on MacOS):
#!/bin/bash
devices=`adb devices | grep "device" | grep -v "List of" | cut -d " " -f 1`
for device in $devices
do
manufacturer=`adb -s $device shell getprop ro.product.manufacturer | sed -e 's/[[:space:]]*\$//'`
model=`adb -s $device shell getprop ro.product.model | sed -e 's/[[:space:]]*\$//'`
echo "$device: $manufacturer $model"
done
It produces e.g. the following output:
T076000UWX: motorola XT1053
C4F12070E5A068E: samsung GT-P7510
4df1ff977b919f91: samsung GT-N7100
4258393032523638324D: Sony Ericsson LT18i

Categories

Resources