ADB

安卓手机相对 iPhone 来说生态系统更加开放,可以做各种各样酷酷的事情,例如通过自己电脑上的命令行来刷刷朋友圈之类的,下面我们来看看如何通过 adb 这个神器来控制我们的安卓机吧。

adb ,全称是 Android Debug Bridge ,也就是安卓调试桥,实际上就是一个命令行工具(并没有这么简单),通过它,我们就可以使用一系列命令来控制我们的手机了。

adb 架构

为了更好的理解,我们先来看一下 adb 的基本架构,在整个架构中存在三个重要的组件,clientADB ServerADB Daemon 。各个组件之间通过 TCP/IP 或者 USB 进行通信来交流信息。

ADB

  • client
    客户端,用于向 ADB Server 发送命令的工具,可以是命令行工具( Terminal )或者是 Android Studio ,存在于我们自己电脑上。

  • ADB Server
    电脑上的客户端和手机中守护进程之间的中介,类似一座桥(这就是为什么adb 叫做桥的原因了),主要负责两者之间的通信,在本地计算机上作为后台进程运行。我们可以通过 grep ps 命令的输出,来发现守护进程 adb 正在监听的 TCP 端口。

1
2
$ ps -e | grep adb
adb -L tcp:5037 fork-server server --reply-fd 4
  • ADB Daemon
    存在于手机之上的守护进程,负责接受并处理来自 ADB Server 的消息。类似的,我们可以通过 adb shell ps | grep adbd 来寻找手机上的adbd 进程及相应的 pid
1
2
$ adb shell ps | grep adbd
shell 13336 1 14532 812 0 0 S adbd

也就是说 clientadb server 存在于宿主机之上,采用传统的 C/S 模型,而 adb server 通过 TCPUSB 来向手机上的 adb daemon 发送消息。

adb 通信

通信肯定会涉及到协议,只有通信的双方都遵守协议才能顺利运行,下面是 abd 协议报文的基本结构,与 HTTP 协议类似,abd 协议同样也处于应用层。传输层既可以采用 USB 来传送,也可以使用我们熟悉的 TCP/IP

1
2
3
4
5
6
unsigned command; /* command identifier constant /
unsigned arg1; / first argument /
unsigned arg2; / second argument /
unsigned data_length; / length of payload (0 is allowed) /
unsigned data_crc32; / crc32 of data payload /
unsigned magic; / command ^ 0xffffffff */

也就是说 adb serveradb daemon 之间可以通过两种传输协议来发送 adb 协议报文。

USB

采用 USB 进行传输时,我们需要先将手机通过数据线连接到电脑上,注意手机需要进入开发者模式,并且通过相应的权限设置,如果连接成功的话,在命令行中输入 adb devices 就可以看到连接的设备了。

1
2
List of devices attached
988a1b30374e494a52 device

TCP/IP

当然除了 USB 这种传统方式的话,我们还可以利用酷酷的 TCP/IP 进行通信,首先也是通过 USB 连接手机和电脑,并确保两者连接到同一个局域网,在命令行中输入 adb tcpip 5555 使得 adb daemon 监听 5555 端口,然后通过 adb connect <IP_OF_YOUR_ANDROID_PHONE> 命令来连接我们的手机,这里 <IP_OF_YOUR_ANDROID_PHONE> 代表手机的 IP 地址。

1
2
$ adb tcpip 5555
$ adb connect <IP_OF_YOUR_ANDROID_PHONE>

如果连接成功,再使用 adb devices 命令会获得以下输出,这里的输出和上面有写不同,这里会输出 192.168.1.101:5555 ,这样就说明我们已经成功连接,可以拔掉我们的数据线进行通信了。

1
2
List of devices attached
192.168.1.101:5555 device

adb 常用命令

正确连接上了我们的手机之后,我们就可以干各种很酷的事情了。

获取版本信息

1
$ adb shell getprop ro.build.version.release

拷贝文件

1
2
$ adb push [source] [destination]
$ adb pull [device file location] [location file location]

screenshot 和 screenrecord

1
$ adb shell screencap -p /sdcard/screenshot.png
1
$ adb shell screenrecord /sdcard/notabletologin.mp4

手势模拟

1
$ adb shell swipe x1 y1 x2 y2

模拟基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Home btn
$ adb shell input keyevent 3
// Back btn
$ adb shell input keyevent 4
// Call
$ adb shell input keyevent 5
// End call
$ adb shell input keyevent 6
// Turn Android device ON and OFF. It will toggle device to on/off status.
$ adb shell input keyevent 26
// Camera
$ adb shell input keyevent 27
// Open browser
$ adb shell input keyevent 64
// Enter
$ adb shell input keyevent 66
// Delete (backspace)
$ adb shell input keyevent 67
// Contacts
$ adb shell input keyevent 207
// Brightness down/up
$ adb shell input keyevent 220 / 221
// Cut/Copy/Paste
$ adb shell input keyevent 277 / 278 /279
// https://developer.android.com/reference/android/view/KeyEvent.html
Pieces of Valuable Programming Knowledges