Take the 2-minute tour ×

I have an USB device with interface that uses alternate settings for its endpoints. How can I select these settings using classes in 'android.hardware.usb' package?

In native code I am using 'libusb_set_interface_alt_setting' function from libUsb, which essentially is IOCTL. However, I wouldn't like to use NDK for that.


share|improve this question
add comment

2 Answers

Just to close this question.

I resolved the issue by writing kernel driver for my device and using it as a char device.

As a side note, I could say that it would be nice to implement set_interface_alt_settings in libusbhost library and use it in 'android.hardware.usb' package through JNI.

share|improve this answer
add comment

To enable an alternative setting for an interface is nothing more than a Standard Device Request according to chapter 9.4 in the USB specification (usb_2.0.pdf).

So this:

UsbDeviceConnection.controlTransfer(UsbConstants.USB_DIR_OUT | 0x01, SET_INTERFACE, _alternate_setting_, _interface_nr_, null, 0, _timeout_);

should work. SET_INTERFACE (= 11 = 0x0B) is a USB specification constant. I could not find it in the android API as Java const.

share|improve this answer
Hi Alexander, thanks for the reply. I read the doc and tried to play with controlTransfer. Unfortunately all values of alt. settings I tried didn't work. I mentioned that bulkTransfer returns 1. Is there something wrong in the sequence of calls I'm using? UsbDevice -> getInterface -> getEndpoint -> openDevice -> claimInterface -> controlTransfer -> bulkTransfer ...... -> close_connection –  aknopov May 8 '12 at 21:02
What do you mean with didn't work? What returns the controlTransfer I suggested? Try the controlTransfer before claimInterface as the controlTransfer somehow changes the associated resources of the interface and our hack doesn't inform libUSB about it, however this is contrary to the libusb recomandation. Note: I never did that before and I can't tell what android is doing under the hood. I'm just telling you what makes sense from reading the USB specs. –  Alexander May 10 '12 at 10:43
By didn't work I mean that data is not sent to device properly. Device receives either 0 or 1 byte from the sent package, though it's a guess - I didn't run sniffer. Regarding controlTransfer - it returns -1 for invalid interface ID and returns 0 for any alt.settings number with valid interface. Regarding order - if controlTransfer is called before claimInterface it returns -1 regardless of parameters. –  aknopov May 10 '12 at 23:32
I don't get it. If controlTransfer returns 0 for the mentioned request, what is the problem we are talking about? 0 means success and request data size is 0. So everything is fine. If you are not sure you really switched to the alternate setting, you may send a Get Interface request. If you are having trouble to communicate after selection of the alternate setting, that's another problem not related to your question. –  Alexander May 11 '12 at 21:04
add comment

Your Answer


