2017-03-08 101 views
0

我有一個複合USB小工具,我想連接到Android手機。它包含以下串行,MTP和海量存儲接口:Android MTP客戶端打開整個設備而不是單個接口

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:https://android.googlesource.com/platform/frameworks/av/+/master/media/mtp/MtpDevice.cpp

在MtpDevice.cpp,似乎我會失敗,「端點未找到\ n」打印到日誌中的錯誤信息,就好像它找不到正確的端點。

現在我結束了先用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"); 
      } 

     } 
    } 
} 

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裝置,然後通過界面打開串口

相關問題