Teensy Arduino作为Android HID设备,在几次输入后停止

Teensy Arduino作为Android HID设备,在几次输入后停止

问题描述:

我的项目是从我的汽车的方向盘控件读取按钮输入,并将它们(使用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方法导致这种“拖延”问题有什么问题吗?

最后,我切换到Arduino Pro Micro,它具有本地USB支持,并使用“消费者设备”方法使用Project-HID library。这适用于我尝试过的任何操作系统/硬件组合。