Tổng quan về phụ kiện và máy chủ USB
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Android hỗ trợ nhiều thiết bị ngoại vi USB và phụ kiện USB Android (phần cứng triển khai giao thức phụ kiện Android) thông qua 2 chế độ: phụ kiện USB và máy chủ USB. Ở chế độ phụ kiện USB, phần cứng USB bên ngoài đóng vai trò là USB host. Ví dụ về phụ kiện có thể bao gồm:
- bộ điều khiển robot
- trạm lấy và trả xe
- thiết bị chẩn đoán và âm nhạc
- kiosk
- máy đọc thẻ
và nhiều câu hỏi khác. Điều này giúp các thiết bị chạy Android không có tính năng lưu trữ có thể tương tác với phần cứng USB. Các phụ kiện USB của Android phải được thiết kế để hoạt động với các thiết bị chạy Android và phải tuân thủ giao thức giao tiếp của phụ kiện Android. Ở chế độ hỗ trợ USB, thiết bị chạy Android đóng vai trò là máy chủ lưu trữ. Ví dụ về các thiết bị bao gồm máy ảnh kỹ thuật số, bàn phím, chuột và tay điều khiển trò chơi. Các thiết bị USB được thiết kế cho nhiều ứng dụng và môi trường vẫn có thể tương tác với những ứng dụng Android có thể giao tiếp chính xác với thiết bị.
Hình 1 cho thấy sự khác biệt giữa hai chế độ. Khi ở chế độ lưu trữ, thiết bị chạy Android sẽ hoạt động như USB host và cung cấp năng lượng cho xe buýt. Khi thiết bị chạy Android đang ở chế độ phụ kiện USB, phần cứng USB được kết nối (trong trường hợp này là phụ kiện USB Android) sẽ đóng vai trò là máy chủ lưu trữ và cấp nguồn cho xe buýt.

Hình 1. Chế độ máy chủ và phụ kiện USB
Các chế độ máy chủ và phụ kiện USB được hỗ trợ trực tiếp trong Android 3.1 (API cấp 12) hoặc các nền tảng mới hơn. Chế độ phụ kiện USB cũng được điều chỉnh cho phiên bản cũ là Android 2.3.4 (API cấp 10) dưới dạng một thư viện tiện ích bổ sung để hỗ trợ nhiều thiết bị hơn.
Nhà sản xuất thiết bị có thể chọn có đưa thư viện tiện ích bổ sung vào hình ảnh hệ thống của thiết bị hay không.
Lưu ý: Cuối cùng, khả năng hỗ trợ các chế độ phụ kiện và máy chủ USB sẽ phụ thuộc vào phần cứng của thiết bị, bất kể cấp độ nền tảng. Bạn có thể lọc tìm các thiết bị hỗ trợ máy chủ USB và phụ kiện thông qua phần tử <uses-feature>
.
Xem tài liệu về phụ kiện và máy chủ USB để biết thêm thông tin chi tiết.
Những điều cần lưu ý khi gỡ lỗi
Khi gỡ lỗi ứng dụng sử dụng phụ kiện USB hoặc tính năng lưu trữ, rất có thể bạn đã kết nối phần cứng USB với thiết bị chạy Android. Thao tác này sẽ ngăn bạn kết nối adb
với thiết bị chạy Android qua USB. Bạn vẫn có thể truy cập adb
qua kết nối mạng. Cách bật adb
qua kết nối mạng:
- Kết nối thiết bị chạy Android bằng USB với máy tính của bạn.
- Trong thư mục
platform-tools/
của SDK, hãy nhập adb tcpip 5555
tại dấu nhắc lệnh.
- Nhập
adb connect <device-ip-address>:5555
. Bây giờ, bạn sẽ được kết nối với thiết bị chạy Android và có thể đưa ra các lệnh adb
thông thường như adb logcat
.
- Để đặt thiết bị của bạn ở chế độ nghe trên USB, hãy nhập
adb usb
.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],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`."]]