FabianCook
FabianCook

Reputation: 20587

USB transfer Android

I have been trying to figure this out for a while now, but every way I have seen just doesn't work, I am trying to send some string data to a USB device, the device should either send back the data I wanted or send back "bad command" which is about 11 bytes right? For now I am getting back is just 2 bytes, 1 and 192. I am trying to just send 9 bytes in total, "$fdump G" and "/n". No matter what I do it just doesn't work.

Here's my code as well, I am not sure how to change the bytes to a string or char but I will do that once I start getting back data because I know that what is meant to return is at least 11 bytes.

for(;;){//this is the main loop for transferring    

                String get = "$fDump G\n";
                l("Sending: " + get);

                byte[] by = get.getBytes();

                //This is where it sends
                l("out " + conn.bulkTransfer(epOUT, by, by.length, 500));

                //This is where it is meant to receive
                byte[] buffer = new byte[4096];
                l("in " + conn.bulkTransfer(epIN, buffer, 4096,500));

                StringBuilder byStr = new StringBuilder();

                //This shows what's coming in
                for(int i = 0; i < buffer.length; i++){
                    if(buffer[i] != 0){
                    byStr.append(buffer[i]);
                    l(byStr);}
                }
                //this shows the complete string
                l(byStr);

                if(mStop){
                    mStopped = true;
                    return;
                }
                l("sent "+counter);
                counter++;
                counter = (byte) (counter % 16);
            }

I really don't know why it is not working. I have lots of skill in Android but I just can't get my head around this.

enIN and enOUT are end points:

epIN = usbIf.getEndpoint(i);

epOUT = usbIf.getEndpoint(i);

Also conn is the USB device connection.

            UsbDevice dev = sDevice;
            if (dev == null)
                return; 
            UsbManager usbm = (UsbManager) getSystemService(USB_SERVICE);
            UsbDeviceConnection conn = usbm.openDevice(dev);
            l("Interface Count: "+dev.getInterfaceCount());
            l("Using "+String.format("%04X:%04X", sDevice.getVendorId(), sDevice.getProductId()));

            if(!conn.claimInterface(dev.getInterface(0), true)) 
                return;

Upvotes: 0

Views: 3467

Answers (1)

Alexander
Alexander

Reputation: 1098

epIN = usbIf.getEndpoint(i);
epOUT = usbIf.getEndpoint(i);

If here i (the endpoint index) has the same value for both calls you will end up with the same endpoint. The endpoint however has a direction and can't be used to send and receive data. As the bulkTransfer() method uses the endpoint address to determine the direction you end up in two reads or two writes respectively.

Note that the endpoint index in an interface does not match the endpoint address. Interfaces are logical mappings for endpoints (which have hardware addresses). I guess you may want to read from IN endpoint 1 (address: 0x81) and write to OUT endpoint 1 (address: 0x01). Therefor you are using the same value for i However IN 1 and OUT 1 are not the same endpoint.

Examine the device descriptor to find out which endpoint you want to communicate with. The first mentioned endpoint after the interface descriptor is accessible via UsbInterface.getEndpoint(0) regardless of its physical address. Check the direction of that endpoint and use it appropriately. Find the other endpoint using UsbInterface.getEndpoint(1) (assuming they are the two first endpoints in one interface).

btw: Your byte buffers are too big. The maximum size of bulk transfer packets is 64 bytes for full speed and 512 bytes for high speed devices.

Upvotes: 1

Related Questions