使用L2CAP连接蓝牙HID设备(鼠标)

SAN*_*SAN 4 android bluetooth hid l2cap

我想找到一种方法来使用L2CAP连接到HID bevice(鼠标),这适用于Android应用程序.但是我在接受连接时遇到错误.我正在使用反射来创建套接字.但有些事情是错的.有人可以指导我一个Android的示例代码,用这种方式使用L2CAP连接到HID设备,但没有生根.

Dav*_*yon 11

你的Android设备Android版本是什么?如果它是Android 4.2,他们现在正在使用我理解的Broadcom,所以我们只能创建SDP连接.

我在Nexus 7(带有CyanogenMod ROM 10的Android 4.2.2)和Wiimote之间进行蓝牙连接时遇到了同样的问题.这是一个HID设备,所以我需要使用L2CAP.最新版本的Android能够创建这种连接(我们可以通过查看市场来弄清楚).如果您在市场上搜索应用程序以处理此问题,您将通过查看不支持Android 4.0版本的所有设备的说明来查看.

我刚刚在几分钟前发现这篇帖子可以帮到你:stackoverflow.com/a/7838587/1772805

如果你解决了这个问题,请告诉我.如果我找到任何东西,我会让你联系.

编辑#1:我在上面的链接上尝试了解决方案.我将其更改为使用不同的构造函数,如下所示:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

我成功创建了套接字但是当我调用该方法时connect(),我得到了这个错误:bt l2cap socket type not supported, type:3.这个日志对我来说是一个非常糟糕的新事物,因为我发现这个帖子Android 4.2不支持L2CAP(或者只是被谷歌禁用了......).

因为我的设备植根于CyanogenMod 10,所以该功能可能会在新版本上重新出现.我希望..

编辑#2:这是一个指向C文件的链接,其中包含问题的原因:btif_sock.c.如果有人知道是否可以重写此文件或如何使用外部C库将L2CAP功能添加到Android.我担心这不是一项简单的任务.