Android:外部设备和USB之间的USB通信; Android应用程序

时间:2014-02-03 10:50:59

标签: android usb usbserial usb-otg usb-hostcontroller

我想实现android应用程序之间的音频数据通信。外部设备通过USB线。

我见过android USB document&这是示例代码。 我能够检测到&在android应用程序中成功连接外部设备。

如何在外部设备和外部设备之间传输(发送/接收)数据。 android app?

修改

让我解释一下到目前为止我所做的事情。

我找到了设备&它是以下代码的界面。

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;
}

然后打开设备连接&amp;通过以下代码声明接口。

// 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将成功归还给我。

根据android developer doc,128为USB_DIR_IN&amp; USB_DIR_OUT为0。所以,我已经采取了这两个接口。     我找到了&amp;通过以下代码输出端点。

 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;
            }
        }
    }           
}

外部设备详细信息:

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&amp;这是终点详情:

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]
相关问题