Some ADB utility functions

Filed Under (android, bash) by Nathan Schwermann on 16-03-2013

Tagged Under : , ,

It is always such a pain debugging when you have multiple devices and emulators connected at the same time. I hate the dreaded “error: more than once device and emulator” message or “-waiting for device” when trying to view logcat.


So today I wrote some helper functions for my bash profile that will offer a menu to select what device to use when more than one is available. It is assumed that both adb and Jeff Sharkey’s coloredlogcat script are in your $PATH variable. If not, it shouldn’t be hard to modify the code to use direct paths.

adbd () {
	adb -s $(get_device) "$@"
}
 
function get_device() {
	local devices=$(adb devices | grep device$)
	if [ $(wc -l <<< "$devices") -eq 1 ]; then
		awk {'print $1'} <<< "$devices" 
	else
		IFS=$'\n' devices=($devices)
		unset IFS
		local device
		PS3="Select a device # "
		select device in "${devices[@]}"; do
			if [ -n "$device" ]; then
				awk {'print $1'} <<< "$device"
			fi
			break
		done
	fi
}
 
function logcat(){
	local device
	device=$(get_device)
	if [ -z "$1" ]
	then
		adb -s $device logcat | coloredlogcat.py
   	else
      		local filters=""
      		for f in $@
      		do
			export filters="$filters $f:*"
      		done
      		echo "filters $filters"
		adb -s $device logcat $filters *:S | coloredlogcat.py    
  	 fi
}

The usage goes as follow, after adding to your bash profile (add to ~./bash_profile on OSX or ~/.bashrc on linux) you can use the follow commands:

deviceid=$(get_device)


Full Source Here

Share

Make a comment

You must be logged in to post a comment.