Connecting to a bluetooth HID device (mouse) using L2CAP

SAN picture SAN · Feb 7, 2013 · Viewed 12.3k times · Source

I'm tring to find a way to connect to a HID bevice (mouse) using L2CAP, this for a android app. but i'm getting error when accepting the connection. I'm using reflection to create the socket. but some thing is wrong with this. can some one please direct me to a example code for android that connect to a HID device using L2CAP this way, but without rooting.

Answer

David Guyon picture David Guyon · Apr 1, 2013

What is your Android device and Android version? If it's Android 4.2, they're now using Broadcom as I understood and so we're only able to create SDP connection.

I'm having the same problem while making a bluetooth connection between my Nexus 7 (Android 4.2.2 with CyanogenMod ROM 10) and a Wiimote. This is an HID device so I need to use L2CAP. Last versions of Android were able to create this connection (we can figure out just by looking at the market). If you search an application to handle this on the market, you'll see by looking at the description that all devices with Android version 4.0+ are not supported.

I just found this post few minutes ago which could help you: stackoverflow.com/a/7838587/1772805

Let me know if you solve this. I'll keep you in touch if I found anything.

EDIT #1: I tried the solution on the link above. I changed it to use a different constructor like this:

private static final int TYPE_RFCOMM = 1;
private static final int TYPE_SCO = 2;
private static final int TYPE_L2CAP = 3;

/**
 * Create a BluetoothSocket using L2CAP protocol
 * Useful for HID Bluetooth devices
 * @param BluetoothDevice
 * @return BluetoothSocket
 */
private static BluetoothSocket createL2CAPBluetoothSocket(BluetoothDevice device){
  int type        = TYPE_L2CAP; // L2CAP protocol
  int fd          = -1;         // Create a new socket
  boolean auth    = false;      // No authentication
  boolean encrypt = false;      // Not encrypted
  int port        = 0;          // port to use (useless if UUID is given)
  ParcelUuid uuid = new ParcelUuid(wiimoteUuid); // Bluetooth UUID service

  try {
    Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
      int.class, int.class, boolean.class, boolean.class,
      BluetoothDevice.class, int.class, ParcelUuid.class);
    constructor.setAccessible(true);
    BluetoothSocket clientSocket = (BluetoothSocket) constructor.newInstance(
      type, fd, auth, encrypt, device, port, uuid);
    return clientSocket;
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}

I succeeded to create the socket but when I call the method connect(), I get this error: bt l2cap socket type not supported, type:3. This log is a very bad new for me because I found this thread which says Android 4.2 does not support L2CAP (or just disabled by Google..).

Because my device is rooted with CyanogenMod 10, the feature will maybe come back on a new release. I hope..

EDIT #2: Here's a link pointing on the C file containing the reason of the problem: btif_sock.c. If anyone knows if it's possible re-write this file or how to add the L2CAP feature to Android with an external C library. I'm afraid it's not a simple task.