Search code examples
androidusbdevicehid

What Android APIs do I need to perform a USB control transfer (setup, data, status) on a HID device?


I am developing the driver for a USB HID controller linked to my Android device using USB-host. All vendor-specific operations enabling me to poll the status of the controller, or to set different flags, are defined as vendor-specific control commands which contain a setup stage and a data stage (device -> controller). Depending on the different commands, the controller may send back a status stage message.

I understood that the method UsbDeviceConnection.controlTransfer() shall be used to send the setup and data stages. What I don't understand is what method/API I should use to read the status sent back by the controller.

Does someone know?

Thank you very much!


Solution

  • According to the well-known open source project https://github.com/mik3y/usb-serial-for-android, that method returns -1 if there's something wrong.

    int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST, SIO_RESET_SIO, 0 /* index */, null, 0, USB_WRITE_TIMEOUT_MILLIS); if (result != 0) { throw new IOException("Reset failed: result=" + result); }

    (Code extracted from FtdiSerialDriver.java)

    I know it, because that's what is happening to me !!