1
votes

I want to implement audio data communication between android application & external device through USB cable.

I have seen android USB document & it's sample code. I am able to detect & connect external device in android application successfully.

How to transfer(send/receive) data between external device & android app ?

EDIT:

Let me explain what I have done so far.

I have found device & it's interface by below code.

UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);

// check for existing devices
for (UsbDevice device :  mManager.getDeviceList().values()) {
    ArrayList<UsbInterface> intf = findInterface(device);   
}

// searches for an interface on the given USB device
static private  ArrayList<UsbInterface> findInterface(UsbDevice device) {
    ArrayList<UsbInterface> usbIntf = new ArrayList<UsbInterface>();

    int count = device.getInterfaceCount();
    for (int i = 0; i < count; i++) {
        UsbInterface intf = device.getInterface(i);

        if( intf.getEndpointCount() > 0 ) {
            for( int j = 0; j < intf.getEndpointCount(); j++ ) {                
                if( intf.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_ISOC ) {
                    usbIntf.add(intf);
                }
            }               
        }    
    }
    return usbIntf;
}

Then open device connection & claims for Interface by below code.

// open device connection
UsbDeviceConnection connection = mManager.openDevice(device);

boolean isSuccess = false;
if (connection != null) {       
    for (int i = 0; i < usbIntf.size(); i++) {
        UsbInterface intf = usbIntf.get(i); 
        isSuccess = connection.claimInterface(intf, false);
    }          
}

claimInterface returns success to me.

According to android developer doc, 128 for USB_DIR_IN & 0 for USB_DIR_OUT. So, i have take those two interfaces. I have found in & out endpoints by below code.

 for( int i = 0; i < usbIntf.size(); i++ ) {
    UsbInterface intf = usbIntf.get(i); 

    for (int j = 0; j < intf.getEndpointCount(); j++) {
        UsbEndpoint ep = intf.getEndpoint(j);
        if( ep.getType() == UsbConstants.USB_ENDPOINT_XFER_ISOC ) {
            if( ep.getDirection() == UsbConstants.USB_DIR_OUT ) {
                epOut = ep;
            } 
            else if ( ep.getDirection() == UsbConstants.USB_DIR_IN ) {
                epIn = ep;
            }
        }
    }           
}

External Device details:

Device Class : 0, Subclass : 0, Protocol : 0, 
Device ID : 2002, Device Name : /dev/bus/usb/002/002, 
Interface Count : 6, Product Id : 316, Vendor ID : 3468

Intarface & it's End Point details:

1. Interface Class : 1, Subclass : 1, Protocol : 0, EndpointCount : 0, ID : 0

2. Interface Class : 1, Subclass : 2, Protocol : 0, EndpointCount : 0, ID : 1

3. Interface Class : 1, Subclass : 2, Protocol : 0, EndpointCount : 1, ID : 1
Endpoint : 0 : Type : 1, Direction : 0, 
Details : UsbEndpoint[mAddress=1,mAttributes=9,mMaxPacketSize=200,mInterval=1]

4. Interface Class : 1, Subclass : 2, Protocol : 0, EndpointCount : 0, ID : 2

5. Interface Class : 1, Subclass : 2, Protocol : 0, EndpointCount : 1, ID : 2
Endpoint : 0 : Type : 1, Direction : 128, 
Details : UsbEndpoint[mAddress=130,mAttributes=9,mMaxPacketSize=100,mInterval=1]

6. Interface Class : 3, Subclass : 0, Protocol : 0, EndpointCount : 1, ID : 3
Endpoint : 0 : Type : 3, Direction : 128, 
Details : UsbEndpoint[mAddress=135,mAttributes=3,mMaxPacketSize=4,mInterval=2]
1
Also see Using Andriod USB Host API to read my USB game controller/Or other USB device data, and the comment on the offsite article. The offsite article shows you how to read and write through the end points.jww

1 Answers