adb多设备管理

由于开发机中总是同时连着好几个设备,而我们的设备大部都有着相同的名字,而且我们可能同时操作着好几个设备,为了解决这个问题写了个shell脚本对这些设备进行管理,方便开发。

功能简介:
1. 如果存在设备环境变量(ANDROID_SERIAL),并且设备的环境变量存在那么就使用设备环境变量的值,否则就从已经存在的设备选择一个。
2. 为okadb添加了自动补全的功能,仅仅添加了几个我常用的。
3. 整合一些常用的功能。
4. 设置设备名字这个函数默认名字为"123",设置设备环境变量默认是选择已经存在的设备,如果传null这取消设备环境变量。

简单使用:

$ source okadb.sh
$ adbsetenv null
$ okadb shell
1) 7f7c509e
2) 0123456789
3) 123 
4) 2.2.2.108:5555
#? 4
select android device is 2.2.2.108:5555
root@pone:/ # exit
$ adbsetenv
1) 7f7c509e
2) 0123456789
3) 123 
4) 2.2.2.108:5555
#? 2
$ okadb shell
select android device is 0123456789
root@d508:/ # exit
$ adbsetenv null
$ okadb tcpip 5555
1) 7f7c509e
2) 0123456789
3) 123 
4) 2.2.2.108:5555
#? 1
select android device is 7f7c509e
restarting in TCP mode port: 5555
$ adbrename myname
1) 0123456789
2) 123 
3) 2.2.2.108:5555
4) 7f7c509e
#? 1
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
7f7c509e    device
123 device
myname  device


脚本代码如下:

#!/bin/bash

# Author: [email protected]
# Time  : Fri May 29 18:04:09 CST 2015

_okadb()
{
	local cur prev opts
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	opts="shell push pull install tcpip usb logcat root reboot 		reboot-bootloader forward devices kill-server remount"

	case "$prev" in
		shell | pull | reboot-bootloader | logcat | devices 			| kill-server | root | remount)
			COMPREPLY=()
			return 0
			;;
		reboot)
			COMPREPLY=( $(compgen -W "bootloader recovery" -- $cur) )
			return 0
			;;
		push)
			COMPREPLY=( $(compgen -o default -o plusdirs -f -- $cur) )
			return 0
			;;
		*)
			local prev2="${COMP_WORDS[COMP_CWORD-2]}"
			if [ "$prev2" == "push" ] || [ "$prev2" == "reboot" ];then
				return 0
			fi
			;;
	esac

	COMPREPLY=( $(compgen -W "$opts" -- $cur) )
	return 0
}

select_device()
{
	# if env dev exist && find it, use it. || choose one of the devices.
	if test $# -eq 0 -a -n "${ANDROID_SERIAL}"; then
		local env_dev="${ANDROID_SERIAL}"
		local devs=( `adb devices` )
		local env_dev_status=0

		# find the env device
		for t in ${devs[@]}; do
			if test "$t" = "$env_dev"; then
				env_dev_status=1
				break
			fi
		done

		if test ${env_dev_status} -eq 1; then
			echo ${env_dev}
			return 0
		fi
	fi

	local c=0 s=0 devs sel_dev
	devs=( `adb devices` )
	for (( c=0; c < ${#devs[@]}; c++ )); do
		if test "device" = ${devs[c]}; then
			t=$((c-1))
			#echo ${devs[$t]}
			sel_dev[$s]=${devs[$t]}
			s=$((++s))
		fi
	done

	#echo "${sel_dev}"
	if test ${#sel_dev[*]} -ge 2; then
		select var in ${sel_dev[*]}; do
			break;
		done
		echo $var
		return 0
	else
		echo ${sel_dev}
		return 0
	fi
	echo
	return 1
}

okadb()
{
	if test $# -eq 1 -a "$1" = "devices"; then
		adb $@
		return 0
	fi

	dev="$(select_device)"
	echo "select android device is $dev"
	if test -n "$dev"; then
		adb -s $dev $@
		return 0
	fi
	adb devices
	return 1
}

# add automatic completion for okadb
complete -F _okadb okadb

adbconnect()
{
	# FIX ME
	adb connect 2.2.2.$1
}

adbdisconnect()
{
	dev="$(select_device)"
	adb disconnect $dev
}

adbrestart()
{
	adb kill-server
	adb start-server
}

adblg()
{
	dev="$(select_device)"
	adb -s $dev logcat
}

adblgt()
{
	dev="$(select_device)"
	adb -s $dev logcat -v time
}

adblgth()
{
	dev="$(select_device)"
	adb -s $dev logcat -v threadtime
}

adbrename()
{
	dev="$(select_device)"
	name="123"
	if test -n "$1"; then
		name="$1"
	fi
	adb -s $dev shell "echo "$name" > /sys/class/android_usb/android0/iSerial"
	adbrestart
	adb devices
}

adbsetenv()
{
	if test $# -eq 1 -a "$1" = "null"; then
		export ANDROID_SERIAL=""
	else
		dev="$(select_device)"
		export ANDROID_SERIAL="$dev"
	fi
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。