How to talk with a bluetooth keyboard?

I wrote an Android app that connects to a Bluetooth keyboard. It connects via the BT connector to the keyboard and receives a socket input stream.

InputStream inStrm = socket.getInputStream(); 

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

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

or

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

Exception: Software caused connection to abort

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

One of two things can happen.

  • My first fear is that there is a HID protocol that you need to talk to the keyboard and it will not reveal its secrets until I cast the proper spell. It's right? Or should it be given to the BT socket socket automatically? The socket stream seems to be a standard serial stream, and I'm not sure if this is correct.

  • My second fear is that since this is a Galaxy Tab, my problem may be that this part of the OS was removed by Samsung (but can I get the correct input stream from the socket connection?). It is widely believed that American versions of Tab simply wonโ€™t connect to any BT HID using standard Android BT utilities, although BT file transfer works fine.

I believe the third possibility is that I just skip keystrokes when they happen. I don't know how much Java buffering does of BT data coming from the HID, but if the socket connection is running, the data should appear in the input stream, not?

I do not want to invest much more time in this if I am completely mistaken (see No. 1), or if he is doomed to failure (see No. 2).

+6
android bluetooth keyboard hid
source share
3 answers

All conventional Bluetooth keyboards implement an HID profile that requires an L2CAP connection. Android so far provides only the ability to use RFCOMM connections. You will need to use the Native Development Kit and write your keyboard code in C to use bluez to achieve your goal. Check out apps that use the Nintendo WiiMote. WiiMote also implements an HID profile.

+4
source share

Mringwal's answer is correct, besides the NDK approach, you can use reflection on some devices to achieve L2CAP connectivity:

 public static BluetoothSocket createL2CAPBluetoothSocket(String address, int psm){ return createBluetoothSocket(TYPE_L2CAP, -1, false,false, address, psm); } // method for creating a bluetooth client socket private static BluetoothSocket createBluetoothSocket( int type, int fd, boolean auth, boolean encrypt, String address, int port){ try { Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor( int.class, int.class,boolean.class,boolean.class,String.class, int.class); constructor.setAccessible(true); BluetoothSocket clientSocket = (BluetoothSocket) constructor.newInstance(type,fd,auth,encrypt,address,port); return clientSocket; }catch (Exception e) { return null; } } 

where TYPE_L2CAP is an integer having a constant value of 3.

Please note that this approach will only work on some Android devices.

Writing a HID application is not an easy task. You need to implement a report descriptor parser, a component used to โ€œdiscoverโ€ the capabilities (special keys, functions) of a remote HID device. You will also need to study the HID protocol and workflow, a copy is available here: http://www.dawidurbanski.pl/public/download/projekty/bluepad/HID_SPEC_V10.pdf

There are already professional programs that do just that, supporting HID on Android, for example, this software: http://teksoftco.com/index.php?section=product&pid=24

Due to stack limitations, the L2CAP protocol is not available on all devices, so a solution that works on ALL devices is currently not possible.

+2
source share

Some Galaxy tabs support the HID protocol, and some do not. It depends on the operator, not Samsung. My Verizon Galaxy Tab appeared without HID support, although T-Mobile had it. But in April of this year, Verizon (and not Samsung) released a firmware update that included HID support, so my BT keyboard and mouse started working. I am running Android 2.2 and my firmware number is SCH-I800.EC02.

I assume that you are trying to do this because your tab will not connect to the BT keyboard at all. This is exactly what I tried to do before April. From what I remember, part of the spell magic should be handled by Android automatically: when you make a HID connection, Android displays a message box to enter the code on the keyboard, then the socket connection returns to your program (or something like that).

So, if you cannot get Tab to connect normally to the keyboard, your HID profile is disabled, and (afaik) no programming will start it, except maybe overwriting the HID profile in Java.

I am sure that you have already tried this, but to check it, go to Settings> Wireless & networks> Bluetooth settings, you should see the keyboard in the list, do you have HID support. Click on the keyboard, it should connect immediately. If it just lingers indefinitely or an error message appears, then you do not have HID support.

+1
source share

All Articles