我的项目是从我的汽车的方向盘控制器读取按钮输入,并将它们(使用类似于Teensy 3.2 Arduino)转换为我选择的android系统操作(例如音量上升,下一个轨道,OK Google)。
为了解决这个问题,我尝试了Teensy提供的几种不同的模式,最初是作为键盘模拟,然后是keyboard仿真,现在是Raw HID设备。所有这些模式在Windows和Linux上都能完美地工作,但在android上却不起作用(我已经在运行Android4.1和Android 5设备的目标设备上试过了,两者都不起作用)。
我最接近这个功能的是一个RawHID设备,我编写了一个小应用程序来解码数据包并将其转换为系统操作。这实际上是works...for大约2-5按钮按下.那就什么都没有了。为了让我的下一个2-5按钮按下,我必须拔掉设备并重新启动程序。程序在thisConnection.requestWait上停止(https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html#requestWait(%29)永远停止)。在较早的版本中,我使用了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();
}
}
所以我的问题更简洁一些:
有没有人在USB上用任何方式与Android进行接口?我的HID方法有什么问题导致这个“拖延”问题吗?
发布于 2017-10-05 12:28:28
最后,我切换到了一个具有本机USB支持的Arduino Pro微,并使用了“消费设备”方法的工程-HID图书馆。这与我尝试过的任何OS/硬件组合都能完美地工作。
https://stackoverflow.com/questions/46391380
复制相似问题