【问题标题】:Controlling Android app with bluetooth mouse/ presenter使用蓝牙鼠标/演示器控制 Android 应用程序
【发布时间】:2012-07-23 06:41:06
【问题描述】:

这不是一个容易搜索的问题,因为大多数回复都涉及使用手机作为指针,但我想做的是使用鼠标/演示器/指针来控制 Android 平板电脑。我买了这个 Targus Bluetoogh Presenter (Amazon),我想在我的 7 英寸平板电脑上连接一个应用程序。现在我花了两个小时搜索 Google 和 stackoverflow 都没有成功,我想我应该寻求帮助。

蓝牙演示器工作正常。它就像鼠标一样,我可以滚动并单击并运行我的应用程序。但这是一个应用程序,平板电脑将在光天化日之下安装在移动的船上,使用鼠标指针的这种精细增益调整是行不通的。我敢肯定,即使我可以控制指针,我也可能看不到它。这是一个高生存能力的应用程序,黑色背景上有 1 英寸高的白色字母。你只是看不到一个微小的鼠标指针。

我需要的是让演示者上的两个可编程按钮将焦点推进到应用程序上的几个按钮上,然后让另一个按钮按下它们。现在,演示者上的一个可编程按钮突出显示了我的应用程序上的一个按钮,但使用右键单击只会触发鼠标指针下方的任何按钮。我想我需要两个可编程按钮来前进和输入,但这只是一个猜测。我愿意接受任何可行的解决方案。

我什至不知道从哪里开始。我应该在我的应用程序中编程吗?我需要一个界面应用程序来对按钮进行编程吗? Play 商店中有什么东西可以满足我的需求吗?当我在蓝牙鼠标上搜索时,我看到的只是使用手机控制计算机的应用程序。不是我想要的方向。我需要一些帮助或指导。

【问题讨论】:

  • 您是 Android 开发者吗?或者您是否至少设置了一个 Android 开发环境?了解您的技能水平将有助于我们确定可以给您的建议。
  • @StephanBranczyk 我和他聊了一会儿。 fwiw 他确实有一个开发环境,并且他已经制作了一个 android 应用程序。但我相信这是他的第一次。不过,他的基础似乎还不错。
  • @Tim 帮了大忙。它基本上是与一些化妆品一起工作的。这是我的第一个应用程序,但它是一个像 Tim 所说的那样工作的应用程序。我正在尝试远程控制应用程序,有点像下一步。我给蒂姆高分是因为他给了我帮助。

标签: android bluetooth


【解决方案1】:

好的,我想我已经为您找到了可行的解决方案。原来我做错了一些事情。对于一个dispatchKeyEvent()onKeyDown() 更好,因为它阻止按下音量按钮进入系统(在我的设备上,它们会发出哔哔声,但音量没有变化)。而且事实证明,您需要使用Instrumentation 类来发送欺骗键事件,而不是手动调用dispatchKeyEvent()。 Instrumentation 类也不会从主线程进行方法调用,因此您必须将调用包装在它们自己的线程中。

我还了解了为什么您设备上的某些按钮会发送两个关键事件 117 和 71。这些与 shift+[ 匹配,出于我们的目的,我们可以忽略按下 shift 并仅使用 [ 为我们采取行动。

这是一个似乎对我有用的被覆盖的 dispatchKeyEvent() 方法。

@Override
public boolean dispatchKeyEvent(KeyEvent ke){
int keyCode = ke.getKeyCode();
if(ke.getAction() == KeyEvent.ACTION_DOWN){
    if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) 
    { 
        /************************************** 
         * What ever code snippet you put 
         * here will run whenever you press the
         * volume down button on your presenter
         * device
         **************************************/
        return true;
    }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) 
    { 
        /************************************** 
         * What ever code snippet you put 
         * here will run whenever you press the
         * volume up button on your presenter
         * device
         **************************************/
        return true;
    }else if (keyCode == 30){ 
        /************************************** 
         * What ever code snippet you put 
         * here will run whenever you press the
         * left programmable button on your 
         * presenter device
         **************************************/
         return true;
        }
    else if (keyCode == 59){
        /************************************** 
         * This was an attempt to get it to ignore
         * the shift keypress coming from the
         * left/right arrow keys on the devices
         * ignoring that would in theory make
         * those keys function as up/down focus
         * movers. Didn't seem to work though.
         * you could probably remove this branch
         * of the if statement if you want.
         * However since those buttons do send 
         * key events to the device it should 
         * should still be possible override these
         * buttons somehow.
         **************************************/
        return true;
    }
}else if(ke.getAction() == KeyEvent.ACTION_UP){
    /************************************** 
     * This section will catche the "release"
     * events from all of the keys we are using
     * and tell the system that we've handled
     * them. So that the system will not pass
     * the events along to anything else.
     **************************************/
    if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) 
    { 
        /************************************** 
         * If you had any reason / desire to
         * you could put a code snipet here and it
         * would be run when you let go after 
         * pressing the volume down button on your
         * presenter device.
         **************************************/
        return true;
    }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) 
    { 
        return true;
    }else if (keyCode == 59){
        return true;
    }else if (keyCode == 30) {
        return true;
    }
}

/************************************** 
 * The following line is needed so that
 * the system will treat any key events 
 * that we aren't interested in normally.
 * i.e. the back button on the tablet, by
 * by calling super.dispatchKeyEvent(), we
 * ensure that the back button still behaves
 * like normal.
 **************************************/
return super.dispatchKeyEvent(ke);
}

这将允许您控制演示者设备上的 3 个按钮(Vol Up、Vol Down 和左侧可编程按钮),方法是将您的代码 sn-ps 放在 if 语句的适当分支中,您可以进行任何操作这 3 个按钮中的任何一个都可以随心所欲。

我已经创建了一个测试项目来实现这一点,如果您想查看整个内容,可以download the zipped project folder here。在这个测试项目中,我进行了设置,以便 vol up/down 将用作 d-pad up/down,这将允许您将焦点移动到活动中的不同视图。

这是您可以放在 if 语句的一个分支中以欺骗 d-pad 箭头按钮的代码:

new Thread(new Runnable() {         
    @Override
    public void run() {                 
        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    }   
}).start();

您可以将 KeyEvent.KEYCODE_DPAD_DOWN替换为您想要的任何其他关键事件,例如 KeyEvent.KEYCODE_DPAD_UPKeyEvent.KEYCODE_DPAD_CENTER @ KeyEvent.KEYCODE_DPAD_CENTER后者将作为选择按钮,将单击事件发送到当前具有焦点的按钮.

【讨论】:

  • 这个帮助很大,如果没有 Tim 的帮助,我是做不到的。我想要做的是拿一个平板电脑并将它安装在一个防水盒中,每个人都可以看到它并用遥控器控制它回到驾驶舱。这让我可以做到这一点。很棒的代码和比我应得的更多帮助。一百万谢谢。我唯一的改变是使用 TAB 在键之间移动而不是上下移动,因为某些键还需要左右才能获得焦点。 TAB 只是在它们之间循环。
  • 蒂姆,我知道你知道我最新问题的答案。需要 android 和 Presenter 的键码表。 stackoverflow.com/questions/11768356/…
猜你喜欢
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多