简体   繁体   中英

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).

The UsbManager is not recognizing the device. 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"

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. I don't think there are any problems with my code, since it will recognize other USB devices, just not this foot pedal. 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). 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.

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?

For completeness, here is my code. I am testing on a Galaxy Nexus 10 tablet:

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.

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

/* 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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