如何在Android中通过蓝牙访问键盘?

时间:2011-11-29 12:02:47

标签: android keyboard bluetooth

我想通过蓝牙访问键盘。

例如: 如果我在第一个设备中打开键盘,两个设备通过蓝牙相互连接,我想在任何编辑文本中访问另一个设备中的键盘。

那么如何在android中通过蓝牙访问其他设备中的键盘?

2 个答案:

答案 0 :(得分:0)

要检测弹出的EditText键盘:

Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
    } else {
        // Keyboard is hidden
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

捕获关键事件

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if (event.getAction() == KeyEvent.ACTION_DOWN) 

         // keycode should be sent over bluetooth. 

          return true;
        }
        return false;
    }
});

要在Activity中注入按键,只需使用相应的KeyEvent

调用onKeyDown()即可

打开键盘

EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

并关闭它:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

如果您对在另一部手机上弹出键盘不感兴趣,请忽略此部分。使用旧的:setText()


现在,您如何形成蓝牙发送这些关键事件的聊天协议取决于您。提示:RemoteDroid used OscMessages

答案 1 :(得分:0)

如果你想要的是在电话A上有人按一个键,并将键输入到电话B上当前突出显示的字段,那么这就是你需要的:

两部手机上的应用程序(一个在“发送模式”,一个在“接收模式”)。

您需要按@Reno的答案中的代码捕获按键。然后,您需要使用传输机制(在本例中为蓝牙)将其传输到其他设备。您应该能够通过蓝牙搜索有关传输字符串的足够教程。

您需要手机B上的应用程序接收字符串/字符,然后将其输出到当前选定的字段。这意味着找到焦点字段(再次,在SO上回答),然后写入该字段(可能是setText("A");)。

相关问题