问题
I have a USB device I need to communicate with, and I have the code working using NDK code using JNI calls to the USB host APIs.
However it involves a call to DeviceConnection.setInterface(), which is an API 21+ call. If I leave that call out, control- and bulkTransfers fail.
How is the configuration set prior to API 21? Which UsbInterface is selected for a DeviceConnection by default? I do call claimInterface, but it still doesn't work.
Is there any way to do this using API 19 calls only, or alternatively can I do this directly using libusb?
回答1:
I ended up resorting to native code to call the same usbfs code that UsbDeviceConnection.setInterface() does:
#include <linux/ioctl.h>
#include <sys/ioctl.h>
// Struct and ioctl define stolen from linux_usbfs.h
struct usbfs_setinterface {
/* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */
unsigned int interface;
unsigned int altsetting;
};
#define IOCTL_USBFS_SETINTF _IOR('U', 4, struct usbfs_setinterface)
// Basically the same as linux_usbfs.c
int fd = gUsbDeviceConnection.getFileDescriptor(env);
struct usbfs_setinterface setintf;
setintf.interface = CIMAX_INTERFACE;
setintf.altsetting = alternate;
int r = ioctl(fd, IOCTL_USBFS_SETINTF, &setintf);
Note that the gUsbDeviceConnection.getFileDescriptor(env); line is my JNI wrapper for calling the Java UsbDeviceConnection.getFileDescriptor method from C++ - your method may vary.
This worked for me on API 19 and 21.
来源:https://stackoverflow.com/questions/38102677/android-usb-host-deviceconnection-setinterface-prior-to-api-level-21