如何与蓝牙键盘通话? [英] How to talk to a Bluetooth keyboard?

查看:15
本文介绍了如何与蓝牙键盘通话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个连接到蓝牙键盘的 Android 应用程序.它通过 BT 套接字连接到键盘并获取套接字的输入流.

I've written an Android app that connects to a Bluetooth keyboard. It connects through a BT socket to the keyboard and acquires the socket's input stream.

InputStream inStrm = socket.getInputStream();

接下来我尝试读取输入流,但它说没有可用字节.

Next I tried to read the input stream, but it says there are no bytes available.

int nBytesAvail = inStrm.available(); // always gives me 0

int  dataByte = inStrm.read(); // always generates IOException

异常说:软件导致连接中止

The exception says: Software caused connection to abort

如果我尝试写入流,则会出现另一个异常:传输端点未连接.

If I try to write to the stream, I get another exception: Transport endpoint is not connected.

可能发生两种情况之一.

One of two things can be happening.

  1. 我首先担心的是,键盘上有 HID 协议,在我说出正确的咒语之前,它不会泄露其秘密.那是对的吗?还是应该由 BT 套接字堆栈自动处理?套接字流似乎是标准的串行流,我不确定是否正确.

  1. My first fear is that there is the HID protocol to be spoken to the keyboard, and it will not divulge its secrets until I utter the proper incantation. Is that correct? Or should that be taken care of by the BT socket stack automatically? The socket stream seems to be a standard serial stream, and I'm not sure that's correct.

我的第二个担心是,由于这是一个 Galaxy Tab,我的问题可能只是操作系统的特定部分已被三星删除(但我仍能从套接字连接中获得有效的输入流吗?).据广泛报道,尽管 BT 文件传输工作正常,但美国版本的 Tab 根本不会使用标准的 Android BT 实用程序连接到任何 BT HID.

My second fear is that since this is a Galaxy Tab, my problem might simply be that that particular part of the OS has been removed by Samsung (but would I still get a valid input stream from the socket connection?). It is widely reported that the US versions of the Tab simply will not connect to any BT HID using the standard Android BT utilities, although BT file transfers do work fine.

我想第三种可能性是我只是错过了按键.我不知道 Java 对来自 HID 的 BT 数据做了多少缓冲,但是如果建立了套接字连接,数据应该出现在输入流中,不是吗?

I suppose a third possibility is that I'm simply missing the keystrokes as they happen. I don't know how much buffering Java does of BT data coming in from a HID, but if the socket connection is made, the data should appear in the input stream, no?

我不愿意在这方面投入更多时间,以防万一我以完全错误的方式进行(参见#1),或者它注定要失败(参见#2).

I'm reluctant to put in much more time into this in case I'm going about it completely the wrong way (see #1), or it is doomed to fail (see #2).

推荐答案

所有普通的蓝牙键盘都实现了 HID 配置文件,这需要 L2CAP 连接.到目前为止,Android 仅提供使用 RFCOMM 连接的能力.您需要使用本机开发工具包并用 C 编写键盘代码才能使用 bluez 来实现您的目标.查看使用 Nintendo WiiMote 的应用程序.WiiMote 还实现了 HID 配置文件.

All normal Bluetooth keyboards implement the HID profile, which requires an L2CAP connection. Android so far only provides the ability to use RFCOMM connections. You would need to use the Native Development Kit and write your keyboard code in C to using bluez to achieve your goal. Have a look at apps that use the Nintendo WiiMote. The WiiMote also implements the HID profile.

这篇关于如何与蓝牙键盘通话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆