没有来自Android BulkTransfer的响应和正确的端点

时间:2017-02-03 11:38:08

标签: java android connection usb usb-otg

我已经在这里看到很多关于bulkTransfer的问题,他们中的大多数都使用了错误的接口进行读写,但是我确定已经选择了正确的接口:

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方法获取positiv反馈。然后我等待一段时间(100或甚至1000毫秒)并尝试readData。我从来没有得到任何数据来读取并始终-1作为反馈(直到方法在5秒后返回一个空数组)。我已经尝试将mReadData数组的大小更改为其他值但到目前为止没有任何工作(在PTP协议1中,30字节数据的大小将是典型ResponseData块的预期值)。

总体而言,我尝试使用我的智能手机(运行Android 6.0.1的Galaxy S7)使用原装USB-OTG适配器从我的尼康D3200读取数据,并且我非常确定我的代码是问题但是经过几个小时尝试不同的东西我真的不知道问题是什么。

以下是我使用过的ISO 15740规范的链接:Link to ISO 15740

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

提前致谢, 多米尼克

1 个答案:

答案 0 :(得分:0)

以下代码无效

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 {...}块或 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