在adb的說明文檔中提到:
“An ADB transport models a connection between the ADB server and one device
or emulator. There are currently two kinds of transports:
- USB transports, for physical devices through USB
- Local transports, for emulators running on the host, connected to
the server through TCP”
大意是說,在物理設備上,adb是通過USB連接到設備上的,而在模擬器上,adb是通過TCP協議連接到設備上的。實際上在物理設備上,也可以讓adb 通過TCP協議來連接設備(當然前提條件是你的設備要有網口)。首先看一下下面這段源代碼,出自system/core/adb/adb.c,第921 行:
** android usb device exists and "service.adb.tcp"
** is not set, otherwise start the network transport.
*/
property_get("service.adb.tcp.port", value, "0");
if (sscanf(value, "%d", &port) == 1 && port > 0) {
// listen on TCP port specified by service.adb.tcp.port property
local_init(port);
} else if (access("/dev/android_adb", F_OK) == 0) {
// listen on USB
usb_init();
} else {
// listen on default port
local_init(ADB_LOCAL_TRANSPORT_PORT);
}
分析上述代碼可以發現,在adbd啓動時首先檢查是否設置了service.adb.tcp.port,如果設置了,就是使用TCP作爲連接方式;如果沒設置,就去檢查是否有adb的USB設備(dev/android_adb),如果有就用USB作爲連接方式;如果沒有USB設備,則還是用TCP作爲連接方式。
因此只需要在啓動adbd之前設置service.adb.tcp.port,就可以讓adbd選則TCP模式,也就可以通過網絡來連接adb了。這需要修改init.rc文件。如果不想修改,也可以在系統啓動之後,在控制檯上執行下列命令:
#stop adbd
#set service.adb.tcp.port 5555
#start adbd
這樣就可以在主機上通過下列命令來連接設備了:
adb connetc <ip-of-device>:5555