[SCRIPT][Linux] ADB Wrapper Script - Multi Devices - Android Software Development

Hi Folks
Here's a quick script I knocked up to select a device from a list when you have multiple devices connected with adb.
Forgive me If something similar has already been posted.... I had a search but nothing obvious came up.
Code:
#!/bin/bash
## A Cheeky Wrapper For ADB and Multiple Devices
if [ $1 == "devices" ] ; then
adb devices
exit;
fi
select DEV in $(adb devices | awk 'NR>1 && $2="device"''{print $1}');
do
adb -s $DEV $*;
break
done
If you don't know what this is, It's probably not any use to you!
Thanks
Trev

Related

[solved/info] - adb with ubuntu

I am running ubuntu 9.04. I have eclipse and the Android s.d.k. set up and running perfectly. But I cannot figure out how to setup a.d.b.
I apologize for the new post, but I promise I have searched for hours on this forum and Google. All I found was info on installing on Windows, and fixing a.d.b that used to work but won't work now after updating to 9.04. Anybody have a tutorial or a fix that I might have missed? Thanks in advance.
there is nothing to set up just make shure it has execute permissions, thats what had me stumped LOL
#chmod +x adb
then just plug in the g1 (no drivers required) and
#adb whatever you want
but i was using arch :O
to make it easier copy the adb executable to /usr/bin and you can execute from any directory withoud using cd /directory
Code:
sudo cp /androidsdk/tools/adb /usr/bin
sudo chmod +x /usr/bin/adb
that's how i use mine on my 8.10 and 9.04
Thanks guys, pretty sure it's working, just need to figure out how to connect to device. I will try your suggestions.
Done. Then I tried
#adb logcat
and i get
- waiting for device -
should I
#cd /bin
#adb logcat
?
I am following tutorials for windows, so it is kind of confusing. I don't think I can connect. I get
Code:
[email protected]:~$ adb start-server
[email protected]:~$ adb devices
List of devices attached
[email protected]:~$
Here is me trying all kinds of stuff
Code:
[email protected]:~$ adb start-server
[email protected]:~$ adb devices
List of devices attached
[email protected]:~$ adb kill-server
[email protected]:~$ adb
Android Debug Bridge version 1.0.20
-d - directs command to the only connected USB device
returns an error if more than one USB device is present.
-e - directs command to the only running emulator.
returns an error if more than one emulator is running.
-s <serial number> - directs command to the USB device or emulator with
the given serial number
-p <product name or path> - simple product name like 'sooner', or
a relative/absolute path to a product
out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must
be an absolute path.
devices - list all connected devices
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> <local> - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] <file> - push this package file to the device and install it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its data)
adb uninstall [-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories)
adb bugreport - return all information from the device
that should be included in a bug report.
adb help - show this help message
adb version - show version num
DATAOPTS:
(no option) - don't touch the data partition
-w - wipe the data partition
-d - flash the data partition
scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints: offline | bootloader | device
adb get-serialno - prints: <serial-number>
adb status-window - continuously print device status for a specified device
adb remount - remounts the /system partition on the device read-write
adb root - restarts adb with root permissions
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PDP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns
adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be updated.
- If it is "system" or "data", only the corresponding partition
is updated.
[email protected]:~$ adb get-state
* daemon not running. starting it now *
* daemon started successfully *
unknown
[email protected]:~$ devices
bash: devices: command not found
[email protected]:~$ adb devices
List of devices attached
[email protected]:~$ adb remount
error: device not found
[email protected]:~$ adb start-server
[email protected]:~$ adb remount
error: device not found
[email protected]:~$ adb get-serialno
unknown
[email protected]:~$
tried another usb cable, different usb port. Same stuff
Code:
[email protected]:~$ adb shell
error: device not found
[email protected]:~$ adb start-server
[email protected]:~$ adb shell
error: device not found
[email protected]:~$ ls
adb examples.desktop Templates
android-sdk-linux_x86-1.5_r2 Firefox_wallpaper.png Videos
Desktop Music workspace
Documents Pictures World of Warcraft Trial
eclipse Public
[email protected]:~$ cd /
[email protected]:/$ ls
bin dev initrd.img media proc selinux tmp vmlinuz
boot etc lib mnt root srv usr
cdrom home lost+found opt sbin sys var
[email protected]:/$ adb shell
error: device not found
[email protected]:/$ adb start-server
[email protected]:/$ adb shell
error: device not found
[email protected]:/$ ls
bin dev initrd.img media proc selinux tmp vmlinuz
boot etc lib mnt root srv usr
cdrom home lost+found opt sbin sys var
[email protected]:/$ adb start-server
[email protected]:/$ adb shell
error: device not found
[email protected]:/$ adb kill-server
[email protected]:/$ adb devices
* daemon not running. starting it now *
* daemon started successfully *
List of devices attached
[email protected]:/$ adb shell
error: device not found
[email protected]:/$
Any ideas?
I am using the Rogers v3 Rom with jf1.42 bootloader and the 2005 s.p.l
Would any of that matter.
CBowley said:
I am using the Rogers v3 Rom with jf1.42 bootloader and the 2005 s.p.l
Would any of that matter.
Click to expand...
Click to collapse
Doubtful. I too am looking to get ADB running on my Ubuntu laptop, as it would save a bunch of time for me being able to use it instead of my slow windows computer (ADB is corrupted driver-wise on my main rig).
Anyone have any reasonable tutorials for lower-level linux users on Ubuntu 9.04 32-bit?
I have Ubuntu + SDK and I don't have any issue.
Are you sure you installed your SDk the correct way == you followed exaclty the android.com howto ?
it should work with ubuntu 9.04. Just follow this instructions to setup connection to your phone. mainly create rule for you device in udev.
http://developer.android.com/guide/developing/device.html
If you're developing on Ubuntu Linux, you need to add a rules file:
1. Login as root and create this file: /etc/udev/rules.d/50-android.rules.
For Gusty/Hardy, edit the file to read:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
For Dapper, edit the file to read:
SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0bb4", MODE="0666"
2. Now execute:
chmod a+rx /etc/udev/rules.d/50-android.rules
reboot, then if you are still having problem after this, one more thing to try to start server swith sudo.
adb kill-server
sudo adb start-server
knaries2000 said:
it should work with ubuntu 9.04. Just follow this instructions to setup connection to your phone. mainly create rule for you device in udev.
http://developer.android.com/guide/developing/device.html
If you're developing on Ubuntu Linux, you need to add a rules file:
1. Login as root and create this file: /etc/udev/rules.d/50-android.rules.
For Gusty/Hardy, edit the file to read:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
For Dapper, edit the file to read:
SUBSYSTEM=="usb_device", SYSFS{idVendor}=="0bb4", MODE="0666"
2. Now execute:
chmod a+rx /etc/udev/rules.d/50-android.rules
reboot, then if you are still having problem after this, one more thing to try to start server swith sudo.
adb kill-server
sudo adb start-server
Click to expand...
Click to collapse
I think I did this part, I will double check. I did try sudo adb start-server, that didn't work. I will double check the rest. Thank you.
Oh that's why I never did it, I cannot find a rule for jaunty, only hardy, dapper, and gutsy.. I'll do more searching. Thanks for the idea.
Okay it works now. This is what I get
Code:
[email protected]:~$ adb start-server
* daemon not running. starting it now *
* daemon started successfully *
[email protected]:~$ adb devices
List of devices attached
HT845GZ28769 device
[email protected]:~$ adb shell
# ls
cache init.goldfish.rc sbin
data init.rc sdcard
default.prop init.trout.rc sqlite_stmt_journals
dev proc sys
etc root system
init runme.sh
#
THIS IS WHAT I DID:
created 2 files on my desktop named:
50-android.rules
51-android.rules
Then I edited both files to read:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
Then I opened terminal and did this
Code:
[email protected]:~$sudo mv /home/chris/desktop/50-android.rules /etc/udev/rules.d
[email protected]:~$sudo mv /home/chris/desktop/51-android.rules /etc/udev/rules.d
Then I rebooted and It all worked. I am runnung ubuntu 9.04 jaunty jakelope(sp?)
Thanks for everyones help. Hope this helps someone else.
CBowley said:
Okay it works now. This is what I get-------
Thanks for everyones help. Hope this helps someone else.
Click to expand...
Click to collapse
Thank you so much, this works perfectly. Bump for someone else finding this who needs it.
Yes it has to be rule 51, not 50 with Jaunty. Google doc is not up to date yet.
see this thread :
http://groups.google.com/group/andr...read/thread/ab6e89c4b51cd905/a05c2a749cd55b88
> this was in /etc/udev/rules.d/50-android.rules
> But it seems to also work like this : (method for Ubuntu 9.04 :
> http://www.frandroid.com/2371/ubuntu-904-et-android/ )
> -> in /etc/udev/rules.d/51-android.rules :
> SUBSYSTEM==”usb”, SYSFS{idVendor}==”0bb4", MODE=”0666"
Ubuntu 9.04 already has a rules/50-something file so you need to make
sure that the android rule is > 50. Otherwise whatever you define gets
reset. We should update the doc to just state rule/51-android.rules
which will work right now till the base configuration changes once
again
> Also take care if you copy/paste this rule : looks like double quotes
> (") may be wrongly copied from html page, and then it did not work for
> me before I retyped them.
Hmm thanks for the info. That's a new one.
Edit
Ok sorry a little OT, but I responded to this because I did not see an answer posted. I Seem to be having problems with posts showing up when I am logged in... but not when I am logged out... really weird. After I logged out at least another dozen posts showed up in this thread.
you need to change the rule from 50 to 51 on ubuntu 9.4
didnt realise the answer was posted sorry. but its def the right answer
CBowley said:
I am running ubuntu 9.04. I have eclipse and the Android s.d.k. set up and running perfectly. But I cannot figure out how to setup a.d.b.
I apologize for the new post, but I promise I have searched for hours on this forum and Google. All I found was info on installing on Windows, and fixing a.d.b that used to work but won't work now after updating to 9.04. Anybody have a tutorial or a fix that I might have missed? Thanks in advance.
Click to expand...
Click to collapse
sudo bash -c "echo SUBSYSTEM==\"usb\", SYSFS{idVendor}==\"0bb4\", MODE=\"0666\" > /etc/udev/rules.d/51-android.rules"
sudo /etc/init.d/udev restart
killall adb
adb shell

One for the toolbox: quick shellscript for pushing links from desktop to device

Just a short shellscript I find useful from times to times (xpost from my blog):
Code:
#!/bin/bash
# Get the clipboard contents and trim it
url=`xclip -o | xargs`
# Don't bother with anything that doesn't start with http
[[ $url = *http* ]] && {
# If we are reasonably sure we have a url, push it to the device
adb shell am start -a android.intent.action.VIEW -d $url
}
Ha, great! That's actually quicker than using Pusbullet from the browser. Not it only needs device to device support

[SCRIPT] Log CPU Usage with interval (Linux)

Hey guys, I've just decided to get inside of the Android development world. Anyways, I'm mostly a fan of Linux, so I've made my first Linux script, I know it's simple, but I've never interacted with the user before, or picked an output. When I knew that Android made use of Linux in its core, my interest on working inside it grew up faster than ever.
In my script, firstly, you type the filename. For compatibility reasons, it'll everytime save to /sdcard/filename. Then, it will ask you how many times you want the script to log everything, and lately, every how many seconds should it save everything to the file.
The script will save, before every entry at the log, the screen status (if it's on or off), that can help to, for example, if your device has been lagging as a result of a bad screen driver or UI error.
Last thnig: it also works on recovery (maybe it give some error, and if no dumpsys is present on your recovery's ramdisk, it'll save everything as OFF).
Remember to send it anywhere excepting from /storage and any sbudir, it won't work as the Android permissions system blocks script execution on those dirs.
Code:
clear
# Wipe $logfile content in /sdcard (if any existing)
echo "Please type the filename where you want to save the log: "
read -r logfile
echo "" > /sdcard/$logfile
echo "Deleted previous log."
echo "SCREEN | CPU" >> /sdcard/$logfile
#cont=60
cont=1
#SHOWLOG=X
echo "Please type how many times the cycle must repeat: "
read -r maxcont
echo "Please type how many seconds will the interval take: "
read -r maxmin
clear
# Here it counts how many times the logging script has been ran
while [ $cont -lt $((maxcont + 1)) ];
do
# That nested while takes control of how many seconds have passed in the current cycle
while [ "$contmin" -lt "$maxmin" ];
do
contmin=$((contmin + 1))
sleep 1
clear
echo "C: $cont/$maxcont - T: $contmin/$maxmin"
done
# echo "$maxmin seconds passed."
# Logs the fist line of "busybox top" to a var
LOGVAR=$(busybox top -d 1 -n 1 | grep "idle")
# Logs the screen state (on or off) to another var
SCREENSTATE=$(dumpsys input_method | grep mScreenOn | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]')
# Saves a string that tells what it should expect if the screen is on
SCREENSTATEON="msystemreadytruemscreenontrue"
if [ "$SCREENSTATE" == "$SCREENSTATEON" ]
then
echo "ON: $LOGVAR" >> /sdcard/$logfile
else
echo "OFF: $LOGVAR" >> /sdcard/$logfile
fi
cont=$((cont + 1))
contmin=0
done
echo "Show logged values? [Y/N]"
read -r SHOWLOG
case $SHOWLOG in
[yY] | [yY][Ee][Ss] )
cat /sdcard/$logfile
;;
[nN] | [n|N][O|o] )
echo "Ok, goodbye. You'll see a what you've asked me to log in /sdcard/$logfile";
exit 1
;;
*) echo "You must write Y or N to continue."
;;
esac
exit 0
For example, save it as script.sh (remember to save it using Unix codifcation, if you're running Windows, else, it'll tell you that it's unable to find many binaries, as long as Linux isn't ok with CR+LF and requires LF as newline). Then, send it to the sdcard (over USB or adb push script.sh /sdcard/) , now, run ADB shell. Type the following commands:
Code:
su -c "cp -f /sdcard/script.sh /data/local/;chmod 777 /data/local/script.sh"
Then, if you want to run it:
Code:
su
sh /data/local/script.sh
That's all, I hope you enjoy it and not so many people kills me for explaining and feeling so grateful for that simple script.

[SOLVED]Unable to get fastboot working on Ubuntu 16.04 LTS

Hey can anyone look into this?
I'm unable to get fastboot working on my laptop (ubuntu 16.04 LTS -everything updated). adb works fine.
I downloaded the platform tools from here: https://dl.google.com/android/repository/platform-tools-latest-linux.zip
After extracting it I even added to the path by adding the following to ~/.profile
Code:
# add Android SDK platform tools to path
if [ -d "$HOME/platform-tools" ] ; then
PATH="$HOME/platform-tools:$PATH"
fi
and then running source ~/.profile
So what I did further after putting my phone in bootloader/fastboot mode is
1. fastboot devices
This returns the following error:
Code:
no permissions (verify udev rules); see [http://developer.android.com/tools/device.html] fastboot
I tried adding the USB IDs in the /etc/udev/rules.d/51-android.rules and running chmod a+r /etc/udev/rules.d/51-android.rules as mentioned on the URL but still nothing. Also it has Motorola, Lenovo, Google and Qualcomm IDs but none worked.
2. fastboot erase cache
This returns
<waiting for any device>View attachment 4071566
I tried rebooting both my system and the phone but nothing works.
I have a Moto G Plus (XT1643)
WHAT SHOULD I DO?
All you need is the following command
sudo apt install android-tools-adb android-tools-fastboot
Then add the sudo word before your fastboot command.
USB debugging has to be on and in some cases, you have to change your USB mode from charging only to file transfer.
Sent from my Moto G4 Plus using Tapatalk
Silesh.Nair said:
All you need is the following command
sudo apt install android-tools-adb android-tools-fastboot
Then add the sudo word before your fastboot command.
USB debugging has to be on and in some cases, you have to change your USB mode from charging only to file transfer.
Sent from my Moto G4 Plus using Tapatalk
Click to expand...
Click to collapse
Woah... I was totally unaware of this. #beginner Thanks alot. works well.
 @Silesh.Nair I also found another solution:
Code:
$ wget -S -O - http://source.android.com/source/51-android.rules | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules
Thread owner has found what he wants and fixed.
THREAD CLOSED on owner's request

[SOLVED][NST/G] USB debugging puzzle

Yes, another puzzle
How ADB works is supposed to be baked into init.rc in uRamdisk. I can see something like this in the scripts for NookManager, although there is no indication that ADB is set up for TCP, just that an unsecure connection via ADB is enabled:
Code:
sed -i \
-e's/^ro.secure=1/ro.secure=0/' \
-e's/^persist.service.adb.enable=0/persist.service.adb.enable=1/' \
/tmp/ramdisk/default.prop
# Verify that the patched lines exist in default.prop
grep -q '^ro.secure=0' /tmp/ramdisk/default.prop && \
grep -q '^persist.service.adb.enable=1' /tmp/ramdisk/default.prop && \
PROP_PATCHED=1
[ -f /tmp/ramdisk/init.rc.orig ] || cp /tmp/ramdisk/default.prop /tmp/ramdisk/init.rc.orig
# Comment out the " disabled" line after "service adb"
# Comment out the "on property:persist.service.adb*" blocks entirely
sed -i \
-e '/^service adbd/{ N s/\(service adbd.*\n\) /\1#MOD# / }' \
-e '/^on property:persist.service.adb.enable/{ N s/^\(.*\)\n \(.*\)/#MOD#\1\n#MOD# \2/}' \
/tmp/ramdisk/init.rc
# Verify that the patched lines exist in init.rc
grep -q '#MOD#on property:persist' /tmp/ramdisk/init.rc && \
grep -q '#MOD# disabled' /tmp/ramdisk/init.rc && \
INIT_PATCHED=1
My last item in the new Developer Settings section of a revamped Settings app is USB debugging. In the hidden Development section of the original Settings app there is a checkbox option for USB debugging, but it does not seem to do anything--at least not consistently. But I have had ADB over USB working on and off for the past few days. I just can't narrow down the conditions.
To my knowledge there are two places where changes occur if USB debugging is checked. One is in the "secure" section of settings.db. There a value for adb_enable is either 0 or 1. Checking USB debugging changes the value to 1. Also, in data/property/persist.service.adb.enable, there is a value of either 0 or 1. Checking USB debugging changes the value to 1.
Of course, these could just be artifacts for other changes that I have not detected.
What is certain is that I have managed to restart ADB by "adb usb" on occasion, after meddling with various things, and it has worked. The setting (whatever it is) persists until a reboot. ADB via WiFi still works (not at the same time, of course).
But I can't seem to get at the key to what makes it work and what does not.
Suggestions?
Progress...kinda
OK, so now I have a reproducible result. I'm using ADB Konnect for WiFi but I suspect other similar apps will do. Here's the sequence:
1. Connect to WiFi
2. Start ADB Konnect (turn it ON)
3. From the command console or however you do it, connect to the NST via ADB (using the device IP address)
4. Once connected type 'adb usb'
(should say restarting)
5. Stop/turn OFF ADB Konnect
6. Connect device to PC with USB cable
7. Type 'adb devices'
You should see your device (some long number string) as connected. This access to USB debugging should persist until a reboot or power cycle.
But HOW? And can all the folderol around this process be trimmed back or (better) converted into a series of shell commands?
Got it!!!!
Easy-peasy....when you know what to do:
Code:
adb shell
#setprop service.adb.tcp.port -1
#stop adbd
#start adbd
This starts USB debugging and still allows ADB via WiFi when wanted. The setting persists until a reboot or power cycle. And...it can all be done by a series of shell commands hidden behind a simple screen tap
@nmyshkin
Oops, late to this party.
adb first looks for service.adb.tcp.port, then for persist.adb.tcp.port
Code:
setprop persist.adb.tcp.port 5555
Renate NST said:
@nmyshkin
Oops, late to this party.
adb first looks for service.adb.tcp.port, then for persist.adb.tcp.port
Code:
setprop persist.adb.tcp.port 5555
Click to expand...
Click to collapse
Yes, this is the default state the device is left in by rooting via NookManager. The good news is that by setting the port to -1 (and restarting adbd), you can use adb via USB as desired while WiFi apps like ADBKonnect will also continue to work--at least until a reboot or power cycle (which for an NST can be a long time!).
Erm, maybe I didn't make my point or maybe you got it.
If you want persistency (and be able to switch modes) don't set service.adb.tcp.port to anything.
Make sure it's not set in init.rc or any other .rc
Use only persist.adb.tcp.port
Setting it to zero (or a negative number) will make adb use only USB
Of course, not having this set either will set it to USB also.
Me, I don't use adb over WiFI.
It would be to easy to forget about it and then connect to some random WiFi which hackers would portscan.

Categories

Resources