2017-03-08 7 views
0

Android携帯に接続する複合USBガジェットがあります。これは、次の、シリアルMTP、およびマスストレージインタフェースが含まれています。Android MTPクライアントは、1つのインターフェイスではなく、デバイス全体を開きます

interface :: id : 0, name : CDC Abstract Control Model (ACM), alt 0 [0002h:0002h:0001h] CDC Control 
interface :: id : 1, name : CDC ACM Data, alt 0 [000ah:0000h:0000h] CDC Data 
interface :: id : 2, name : MTP, alt 0 [00ffh:00ffh:0000h] Vendor Specific 
interface :: id : 3, name : Mass Storage, alt 0 [0008h:0006h:0050h] Mass Storage 

私の問題は、シリアルおよびMTPの両方のインターフェイスを開こうとしています。ここに私のコードは次のとおりです。

private class SetupInterfacesRunnable implements Runnable 
{ 
    @Override 
    public void run() 
    { 
     synchronized(MyService.this) 
     { 
      usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
      usbConnection = usbManager.openDevice(usbDevice); 

      /* 
      interface :: id : 0, name : CDC Abstract Control Model (ACM), alt 0 [0002h:0002h:0001h] CDC Control 
      interface :: id : 1, name : CDC ACM Data, alt 0 [000ah:0000h:0000h] CDC Data 
      interface :: id : 2, name : MTP, alt 0 [00ffh:00ffh:0000h] Vendor Specific 
      interface :: id : 3, name : Mass Storage, alt 0 [0008h:0006h:0050h] Mass Storage 
      */ 

      // Interface 1 on the composite usb device is cdc acm data. 
      serialPort = UsbSerialDevice.createUsbSerialDevice(usbDevice, usbConnection, 1); 
      if(serialPort != null) 
      { 
       if(serialPort.open()) 
       { 
        serialPort.setBaudRate(115200); 
        serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); 
        serialPort.setParity(UsbSerialInterface.PARITY_NONE); 
        serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); 
        serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); 

        mUIHandler.post(notifyRadgetConnected); 

        // set the callback to catch serial data 
        serialPort.read(mCallback); 

        mUIHandler.post(handshake); 

       }else 
       { 
        // Serial port could not be opened, maybe an I/O error or it CDC driver was chosen it does not really fit 
        LoggerV2.e("Failed to open device serial port"); 
       } 
      }else 
      { 
       // No driver for given device, even generic CDC driver could not be loaded 
       LoggerV2.e("Failed to find driver for the serial device"); 
      } 

      // Interface 2 on the composite usb device is MTP. 
      MtpDevice mtpDevice = new MtpDevice(usbDevice); 
      if (!mtpDevice.open(usbConnection)) { 
       LoggerV2.e("Failed to obtain device mtp storage"); 
      } 

     } 
    } 
} 

私は(felHR85/UsbSerial)を使用していたシリアルの実装は、しかし、私はこのようにMTPDeviceクラスを実装する簡単な方法を見ることができない、単一のインターフェースを開くことができます。

Android MTP APIは、open関数が呼び出されたときに、デバイス/接続全体が開きたいと思うようです。

native_open(mDevice.getDeviceName(), connection.getFileDescriptor()); 

APIドキュメント:https://developer.android.com/reference/android/mtp/MtpDevice.html

ソースコード:https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/mtp/MtpDevice.java

私が見ることができないだけで、単一のインターフェイスを開く方法。接続を使用して同じデバイスに複数のインターフェイスを開くという簡単な方法がありませんか?

答えて

0

簡単な答え/回避策は、インターフェイス0でMTPレスポンダを実行してから、MTPデバイスの後にシリアルポートを開くことです。長い答えは

... コードを介してビットを掘り後、私が見つかりました。どこフォローソースファイルを通じてnative_openコールフィルタ:

  • MtpDevice.java
    • android_mtp_MtpDevice.cpp
      • MtpDevice.cpp

MtpDevice.cpp:MtpDevice.cppでhttps://android.googlesource.com/platform/frameworks/av/+/master/media/mtp/MtpDevice.cpp

、私がそれを見つけることができなかったかのようにログに印刷されたエラーメッセージ「\ nは見つからないエンドポイント」で失敗しているようです正しい終点。今、私は最初のMTPとのインターフェースを並べ替えることにより、この問題を回避働くことになったために

interface :: id : 0, name : MTP, alt 0 [00ffh:00ffh:0000h] Vendor Specific 
interface :: id : 1, name : CDC Abstract Control Model (ACM), alt 0 [0002h:0002h:0001h] CDC Control 
interface :: id : 2, name : CDC ACM Data, alt 0 [000ah:0000h:0000h] CDC Data 
interface :: id : 3, name : Mass Storage, alt 0 [0008h:0006h:0050h] Mass Storage 

これは、インターフェイスでシリアルを開く、私は最初のMTPデバイスを開くことができます:

private class SetupInterfacesRunnable implements Runnable 
{ 
    @Override 
    public void run() 
    { 
     synchronized(RadgetService.this) 
     { 
      usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
      usbConnection = usbManager.openDevice(usbDevice); 

      /* 
      interface :: id : 0, name : MTP, alt 0 [00ffh:00ffh:0000h] Vendor Specific 
      interface :: id : 1, name : CDC Abstract Control Model (ACM), alt 0 [0002h:0002h:0001h] CDC Control 
      interface :: id : 2, name : CDC ACM Data, alt 0 [000ah:0000h:0000h] CDC Data 
      interface :: id : 3, name : Mass Storage, alt 0 [0008h:0006h:0050h] Mass Storage 
      */ 

      // Interface 0 on the composite device is MTP 
      MtpDevice mtpDevice = new MtpDevice(usbDevice); 
      if (!mtpDevice.open(usbConnection)) { 
       LoggerV2.e("Failed to obtain radget mtp storage"); 
      } 
      else 
      { 
       LoggerV2.i("Opened MTP storage: %s", mtpDevice.getDeviceName()); 

       int[] storageIds = mtpDevice.getStorageIds(); 
       if (storageIds == null) { 
        LoggerV2.i("No mtp storage id's found"); 
        return; 
       } 

       /* 
       * scan each storage 
       */ 
       for (int storageId : storageIds) { 
        LoggerV2.i("~~~~Storage id: %d~~~~", storageId); 
        scanObjectsInStorage(mtpDevice, storageId, 0, 0); 
       } 
      } 

      // Interface 2 on the composite usb device is cdc acm data. 
      serialPort = UsbSerialDevice.createUsbSerialDevice(usbDevice, usbConnection, 2); 
      if(serialPort != null) 
      { 
       if(serialPort.open()) 
       { 
        serialPort.setBaudRate(115200); 
        serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); 
        serialPort.setParity(UsbSerialInterface.PARITY_NONE); 
        serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); 
        serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); 

        mUIHandler.post(notifyRadgetConnected); 

        // set the callback to catch serial data 
        serialPort.read(mCallback); 

        mUIHandler.post(handshake); 

       }else 
       { 
        // Serial port could not be opened, maybe an I/O error or it CDC driver was chosen it does not really fit 
        LoggerV2.e("Failed to open device serial port"); 
       } 
      }else 
      { 
       // No driver for given device, even generic CDC driver could not be loaded 
       LoggerV2.e("Failed to find driver for serial device"); 
      } 

     } 
    } 
} 
関連する問題