Android 经验: 5555 端口会被 adb 误认为 emulator

在本机启动 Android, 再用本机的的 adb 去连接

adb connect 127.0.0.1:5555


而后 adb devices 查看

user@ubuntu:~$ adb devices              
List of devices attached 
emulator-5554   device
127.0.0.1:5555  device

为何会出现两个设备? 我并没有运行 emulator?


如果你修改 system/build.prop 加入下面,修改adbd 的监听端口

service.adb.tcp.port=5566

就只出现一个设备

127.0.0.1:5566  device


这又是为何? 经过调查分析有了答案:


1) adb 启动就连接5555端口

启动 adb 的时候, adb 通过 "adb fork-server server" 启动 adb deamon

而后deamon 就会去找本地的 5555 端口, 直到 5555+32

ref: jellybean/system/core/adb/transport_local.c#140

为何连接上就叫 emulator, 这是因为 adb 期望自动为用户连接本机的emulator ( 每个emu两个端口, 可以多达16个)

如果你不用service.adb.tcp.port=5566 而用 5565 就是出现  emulator-5564, 因为连接只测试奇数端口


2) 为何连接叫 emulator-5554  而不是  emulator-5555

这是因为缺省emulator的 console 端口是 5554 ( 应该可以用 telnet 连接与 emulator 交互(还没有试验)) , 而adb 的端口是console端口 +1 就是 5555

当使用  adb emu <command>  可能就是把 <command> 发到5554端口 


总结: 

以前以为 emulator 是构造虚拟的 usb设备, 看来不是

adb  连接 emulator 也是通过 tcp 连接的

如果你有程序监听 5555 端口, 会被 adb 认为是 emulator

原文地址:https://www.cnblogs.com/jiangu66/p/3184790.html