2017-09-24 132 views
0

我的項目是從我的汽車的方向盤控件讀取按鈕輸入,並將它們(使用Teensy 3.2 Arduino-like)轉換爲我選擇的android系統動作(例如音量Up,Next Track,OK Google)。Teensy Arduino作爲Android HID設備,在幾次輸入後停止

我嘗試了Teensy提供的幾種不同的模式來解決這個問題,最初是作爲鍵盤仿真,然後是遊戲杆模擬,現在最後是一個原始HID設備。所有這些模式在Windows和Linux上完美地工作,但不能在Android上工作(我試過在運行Android 4.1和Android 5設備的目標設備上,都沒有工作)。

最近我設法使這項工作是作爲一個RawHID設備與我寫的一個小應用程序解碼數據包並轉換爲系統操作。 這實際上工作 ...約2-5按鈕按下。然後沒有。爲了得到我的下一個2-5按鈕我不得不拔下設備並重新啓動程序。該程序暫停thisConnection。 requestWait()永遠。在舊版本中,我使用bulkTransfer,它具有類似的效果,在2-5次按鈕按下後返回-1並且永久不會有數據。

代碼OpenConnection的:

public boolean OpenConnection(UsbManager pUsbManager) 
    { 

     if(ActiveDevice == null) return false; 
     if(!hasPermission) return false; 
     if(ActiveConnection != null && ActiveEndpoint != null) return true; 

     if(hasAssociatedUsbDevice()) { 

      ActiveInterface = ActiveDevice.getInterface(InterfaceIndex); 
      ActiveEndpoint = ActiveInterface.getEndpoint(EndpointIndex); 
      ActiveConnection = pUsbManager.openDevice(ActiveDevice); 
      ActiveConnection.claimInterface(ActiveInterface, true); 
      ActiveRequest = new UsbRequest(); 
      ActiveRequest.initialize(ActiveConnection,ActiveEndpoint); 

      return true; 
     } 

     return false; 
    } 

代碼設備循環(一個單獨的低優先級的線程中運行)

private void deviceLoop(Config.HIDDevice pHIDDevice) 
{ 

    try 
    { 
     if (!pHIDDevice.OpenConnection(mUsbManager)) return; 


     ByteBuffer dataBufferIn = ByteBuffer.allocate(64); 

     //String activeAppName = mAppDetector.getForegroundAppName(); //TODO: Refactor, causing excessive memory alloc 


     String activeAppName = null; 

     Config.AppProfile activeProfile = pHIDDevice.getAppProfile(activeAppName); 

     while (!mExitDeviceThreads) 
     { 
      UsbDeviceConnection thisConnection = pHIDDevice.getActiveConnection(); 
      if (thisConnection == null) break; //connection dropped 

      UsbRequest thisRequest = pHIDDevice.getActiveRequest(); 
      if (thisRequest == null) break; //connection dropped 

      thisRequest.queue(dataBufferIn, dataBufferIn.capacity()); 

      if (thisConnection.requestWait() == thisRequest) 
      { 
       byte[] dataIn = dataBufferIn.array(); 

       for (Config.ButtonPacketMapping thisButtonMapping : pHIDDevice.getButtonPacketMappings()) 
       { 
        if (thisButtonMapping.Update(dataIn)) 
        { 
         for (Config.ButtonAction thisButtonAction : activeProfile.getButtonActions(thisButtonMapping.getName())) 
         { 
          if (thisButtonMapping.getLastValue() == false && thisButtonMapping.getValue() == true) 
          { 
           if (thisButtonAction.buttonAction == Config.ButtonAction.eButtonActionType.Press) 
           { 
            thisButtonAction.Set(); 
           } 
          } 
          else if (thisButtonMapping.getLastValue() == true && thisButtonMapping.getValue() == false) 
          { 
           if (thisButtonAction.buttonAction == Config.ButtonAction.eButtonActionType.Release) 
           { 
            thisButtonAction.Set(); 
           } 
          } 
         } 
        } 
       } 
      } 
      else 
      { 
       break; //Connection dropped or something went very wrong 
      } 
     } 
    } 
    finally 
    { 
     pHIDDevice.CloseConnection(); 
    } 
} 

所以我的問題更簡潔是:

有沒有人設法讓Teensy Arduino以任何方式通過USB與Android進行交互?我的HID方法導致這種「拖延」問題有什麼問題嗎?

回答