如何在 Android 上管理 USB?

How to manage USB on Android?

代码思路:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String,UsbDevice> stringUsbDeviceHashMap=manager.getDeviceList();
    Log.e("asdadd", String.valueOf(stringUsbDeviceHashMap.size()));
}

5310-5310/com.example.AppUSB E/asdadd﹕ 0

命令亚行:

 C:\Users\Serg>adb devices
 List of devices attached
 DMJNPRCAP7IJGQFA        device

如果我们有 1 个单一连接,为什么我得到的连接设备数为 0?

adb 从您的计算机的角度列出连接的设备,而 UsbManager.getDeviceList() 从字面上列出与您的 Android 设备连接的 USB 设备...

UsbManager.getDeviceList() returns UsbDevice 的列表,由 documentation:

定义

This class represents a USB device attached to the android device with the android device acting as the USB host. Each device contains one or more UsbInterfaces, each of which contains a number of UsbEndpoints (the channels via which data is transmitted over USB).