【问题标题】:How to access a keyboard via Bluetooth in Android?如何在 Android 中通过蓝牙访问键盘?
【发布时间】:2011-11-29 21:45:08
【问题描述】:

我想通过蓝牙访问键盘。

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

那么我怎样才能通过 android 中的蓝牙访问另一台设备中的键盘呢?

【问题讨论】:

    标签: android keyboard bluetooth


    【解决方案1】:

    检测弹出的 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

    【讨论】:

    • @Reno ..但我没有使用 edittext ......我想将第一个设备键盘访问到另一个设备的任何编辑器(Edittext)......所以我怎样才能在 edittext 中获取这些键示例:如果我想在另一台设备上输入用户名和密码,那么我可以访问第一个设备键盘?
    • 你在说什么......你没有使用EditText,但你想使用EditText吗?我不明白你的意思。
    【解决方案2】:

    如果您想要让电话 A 上的某个人按下一个键并将该键输入到电话 B 上当前突出显示的字段中,那么这就是您所需要的:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多