rk3188/3288 android 5.x UsbDevice 获取 UsbInterface问题
来源:互联网 发布:如何看自己淘宝等级 编辑:程序博客网 时间:2024/04/29 16:53
问题描述:
假如我有两个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中会针对新设备去新建这些对象.
- rk3188/3288 android 5.x UsbDevice 获取 UsbInterface问题
- rk3188--5.android input 系统架构分析
- Android API 翻译之 UsbDevice
- android的setting语言列表 rk3188
- RK3188从SDCARD中运行Android系统
- RK3188从SDCARD中运行Android系统
- rk3188--6.android 触摸屏驱动分析
- rk3188--7.android gsensor驱动分析
- rk3188--8.android camera驱动分析
- rk3188--9.android camera数据流程分析
- rk3188--10.android i2c驱动分析
- Android中的USB中的UsbAccessory和UsbDevice的区别
- Android中的USB中的UsbAccessory和UsbDevice的区别
- 【整理】Android中的USB中的UsbAccessory和UsbDevice的区别
- Android中的USB中的UsbAccessory和UsbDevice的区别
- Android中的USB中的UsbAccessory和UsbDevice的区别
- android 上安装 mysql 数据库 rk3188测试平台
- rk3188+android在ubuntu上用wifi实现adb调试
- 养老---任重而道远
- C#数据集合与数据结构
- Android 视频播放器VideoView
- 兼容IE、Firefox的背景半透明内容不透明设置
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用JXL生成Excel文件(一)
- rk3188/3288 android 5.x UsbDevice 获取 UsbInterface问题
- 策略模式
- js保留两位小数
- CentOS7自定义开机启动项
- 函数work2
- window平台使用bat批处理实现进程守护
- asp.net mvc3 初学者 用法小结(2)
- 【oracle】查看schema使用情况
- C++第4次试验:项目一(实验)