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

我的项目是从我的汽车的方向盘控件中读取按钮输入并将它们(使用Teensy 3.2 Arduino相似)转换为我选择的Android系统动作(例如,音量调高,下一曲,OK谷歌) 。

为了解决这个问题,我尝试了几种不同的模式,最初是作为键盘模拟,然后是操纵杆模拟,现在最终是原始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以任何方式与Android接口?我的HID方法是否有任何问题导致这种"停滞"问题吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值