Connecting a physical USB device (like a sensor, serial adapter, or specialized peripheral) to the Android Emulator can be a complex task because the emulator is a virtualized environment. By default, it does not "see" the hardware ports of your computer.
To connect a USB device to an Android emulator better, you must use USB Passthrough, a method that tells the underlying virtualization engine (QEMU) to bypass the host operating system and give the emulator direct access to the USB hardware. 1. Prerequisites for USB Passthrough
Before attempting a connection, ensure your environment is set up for success:
Use x86 or x86_64 Images: Most ARM-based emulator images do not support USB controllers, making passthrough nearly impossible without custom kernel work.
Identify Your Device: You need the VendorID and ProductID of your USB device. On Linux, run lsusb; on Windows, find this in the "Details" tab of the device's properties in Device Manager.
Install Necessary Drivers: On Windows, you may need the Google USB Driver installed via the Android Studio SDK Manager to ensure consistent communication. 2. Method 1: Command-Line Passthrough (Most Reliable)
The most effective way to connect a USB device is to launch the emulator from the command line with specific QEMU arguments.
Locate your emulator executable: Typically found in ~/Android/Sdk/tools/ or ~/AppData/Local/Android/Sdk/emulator/. Run the passthrough command:
emulator -avd Your_Device_Name -qemu -usb -device usb-host,vendorid=0xXXXX,productid=0xXXXX Use code with caution. Replace 0xXXXX with your actual hex IDs.. 3. Method 2: Modern "USB-Passthrough" Flag
Newer versions of the Android Emulator (specifically for Automotive or Android 13+) support a more direct flag that simplifies the process:
emulator -avd Your_Device_Name -usb-passthrough vendorid=0xXXXX,productid=0xXXXX Use code with caution.
This method is often used for testing Bluetooth dongles or specialized serial devices. 4. Method 3: Third-Party Alternatives
If the standard Android Studio emulator is too restrictive, consider these alternatives:
Genymotion: Often cited for better hardware support, Genymotion allows you to toggle USB devices on and off directly from a sidebar menu.
Oracle VirtualBox with Android-x86: If you run an Android-x86 image in VirtualBox, you can use the Devices > USB menu to easily attach physical hardware to the running VM.
WSA (Windows Subsystem for Android): For Windows users, WSA offers a more integrated experience, though USB support is currently limited to specific classes like keyboards and game controllers. 5. Troubleshooting Tips for Better Connectivity
Permissions (Linux): You often need to create a udev rule to allow the emulator to access the /dev/bus/usb/ path without root privileges.
Enable USB Host in Android: Some emulator images have the USB Host feature disabled in the software. You may need to create an XML file at /system/etc/permissions/android.hardware.usb.host.xml to tell the Android OS to look for USB devices.
Disconnect from Host: Ensure the host OS (Windows/Mac) isn't "claiming" the device (e.g., a serial monitor or specialized driver) while the emulator is trying to grab it.
Connecting a physical USB device (like a sensor, controller, or specialized dongle) to an Android Emulator requires USB Passthrough
. This is different from connecting a physical phone to your computer for debugging.
The most reliable way to achieve this is via the command line using specific flags that tell the emulator to "grab" the host's USB port. 1. Identify the USB Device First, you must find the specific Product ID of the device you want to connect to the emulator. On Linux/macOS: Open a terminal and run . Look for your device and note the hexadecimal IDs (e.g., On Windows: Device Manager
. Right-click your device > Properties > Details > Hardware IDs. Android Developers 2. Launch Emulator via Command Line
Standard GUI-launched emulators in Android Studio often do not support full hardware passthrough. You must launch the emulator from your terminal using the following syntax:
emulator -avd
emulator -avd Pixel_8 -usb-passthrough vendorid=0x0b05,productid=0x17cb Android Open Source Project 3. Advanced QEMU Passthrough (Alternative) If the standard -usb-passthrough flag fails, you can use raw
arguments. This often requires root/sudo privileges to access the host hardware.
sudo emulator -avd
. Windows support has historically been limited to specific Canary/Dev channels of the emulator. Permissions: On Linux, ensure your user is in the
group to avoid permission errors when accessing USB devices. Third-Party Tools: For simple storage devices, VirtualBox
can sometimes act as a host for Android-x86 images, allowing you to use its built-in USB filters. Debugging Hardware: If you are debugging an app that uses
features, the physical connection to the computer for ADB might interfere with the hardware you're trying to test. In these cases, it is often better to switch to Wireless Debugging after the initial setup. Android Developers What specific type of USB device
(e.g., thermal camera, specialized sensor, game controller) are you trying to connect? Run apps on a hardware device | Android Studio
Connecting physical USB devices to an Android Emulator is notoriously difficult because the emulator runs inside a virtual machine (QEMU), which creates a layer of abstraction between the guest OS (Android) and the host OS (Windows/macOS/Linux).
Here is an interesting post-style breakdown of how to solve this, ranging from the "easy way" to the "hard way."
Part 5: For macOS – The Hard Truth
Apple’s hypervisor framework does not support USB passthrough to QEMU easily. The best method on macOS is:
4.3 Why Ethernet and not ADB?
- ADB’s
forwardadds per‑packet overhead (unnecessary protocol framing). USB/IP uses raw UDP/TCP bulk streaming, preserving isochronous timing.
The Catch
- Linux only (detailed below for Windows/macOS workarounds).
- Requires root on the host to unbind drivers.
- Destroys host access until reboot or rebinding.
3. Permission Popups Will Haunt You
Even when the device appears in /dev/bus/usb on the host, the emulated Android will still ask the user for USB permission via UsbManager.requestPermission(). There is no "auto-approve" flag. For automation, you must:
- Use
adb shell pm grantfor your app (not sufficient for USB). - Or patch
frameworks/base(impractical). Better to handle the permission dialog programmatically viaUiAutomatorin tests.
The Pros
- No extra software. It's built into the Android SDK.
- Works for simple devices (serial-to-USB converters, some input devices).
Chapter 1: Why "Better" Matters – The Problem with Default Emulation
Before diving into solutions, let's diagnose the problem. The Android Emulator is based on QEMU (Quick Emulator). When you run an AVD, the emulator creates a virtual "Goldfish" or "Ranchu" kernel. This kernel has its own virtual USB stack.
By default, the emulator passes through only a handful of device classes (keyboard, mouse, touch). Everything else—mass storage, HID barcode scanners, ADB interfaces—is blocked or ignored.
The "Bad" Way: Port Forwarding & Mocking
Many developers give up and mock USB data. They write scripts that read from /dev/ttyUSB0 on Linux and inject KeyEvent objects into the emulator. This is fragile, slow, and doesn't test the real UsbManager APIs.
The "Better" Way: True Hardware Passthrough Better means the emulator's Android OS sees the USB device as if it were plugged into a physical phone. You should be able to:
- See the device in
lsusb(within the emulator's shell). - Request USB permission via
UsbManager. - Achieve actual USB host speeds (not network fakes).
Let's achieve this.
Part 1: Understanding the Problem – Why Doesn’t It Just Work?
Before fixing the issue, let’s diagnose the anatomy of the problem.
When you plug a USB device into your host computer (Windows, macOS, or Linux), the host OS claims it. The Android Emulator runs as a virtual machine (VM) with virtualized hardware. By default, the emulator sees:
- A virtual GPS sensor
- Virtual accelerometers
- A virtual battery
- A virtual SD card
But it does not see your physical USB desk fan or MIDI keyboard. Why? Because the host OS doesn’t automatically forward USB interrupts to QEMU (the underlying emulation engine).
The core challenge: You need to detach the USB device from the host driver and attach it to the emulated Android environment. This requires a translation layer—either ADB (Android Debug Bridge) or virtual USB passthrough.