USB 主机和配件概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 通过 USB 配件和 USB 主机两种模式支持各种 USB 外围设备和 Android USB 配件(实现 Android 配件协议的硬件)。在 USB 配件模式下,外部 USB 硬件充当 USB 主机。配件示例包括:
- 机器人控制器
- 停放点
- 诊断和音乐设备
- 自助服务终端
- 读卡器
等等。这样,不具备主机功能的 Android 设备就能够与 USB 硬件进行交互。Android USB 配件必须设计为与 Android 设备兼容,并且必须遵循 Android 配件通信协议。在 USB 主机模式下,Android 设备充当主机。设备示例包括数码相机、键盘、鼠标和游戏控制器。专为各种应用和环境设计的 USB 设备仍可与能够与设备正确通信的 Android 应用交互。
图 1 展示了这两种模式之间的差异。当 Android 设备处于主机模式时,它会充当 USB 主机并为总线供电。当 Android 设备处于 USB 配件模式时,连接的 USB 硬件(在本例中为 Android USB 配件)将充当主机并为总线供电。

图 1. USB 主机和配件模式
Android 3.1(API 级别 12)或更高版本的平台直接支持 USB 配件和主机模式。USB 配件模式还作为插件库向后移植到 Android 2.3.4(API 级别 10),以支持更广泛的设备。设备制造商可以选择是否在设备的系统映像中包含插件库。
注意:对 USB 主机和配件模式的支持最终取决于设备的硬件,而与平台级别无关。您可以通过 <uses-feature>
元素过滤出支持 USB 主机和配件的设备。如需了解详情,请参阅 USB 配件和主机文档。
调试注意事项
调试使用 USB 配件或主机功能的应用时,您很可能已将 USB 硬件连接到 Android 设备。这样可以阻止您使用 USB 与 Android 设备建立 adb
连接。您仍可通过网络连接访问 adb
。如需通过网络连接启用 adb
,请执行以下操作:
- 通过 USB 将 Android 设备连接到计算机。
- 在 SDK
platform-tools/
目录中,在命令提示符处输入 adb tcpip 5555
。
- 输入
adb connect <device-ip-address>:5555
。您现在应该已连接到 Android 设备,并且可以发出常规的 adb
命令(如 adb logcat
)。
- 要设置设备来监听 USB,请输入
adb usb
。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# USB host and accessory overview\n\nAndroid supports a variety of USB peripherals and Android USB accessories\n(hardware that implements the Android accessory protocol) through two modes: USB\naccessory and USB host. In USB accessory mode, the external USB hardware acts as\nthe USB host. Examples of accessories might include:\n\n- robotics controllers\n- docking stations\n- diagnostic and musical equipment\n- kiosks\n- card readers\n\nand many more. This gives Android-powered devices that don't have host\ncapabilities the ability to interact with USB hardware. Android\nUSB accessories must be\ndesigned to work with Android-powered devices and must adhere to the [Android\naccessory communication protocol](http://accessories.android.com/demokit). In\nUSB host mode, the Android-powered device acts as the host. Examples of devices\ninclude digital cameras, keyboards, mice, and game controllers. USB devices that\nare designed for a wide range of applications and environments can still\ninteract with Android applications that can correctly communicate with the\ndevice.\n\nFigure 1 shows the differences between the two modes. When the Android-powered\ndevice is in host mode, it acts as the USB host and powers the bus. When the\nAndroid-powered device is in USB accessory mode, the connected USB hardware (an\nAndroid USB accessory in this case) acts as the host and powers the bus.\n\n**Figure 1.** USB Host and Accessory Modes\n\nUSB accessory and host modes are directly supported in Android 3.1 (API level\n12) or newer platforms. USB accessory mode is also backported to Android 2.3.4\n(API level 10) as an add-on library to support a broader range of devices.\nDevice manufacturers can choose whether or not to include the add-on library on\nthe device\\\\'s system image.\n\n**Note:** Support for USB host and accessory modes are ultimately dependant on\nthe device\\\\'s hardware, regardless of platform level. You can filter for devices\nthat support USB host and accessory through a\n[`\u003cuses-feature\u003e`](/guide/topics/manifest/uses-feature-element) element.\nSee the USB [accessory](/develop/connectivity/usb/accessory) and\n[host](/develop/connectivity/usb/host) documentation for more details.\n\nDebug considerations\n--------------------\n\nWhen debugging applications that use USB accessory or host features, you most\nlikely have USB hardware connected to your Android-powered device. This\nprevents you from having an `adb` connection to the Android-powered device\nusing USB. You can still access `adb` over a network connection. To enable `adb`\nover a network connection:\n\n1. Connect the Android-powered device using USB to your computer.\n2. From your SDK `platform-tools/` directory, enter `adb tcpip 5555` at the command prompt.\n3. Enter `adb connect \u003cdevice-ip-address\u003e:5555` You should now be connected to the Android-powered device and can issue the usual `adb` commands like `adb logcat`.\n4. To set your device to listen on USB, enter `adb usb`."]]