API 级别 21 之前的 Android USB 主机 DeviceConnection.setInterface [英] Android USB host DeviceConnection.setInterface prior to API Level 21

查看:34
本文介绍了API 级别 21 之前的 Android USB 主机 DeviceConnection.setInterface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要与之通信的 USB 设备,我的代码使用 NDK 代码使用 JNI 调用 USB 主机 API.

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.

然而,它涉及对 DeviceConnection.setInterface() 的调用,这是一个 API 21+ 调用.如果我忽略该调用,则 control- 和 bulkTransfers 将失败.

However it involves a call to DeviceConnection.setInterface(), which is an API 21+ call. If I leave that call out, control- and bulkTransfers fail.

API 21 之前的配置是如何设置的?默认情况下为 DeviceConnection 选择哪个 UsbInterface?我确实调用了 claimInterface,但它仍然不起作用.

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.

有什么办法可以只使用 API 19 调用来做到这一点,或者我可以直接使用 libusb 做到这一点吗?

Is there any way to do this using API 19 calls only, or alternatively can I do this directly using libusb?

推荐答案

我最终求助于本机代码来调用与 UsbDeviceConnection.setInterface() 相同的 usbfs 代码:

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);

注意 gUsbDeviceConnection.getFileDescriptor(env);line 是我的 JNI 包装器,用于从 C++ 调用 Java UsbDeviceConnection.getFileDescriptor 方法 - 您的方法可能会有所不同.

Note that the gUsbDeviceConnection.getFileDescriptor(env); line is my JNI wrapper for calling the Java UsbDeviceConnection.getFileDescriptor method from C++ - your method may vary.

这在 API 19 和 21 上对我有用.

This worked for me on API 19 and 21.

这篇关于API 级别 21 之前的 Android USB 主机 DeviceConnection.setInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆