java - 具有正确端点的 Android BulkTransfer 没有响应

标签 java android connection usb usb-otg

我已经在这里看到了很多关于 bulkTransfer 的问题,其中大部分都使用了错误的接口(interface)来读写,但我确信我选择了正确的接口(interface):

private UsbManager usbManager;
private UsbDevice usbDevice;
private UsbDeviceConnection usbDeviceConnection;
private UsbInterface usbInterface;
private UsbEndpoint readEndpoint;
private UsbEndpoint writeEndpoint;
private UsbEndpoint interruptEndpoint;
private PtpSession ptpSession;

public boolean Connect(UsbManager usbManager,UsbDevice usbDevice, PtpSession ptpSession)
{
    if(usbManager == null ||usbDevice == null||ptpSession == null)
        return false;
    else
    {
        this.usbManager = usbManager;
        this.usbDevice = usbDevice;
        this.ptpSession = ptpSession;

        for (int i = 0; i < this.usbDevice.getInterfaceCount(); i++) {
            UsbInterface uintf = this.usbDevice.getInterface(i);
            if (uintf.getInterfaceClass() == UsbConstants.USB_CLASS_STILL_IMAGE) {
                Log.d(TAG, "Imaging USB interface found");
                this.usbInterface = uintf;
                break;
            }
        }
    }

    // find the wright usb endpoints
    if(usbInterface == null)
        return false;
    else
    {
        for(int i = 0; i < usbInterface.getEndpointCount();i++)
        {
            UsbEndpoint ep = usbInterface.getEndpoint(i);
            if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                if(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
                {

                    readEndpoint = ep;
                }
                else if(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
                {
                    interruptEndpoint = ep;

                }
            } else if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                writeEndpoint = ep;
            }
        }

        this.usbDeviceConnection = usbManager.openDevice(usbDevice);
        usbDeviceConnection.claimInterface(usbInterface,true);
    }

    if(readEndpoint == null || writeEndpoint == null ||interruptEndpoint == null)
        return false;
    return true;
}

这部分代码貌似能正常工作,那么还有另外两种读写数据的方法:

private boolean writeData(byte[] data)
{
    int retry = 0;
    while (usbDeviceConnection.bulkTransfer(writeEndpoint, data, data.length, 5000) != data.length)
    {
        retry++;
        if (retry > 4)
        {
            return false;
        }
    }
    return true;
}

private byte[] readData()
{
    long rStart = System.currentTimeMillis();
    byte[] mReadData = new byte[30];
    while (true) {
        int bytesCount = usbDeviceConnection.bulkTransfer(readEndpoint, mReadData, mReadData.length, 5000);
        if (bytesCount >= 0) {
            return mReadData;
        }
        else if (System.currentTimeMillis() - rStart > 5000) {
            return new byte[]{};
        } else {
            Log.e(TAG, String.format("Bulk read -1 cmd"));
        }


    }
}

我可以发送数据并从 writeData 方法获得积极的反馈。然后我等待一段时间(100 甚至 1000 毫秒)并尝试读取数据。而且我从来没有得到任何数据来读取并且总是得到 -1 作为反馈(直到方法在 5 秒后返回一个空数组)。我已经尝试将 mReadData 数组的大小更改为其他值,但到目前为止没有任何效果(30 字节的数据大小将是 PTP 协议(protocol)中典型 ResponseData block 的预期值)。

总的来说,我尝试使用带有原始 USB-OTG 适配器的智能手机(运行 Android 6.0.1 的 Galaxy S7)从我的尼康 D3200 读取数据,我非常确定我的代码是问题所在,但在尝试了数小时不同的东西之后我真的不知道问题出在哪里。

这是我使用的 ISO 15740 规范的链接:Link to ISO 15740

也许你们中有人知道我的问题是什么?

提前致谢, 多米尼克

最佳答案

下面的代码将不起作用

private boolean writeData(byte[] data)
{
    int retry = 0;
    while (usbDeviceConnection.bulkTransfer(writeEndpoint, data, data.length, 5000) != data.length)
    {
        retry++;
        if (retry > 4)
        {
            return false;
        }
    }
    return true;
}  

改为使用以下检查连接状态

 int ret = usbDeviceConnection.bulkTransfer(writeEndpoint, data, data.length, 5000;


     if(ret < 0)
                    {
                      Log.d("TAG", "Error happened!");
                    }
                    else if(ret == 0)
                    {
                        Log.d("TAG", "No data transferred!");
                    }
                    else
                    {
                        Log.d("TAG", "success!");
                    }

(https://www.developer.android.com/studio/debug/am-logcat.html 除了记录之外,您还可以使用 try {...} catch {...} block 或 logcat,...)

复制自android usb UsbDeviceConnection.bulkTransfer returns -1

那么有多种可能为什么你没有写入/读取数据

  • 编程错误(可能是引用错误,因为writeData() 方法不知道对usbDeviceConnection 对象的引用) 尝试将它作为参数提供给方法,例如

    private UsbDeviceConnection usbDeviceConnection;
    private boolean writeData(UsbDeviceConnection  usbDeviceConnection, byte[] data) { ... }
    

    并调用方法

     writeData(usbDeviceConnection, ...);
    

( http://www.programcreek.com/java-api-examples/index.php?api=android.hardware.usbUsbDeviceConnection )

同样适用于 readData() 方法

https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html

关于java - 具有正确端点的 Android BulkTransfer 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023388/

相关文章:

java - 我如何执行以下转换?

android - 将使用 AndroidX 的自定义 .aar 库添加到 Cordova 插件

java - 消失的默认构造函数

php - 将 Laravel 连接到另一个 IP 上的数据库

python - 如何以编程方式确定(在 Python 中)何时有人通过 RDP 连接到我的 Windows 7 计算机?

java - EJB、休息、JPA NullPointerException

java - Swing 跌落 block

java - 如何模拟构造函数中设置的属性

android - posix setuid 不同进程

c# - 不调用 Response.Flush 仍然发送连接 : Close in the Response