1

USBケーブルを介してAndroidアプリと外部デバイスとの間でオーディオデータ通信を実装したいです。

Android USBドキュメントとサンプルコードを見てきました。Androidアプリケーションで外部デバイスを正常に検出して接続できます。

外部デバイスとAndroidアプリの間でデータを転送(送受信)する方法は?

編集:

私がこれまでに行ったことを説明しましょう。

以下のコードでデバイスとそのインターフェースを見つけました。

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

次に、以下のコードでインターフェイスのデバイス接続とクレームを開きます。

// 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によると、USB_DIR_IN の場合は 128、USB_DIR_OUT の場合は 0 です。だから、私はこれらの2つのインターフェースを取りました。以下のコードでイン&アウトエンドポイントを見つけました。

 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

インターフェイスとエンドポイントの詳細:

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]
4

1 に答える 1

2

転送用http://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html#bulkTransfer(android.hardware.usb.UsbEndpoint , byte[], int, int, int)

于 2014-02-12T13:13:42.627 に答える