简体   繁体   English

连接的USB设备的Android设备节点名称

[英]Android device node name for attached usb device

I am currently writing an application for an Android nexus 5 phone. 我目前正在为Android nexus 5手机编写应用程序。 The application uses gstreamer that runs natively to access a usb webcam that has been plugged into the device. 该应用程序使用本地运行的gstreamer访问已插入设备的USB网络摄像头。 In order to access the webcam I need the device node location of the webcam. 为了访问网络摄像头,我需要网络摄像头的设备节点位置。 Generally it is /dev/video3 but sometimes it changes values, ie /dev/video4 通常它是/ dev / video3,但有时会更改值,即/ dev / video4

Currently I have to set the device node location manually for this to work. 目前,我必须手动设置设备节点位置才能正常工作。 I wish to render the process automatic. 我希望使该过程自动化。 Especially given the case where a user could disconnect and reconnect the webcam. 特别是考虑到用户可以断开并重新连接网络摄像头的情况。

So far, I can determine the vendorID, productID from Java, I then pass this info to my C code using JNI. 到目前为止,我可以从Java确定vendorID,productID,然后使用JNI将此信息传递给C代码。

I use the following to detect the USB device info : 我使用以下方法检测USB设备信息:

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) 
        {
            deviceFound = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

            Log.i("TAG", "USB DEVICE ATTACHED");

            deviceFound.getDeviceName();
            deviceFound.getVendorId();
            deviceFound.getProductId();

        }

getDeviceName returns a string that contains "/dev/bus/usb/001/002". getDeviceName返回一个包含“ / dev / bus / usb / 001/002”的字符串。 If I try using this to access my webcam using gstreamer I get the following error "Error getting capabilities for device '/dev/bus/usb/001/002': It isn't a v4l2 driver" 如果我尝试使用此工具通过gstreamer访问我的网络摄像头,则会收到以下错误“获取设备'/ dev / bus / usb / 001/002'的功能时出错:它不是v4l2驱动程序”

I'm assuming that /dev/bus/usb/002/001 isn't quite the same as /dev/video3 in this case. 在这种情况下,我假设/ dev / bus / usb / 002/001与/ dev / video3不太相同。 A few questions : 几个问题 :

1) Can I obtain the correct device node ie /dev/video3 using /dev/bus/usb/001/002 ? 1)我可以使用/ dev / bus / usb / 001/002获得正确的设备节点,即/ dev / video3吗?

2) Is there a way of directly obtaining the device node (/dev/video*) given the USB device's vendor & product identifiers programmatically ? 2)是否可以通过编程方式直接获取USB设备的供应商和产品标识符来直接获取设备节点(/ dev / video *)?

Thanks. 谢谢。

Check the modification time on all candidates in /dev/video* . 检查/dev/video*所有候选者的修改时间。 You are looking for the one close to the time you received USB_DEVICE_ATTACHED action. 您正在寻找接近USB_DEVICE_ATTACHED操作的时间。

Android have USB accessory APIs (android.hardware.usb) in which you can get when any USB device attach or detach from android device and add Android具有USB附件API (android.hardware.usb) ,您可以在其中将任何USB设备从android设备连接或分离并添加时获取

to manifest and this intent filter to 表现出来,这个意图过滤器

<intent-filter>
       <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> 
</intent-filter>

to activity in manifest 明显的活动

UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

from this you can get system service for USB 由此您可以获得USB的系统服务

UsbAccessory[] accessories = mUsbManager.getAccessoryList();

You will get a list of currently attached USB accessories. 您将获得当前连接的USB附件的列表。 it reports its manufacturer and model names, the version of the accessory, and a user visible description of the accessory to the device. 它会报告其制造商和型号名称,配件的版本以及该设备对配件的用户可见描述。

HashMap<String,UsbDevice> result= mUsbManager.getDeviceList()

Returns a HashMap containing all USB devices currently attached. 返回一个HashMap,其中包含当前连接的所有USB设备。 USB device name is the key for the returned HashMap. USB设备名称是返回的HashMap的密钥。 And UsbDevice contains manufacturer and model names, the version etc. UsbDevice包含制造商和型号名称,版本等。

For more detailed description Read Android Developer 有关更多详细说明,请阅读Android Developer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM