简体   繁体   English

Android专家可以解释奇怪的USB主机行为

[英]Can an Android Expert Explain Strange USB Host Behavior

I am writing an Android application to read input from a HID USB foot pedal (press the pedal, get a message, do something). 我正在编写一个Android应用程序,以从HID USB脚踏板读取输入(按下踏板,获取消息,执行某些操作)。

The UsbManager is not recognizing the device. UsbManager无法识别该设备。 The foot pedal may be throwing an error in Android kernel when it plugs in, because I see this error message in the logcat: "EventHub could not get driver version for /dev/input/mouse0, not a typewriter" 脚踏板插入后,可能会在Android内核中引发错误,因为我在logcat中看到以下错误消息:“ EventHub无法获取/ dev / input / mouse0的驱动程序版本,而不是打字机的驱动程序版本”

However, I know the foot pedal works, because when I plug it in and press it, it changes the focus to the next button on the activity... So I know it is communicating with my Nexus tablet and apparently its default action is to move the focus to the next button/object. 但是,我知道脚踏板是有效的,因为当我插入脚踏板并按下它时,它会将焦点切换到活动上的下一个按钮...所以我知道它正在与我的Nexus平板电脑进行通讯,显然它的默认操作是将焦点移到下一个按钮/对象。 I don't think there are any problems with my code, since it will recognize other USB devices, just not this foot pedal. 我认为我的代码没有任何问题,因为它可以识别其他USB设备,而不仅仅是脚踏板。 I can actually tell when it's pressed by checking for when the focus changes, but that won't work for what I want since this app will run in the background as a service. 我实际上可以通过检查焦点何时更改来知道何时按下该按钮,但这不适用于我想要的功能,因为此应用程序将作为服务在后台运行。 I've tried setting an intent filter for this specific USB device (I know its product id and vendor id). 我尝试为此特定的USB设备设置一个意图过滤器(我知道它的产品ID和供应商ID)。 However, it still shows no connected devices and the pop-up message that is supposed to ask the user to confirm launching the application never shows up. 但是,它仍然没有显示已连接的设备,并且应该要求用户确认启动该应用程序的弹出消息从不显示。 I've tried just listing all the connected USB devices as well, but I always get an empty list. 我也尝试只列出所有连接的USB设备,但是我总是得到一个空列表。

Is there any way to intercept input from this device so I can tell when the foot pedal gets pressed, even though Android's USB Manager will not recognize it? 有什么方法可以拦截来自此设备的输入,以便即使Android的USB管理器无法识别踩脚踏板的时间,我也可以知道。

For completeness, here is my code. 为了完整起见,这是我的代码。 I am testing on a Galaxy Nexus 10 tablet: 我正在Galaxy Nexus 10平板电脑上进行测试:

public int list_usb_devices()
{
    int device_count = 0;
    UsbManager mUsbManager;
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    String LOG_TAG = "USB";
    for (UsbDevice device : mUsbManager.getDeviceList().values()) {

        //This code is never reached...

        Log.d(LOG_TAG, "Detected device: " + device.toString());
        Log.d(LOG_TAG, "Model: " + device.getDeviceName());
        Log.d(LOG_TAG, "Id: " + device.getDeviceId());
        Log.d(LOG_TAG, "Class: " + device.getDeviceClass());
        Log.d(LOG_TAG, "Protocol: " + device.getDeviceProtocol());
        Log.d(LOG_TAG, "VendorId: " + device.getVendorId());
        Log.d(LOG_TAG, "ProductId: " + device.getProductId());

        CharSequence text = device.toString();
        show_toast(text);

        device_count++;
    }
    return device_count;
}

I did some research in the Android source and it seems that all HID boot devices (mouse, keyboard etc.) are blacklisted and can therefore not be accessed using the USBManager API. 我在Android来源中进行了一些研究,似乎所有HID引导设备(鼠标,键盘等)都已列入黑名单,因此无法使用USBManager API进行访问。

Here is the relevant part from the UsbHostManager.java , see here: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/com/android/server/usb/UsbHostManager.java/?v=source 这是UsbHostManager.java的相关部分,请参见此处: http : //grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/com/android/ server / usb / UsbHostManager.java /?v = source

/* returns true if the USB device should not be accessible by applications */
private boolean isBlackListed(int clazz, int subClass, int protocol) {
    // blacklist hubs
    if (clazz == UsbConstants.USB_CLASS_HUB) return true;

    // blacklist HID boot devices (mouse and keyboard)
    if (clazz == UsbConstants.USB_CLASS_HID &&
            subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) {
        return true;
    }

    return false;
}

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

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