I have already seen lot's of questions here about bulkTransfer, most of them have used the wrong interfaces to read and write but I'm quit sure that I have choosen the proper interfaces:
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;
}
This part of the Code seems to work in the way it should, then there are two other methods to read and write data:
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"));
}
}
}
I can send data and get positiv feedback from the writeData Method. Then I wait for some time (100 or even 1000ms) and try to readData. And I have never gotten any data to read and get always -1 as feedback (until the Method returns a empty array after 5 seconds). I have already tried to change the size of the mReadData array to other values but nothing has worked so far (a size of 30bytes of data would be the expected value for a typical ResponseData block at the PTP Protocoll).
Overall I try to read data from my Nikon D3200 with my Smartphone (Galaxy S7 with Android 6.0.1 running) with the original USB-OTG Adapter and im quite sure that my Code is the problem but after hours of trying different things I have really no idea what the problem is.
Here is a Link to the ISO 15740 Norm that I have used: Link to ISO 15740
Maybe someone of you has an idea what's my Problem ?
Thanks in advance, Dominik