2

I work on a project where I need get data of keyboard and mouse on a service. I also have to be able to send data to my device hid. To do this, I want use the usb host mode. When I get list of device, I can see a mass storage but no mouse and keyboard. After search, I have seen that usbManager don't return mouse and keyboard device. I have tried to change permissions (on /system/etc/permissions) whitout success). I have run the application USB Device Info and I see mouse and keyboard on linux device and not on android devices.

I use android 4.2.2. Is it possible to get hid data of mouse (and keyboard) with usb host or I have to find an other way ?

Thank you for your help

helene
  • 1,201
  • 2
  • 18
  • 30

2 Answers2

3

You can detect mouse/keyboard via InputManager:

if(Build.VERSION.SDK_INT > 15) {
    InputManager inptmgr = (InputManager)getSystemService(INPUT_SERVICE);
    int[] inputs = inptmgr.getInputDeviceIds();
    for(int i = 0;i<inputs.length;i++) {
        String devicename = inptmgr.getInputDevice(inputs[i]).getName();
        if(devicename.toLowerCase().contains("mouse")) {

        } else if(devicename.toLowerCase().contains("keyboard")) {

        }
    }
}

As for any other data you may want to collect, you can browse all the other methods under getInputDevice() such as getVendorId(), getProductId(), getMotionRange(), getKeyCharacterMap(), and others.

Hope this helps!

Aaron Gillion
  • 2,227
  • 3
  • 19
  • 31
  • Thank you for your answer. I can see my device. Now the problem is to communicate with it. In fact, I think I rather need of UsbDevice. – helene Apr 21 '15 at 08:33
  • 1
    Thanks, this is what I was looking for for a long time! I was confused by UsbManager recognizing one of the devices, and not recognizing the other, which I knew was working. I spent some hours trying to figure out what might be wrong with my code, or hardware. Now I tested it with InputManager, and it's found there! – Kurtevich Apr 30 '15 at 08:42
  • Yes, InputManager will detect *all* inputs, including stylus. You can probably combine it with UsbManager to detect a usb drive *in addition to* the mouse/keyboard. – Aaron Gillion Apr 30 '15 at 12:18
0

If you can already use the HID device for input, it' working as a input device(keyboard or mouse) and you don't need to access it as a USB device. You can simply catch the input from the device via listeners such as OnKeyListener.

sinihong
  • 79
  • 1
  • 2