问题描述:
假如我有两个USB设备 A和B, 先接入A, 再接入B, 会发现, 获取设备A的 UsbInterface的时候, 是空的, 单接A 或 B都是可以正常工作的.
解决过程:
根据USB设备接入LOG中可看到:
04-10 15:32:15.161 392-499/system_process D/UsbHostManager: Added device UsbDevice[mName=/dev/bus/usb/002/008,mVendorId=6408,mProductId=550,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=null,mSerialNumber=null,mConfigurations=[
UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=75,mInterfaces=[
UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=8,mSubclass=6,mProtocol=80,mEndpoints=[
UsbEndpoint[mAddress=1,mAttributes=2,mMaxPacketSize=512,mInterval=0]
UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=512,mInterval=0]]]]
而应用程序中获取USB设备的方法是:
UsbManager usbMgr = (UsbManager)getSystemService(USB_SERVICE); HashMap<String ,UsbDevice> devices = usbMgr.getDeviceList();
所有的问题都指向一个地方: UsbHostManager
源码路径:
frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java
当中有几个关键的方法:
/* Called from JNI in monitorUsbHostBus() to report new USB devices Returns true if successful, in which case the JNI code will continue adding configurations, interfaces and endpoints, and finally call endUsbDeviceAdded after all descriptors have been processed */ private boolean beginUsbDeviceAdded(String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, String manufacturerName, String productName, String serialNumber) { if (DEBUG) { Slog.d(TAG, "usb:UsbHostManager.beginUsbDeviceAdded(" + deviceName + ")"); // Audio Class Codes: // Audio: 0x01 // Audio Subclass Codes: // undefined: 0x00 // audio control: 0x01 // audio streaming: 0x02 // midi streaming: 0x03 // some useful debugging info Slog.d(TAG, "usb: nm:" + deviceName + " vnd:" + vendorID + " prd:" + productID + " cls:" + deviceClass + " sub:" + deviceSubclass + " proto:" + deviceProtocol); } // OK this is non-obvious, but true. One can't tell if the device being attached is even // potentially an audio device without parsing the interface descriptors, so punt on any // such test until endUsbDeviceAdded() when we have that info. if (isBlackListed(deviceName) || isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) { return false; } synchronized (mLock) { if (mDevices.get(deviceName) != null) { Slog.w(TAG, "device already on mDevices list: " + deviceName); return false; } if (mNewDevice != null) { Slog.e(TAG, "mNewDevice is not null in endUsbDeviceAdded"); return false; } mNewDevice = new UsbDevice(deviceName, vendorID, productID, deviceClass, deviceSubclass, deviceProtocol, manufacturerName, productName, serialNumber); mNewConfigurations = new ArrayList<UsbConfiguration>(); mNewInterfaces = new ArrayList<UsbInterface>(); mNewEndpoints = new ArrayList<UsbEndpoint>(); } return true; } /* Called from JNI in monitorUsbHostBus() to report new USB configuration for the device currently being added. Returns true if successful, false in case of error. */ private void addUsbConfiguration(int id, String name, int attributes, int maxPower) { if (mNewConfiguration != null) { mNewConfiguration.setInterfaces( mNewInterfaces.toArray(new UsbInterface[mNewInterfaces.size()])); mNewInterfaces.clear(); } mNewConfiguration = new UsbConfiguration(id, name, attributes, maxPower); mNewConfigurations.add(mNewConfiguration); } /* Called from JNI in monitorUsbHostBus() to report new USB interface for the device currently being added. Returns true if successful, false in case of error. */ private void addUsbInterface(int id, String name, int altSetting, int Class, int subClass, int protocol) { if (mNewInterface != null) { mNewInterface.setEndpoints( mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()])); mNewEndpoints.clear(); } mNewInterface = new UsbInterface(id, altSetting, name, Class, subClass, protocol); mNewInterfaces.add(mNewInterface); } /* Called from JNI in monitorUsbHostBus() to report new USB endpoint for the device currently being added. Returns true if successful, false in case of error. */ private void addUsbEndpoint(int address, int attributes, int maxPacketSize, int interval) { mNewEndpoints.add(new UsbEndpoint(address, attributes, maxPacketSize, interval)); } /* Called from JNI in monitorUsbHostBus() to finish adding a new device */ private void endUsbDeviceAdded() { if (DEBUG) { Slog.d(TAG, "usb:UsbHostManager.endUsbDeviceAdded()"); } if (mNewInterface != null) { mNewInterface.setEndpoints( mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()])); } if (mNewConfiguration != null) { mNewConfiguration.setInterfaces( mNewInterfaces.toArray(new UsbInterface[mNewInterfaces.size()])); } synchronized (mLock) { if (mNewDevice != null) { mNewDevice.setConfigurations( mNewConfigurations.toArray(new UsbConfiguration[mNewConfigurations.size()])); mDevices.put(mNewDevice.getDeviceName(), mNewDevice); Slog.d(TAG, "Added device " + mNewDevice);//------------------------------Log 是从这里输出的 getCurrentSettings().deviceAttached(mNewDevice); mUsbAudioManager.deviceAdded(mNewDevice); } else { Slog.e(TAG, "mNewDevice is null in endUsbDeviceAdded"); } mNewDevice = null; mNewConfigurations = null; mNewInterfaces = null; mNewEndpoints = null; } }
问题根源在:
/* Called from JNI in monitorUsbHostBus() to report new USB configuration for the device currently being added. Returns true if successful, false in case of error. */ private void addUsbConfiguration(int id, String name, int attributes, int maxPower) { if (mNewConfiguration != null) {//---------问题在这里, mNewConfiguration指向的是上一个接入的USB设备的Configuration, 下面的代码, 把它清掉了 mNewConfiguration.setInterfaces( mNewInterfaces.toArray(new UsbInterface[mNewInterfaces.size()])); mNewInterfaces.clear(); } mNewConfiguration = new UsbConfiguration(id, name, attributes, maxPower); mNewConfigurations.add(mNewConfiguration); }
解决:
在beginUsbDeviceAdded 这个方法中, 我加入了
mNewConfiguration = null;
mNewInterface = null;
后面, addConfiguration中会针对新设备去新建这些对象.