Android USB host DeviceConnection.setInterface prior to API Level 21

大城市里の小女人 提交于 2019-12-12 00:11:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!