如何在android中使用蓝牙HID配置文件?

时间:2017-10-12 06:50:36

标签: android bluetooth bluetooth-lowenergy android-bluetooth hid

我想让我的Android设备充当使用蓝牙隐藏或任何配置文件的计算机或任何其他设备的输入设备。

只要我通过蓝牙连接客户端设备,我就可以将我的Android设备用作鼠标或键盘,就像无线键盘或鼠标一样。

经过大量研究后我才知道android不支持HID配置文件,所以我怎样才能实现这一点,有任何方法可以做到这一点,我已经根据设备与我的任何帮助将不胜感激。

编辑:计算机或其他任何应该将Android设备检测为无线鼠标,而不是将其检测为Android设备,这样我就无需在设备的控制端安装任何其他应用程序。

谢谢。

1 个答案:

答案 0 :(得分:0)

获取MAC地址(device_UUID):

BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID_PC);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(cmdJsonObject); // for example
socket.close();

来自android的init cmd,就像JsonObject:

LocalDevice localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service
String url = "btspp://localhost:" + device_UUID_Android + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);
StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();

String cmdJsonObject;
while (true) {
    cmdJsonObject = dis.readUTF();
}

connection.close();

在两台设备之间建立连接: 对于Android:

Robot robot = new Robot();
JsonObject jsonCMD = JsonObject(cmdJsonObject);
if(jsonCMD.containsKey("mouseMove")){
    JsonArray jsonData = jsonCMD.getJsonArray("data");
    int x = jsonData.getInt(0);
    int y = jsonData.getInt(1);
    robot.mouseMove(x, y);
}

对于PC:

null

使用Robot执行命令创建服务:

var formData = {
  txToClose: '1234,5678,98549',
  sno:  '0195'
};

$.ajax({
  type: 'post',
  url: url,
  async: false,  
  data: JSON.stringify(formData),
  contentType: "application/json",
  dataType: "json",      
  success: function(result, status, xhr) {
    console.log(result);
  }                           
});

我希望它可以帮到你!

感谢 @Victor Wong:Send data from android bluetooth to PC with bluecove

@Faisal Feroz:Moving the cursor in Java

相关问题