如何跟一个蓝牙键盘?蓝牙、键盘

2023-09-05 09:43:14 作者:3.执笔梦一场

我已经写了一个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.

一两件事情可以发生。

我的第一个担心的是,还有就是HID协议进行口语键盘,也不会泄露自己的秘密,直到我说出正确的咒语。那是对的吗?或者应该被照顾的自动BT插座栈?套接字流似乎是一个标准的串行流,我不知道这是正确的。

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的,我的问题可能仅仅是该操作系统的特定部分已经被三星删除(但我还会得到插座连接的有效输入流? )。它被广泛报道,美国版本的标签根本无法连接使用标准的Andr​​oid BT公用事业任何BT HID,尽管BT文件传输都正常工作。

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的多少缓冲确实的BT数据从HID进来的,但如果是由套接字连接,该数据应出现在输入流,不是吗?

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连接。安卓至今只提供了使用RFCOMM连接的能力。您需要使用原生开发套件,写你的键盘code在C中使用的bluez来实现自己的目标。有一个在使用任天堂的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.