Java 没有来自具有正确端点的传输的响应
我在这里已经看到了很多关于bulkTransfer的问题,大多数问题都使用了错误的接口进行读写,但我确信我选择了正确的接口:Java 没有来自具有正确端点的传输的响应,java,android,connection,usb,usb-otg,Java,Android,Connection,Usb,Usb Otg,我在这里已经看到了很多关于bulkTransfer的问题,大多数问题都使用了错误的接口进行读写,但我确信我选择了正确的接口: private UsbManager usbManager; private UsbDevice usbDevice; private UsbDeviceConnection usbDeviceConnection; private UsbInterface usbInterface; private UsbEndpoint readEndpoint; private U
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;
}
我可以通过writeData方法发送数据并获得积极反馈。然后我等待一段时间(100甚至1000毫秒)并尝试读取数据。而且我从来没有得到过任何要读取的数据,并且总是得到-1作为反馈(直到该方法在5秒后返回一个空数组)。我已经尝试将mReadData数组的大小更改为其他值,但到目前为止没有任何效果(30字节的数据大小将是PTP协议中典型ResponseData块的预期值)
总的来说,我试着用我的智能手机(运行Android 6.0.1的Galaxy S7)和原始USB-OTG适配器读取尼康D3200的数据,我很确定我的代码是问题所在,但在尝试了几个小时不同的事情后,我真的不知道问题出在哪里
以下是我使用过的ISO 15740标准的链接:
也许你们中有人知道我有什么问题
提前感谢,,
Dominik以下代码将不起作用
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!");
}
并使用
writeData(usbDeviceConnection, ...);
()
- 配置不正确的协议(即主机的预期
与设备的maxPacketSize
,…)maxPacketSize
- 权限()
readData()
方法
感谢您的快速响应!它不能是引用错误,因为代码在同一个类中。我已经实现了给定的write方法,它工作得很好(输出是:“success!”)。我尝试过getMaxPacketSize来定义发送和读取的字节数组的大小,但也没有成功。为了确保没有权限问题,我尝试了usbManager.haspowpermission(usbDevice)——结果为true。输入和输出端点的getMaxPacketSize()?writeEndpoint.getMaxPacketSize()=512和readEndpoint.getMaxPacketSize()=512这意味着您至少获得了端点描述符,但无法读取/写入端点。我不知道原因是什么,也许可以看看libptp的源代码(在C中)看看PTP协议中是否有特殊功能……这很糟糕,因为我已经看过一些PTP协议(例如,我给出了描述PTP的规范的链接)。无论如何谢谢你,至少代码现在更干净了,也许我能找到问题。
private UsbDeviceConnection usbDeviceConnection;
private boolean writeData(UsbDeviceConnection usbDeviceConnection, byte[] data) { ... }
writeData(usbDeviceConnection, ...);