首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Teensy Arduino作为Android隐藏设备,在几个输入后停止。

Teensy Arduino作为Android隐藏设备,在几个输入后停止。
EN

Stack Overflow用户
提问于 2017-09-24 14:56:07
回答 1查看 549关注 0票数 0

我的项目是从我的汽车的方向盘控制器读取按钮输入,并将它们(使用类似于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代码:

代码语言:javascript
运行
复制
    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;
    }

设备循环的代码(在单独的低优先级线程上运行)

代码语言:javascript
运行
复制
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方法有什么问题导致这个“拖延”问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-05 12:28:28

最后,我切换到了一个具有本机USB支持的Arduino Pro微,并使用了“消费设备”方法的工程-HID图书馆。这与我尝试过的任何OS/硬件组合都能完美地工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46391380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档