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.
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.
Some Galaxy Tabs support the HID protocol, some don't. It depends on the carrier, not on Samsung. My Verizon Galaxy Tab came without HID support even though the T-Mobile ones had it. But in April of this year Verizon (not Samsung) pushed out a firmware upgrade that included HID support so my BT keyboard & mouse started working. I'm running Android 2.2, and my firmware build number is SCH-I800.EC02.
It's my guess that you're trying to do this because your Tab won't connect to the BT keyboard at all. This is exactly what I was trying to do before April. From what I remember reading, the magic incantation part is supposed to be handled by Android automatically: when you make the HID connection, Android pops up a message window to enter a code on the keyboard, then the socket connection is returned to your program (or something to that effect).
So if you can't get the Tab to connect to the keyboard normally, then your HID profile has been disabled and (afaik) no amount of programming will make it work, except maybe a rewrite of the HID profile in Java.
I'm sure you tried this already, but to test it go to Settings > Wireless and Networks > Blutetooth settings, you should see the keyboard in the list whether or not you have HID support. Tap the keyboard entry, it should connect right away. If it just delays indefinitely or if you get an error message, then you have no HID support.
mringwal's answer is correct, besides the NDK approach, it is possible to 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 the constant value 3.
Note that this approach will only work on SOME android devices.
Writing a HID application is not a simple 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 learn the HID protocol and work flow , a copy is available here: http://www.dawidurbanski.pl/public/download/projekty/bluepad/HID_SPEC_V10.pdf
There are already professional programs doing exactly that, supporting HID on Android, see for example this software: http://teksoftco.com/index.php?section=product&pid=24
Because of Stack limitations, the L2CAP protocol is not available on all devices, so a solution that works on ALL devices is currently impossible.