USB开发:5.1系统(多个USB设备通信失败,USBInterfaceCount为0)

本文分析了在Android 5.1系统中,当连接多个USB设备时出现通信失败的问题。问题源于UsbHostManager在处理新设备时,由于未正确清空mNewConfiguration和mNewInterface,导致第二次添加设备时,第一个设备的UsbInterface被错误覆盖,从而使得其中一个设备无法正常通信。在Android 6.0及以上版本,这个问题已被修复,通过在初始化结束时清空相关对象,避免了接口复用错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻看UsbManager源码,发现其构造方法有两个参数,一个是Context对象,一个是IUSBManger对象,IUsbManager是aidl接口,具体实现类是UsbService,UsbService的构造方法中会初始化UsbHostManger和UsbDeviceManager。
以UsbHostManager为例。UsbManager中getDeviceList()方法,实际是调用UsbHostManager中getDeviceList(Bundle b),这里的Bundle对象是UsbManager中getDeviceList()方法创建的,在UsbHostManager中对该Bundle对象赋值。
源码如下:

   public void getDeviceList(Bundle devices) {
            synchronized (mLock) {
                 for (String name : mDevices.keySet()) {
                     devices.putParcelable(name, mDevices.get(name));
                                                                               }
                                          }
    }

HashMap, UsbDevice> mDevices; 该对象用来存储获取的USB设备。具体是如何给mDevices赋值,接着翻看源码。
系统启动后,会调用JNI 中monitorUsbHostBus()方法,来反馈usb设备,具体源码在frameworks\base\services\core\jni\com_android_server_UsbHostManager.cpp该类会调用UsbHostManager中的几个方法,以添加usb设备为例。

private UsbDevice mNewDevice;
    private UsbConfiguration mNewConfiguration;
    private UsbInterface mNewInterface;
    private ArrayList mNewConfigurations;
    private ArrayList mNewInterfaces;
    private ArrayList mNewEndpoints;

    private boolean beginUsbDeviceAdded(String deviceName, int vendorID, int productID,
                                        int deviceClass, int deviceSubclass, int deviceProtocol,
                                      
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值