【问题标题】:How to use bluetooth HID profile in android?如何在 android 中使用蓝牙 HID 配置文件?
【发布时间】:2017-10-12 06:50:36
【问题描述】:

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

一旦我通过蓝牙连接客户端设备,我应该能够将我的安卓设备用作鼠标或键盘,就像无线键盘或鼠标一样。

经过大量研究,我知道 android 不支持 HID 配置文件,所以我怎么能做到这一点,有什么办法可以做到这一点,我有根设备,任何帮助将不胜感激。

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

谢谢。

【问题讨论】:

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


    【解决方案1】:

    获取 MAC 地址 (device_UUID):

    InetAddress address = InetAddress.getLocalHost();
    NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
    byte[] macAddress = networkInterface.getHardwareAddress();
    StringBuilder sb = new StringBuilder();
    for (int byteIndex = 0; byteIndex < macAddress.length; byteIndex++) {
        sb.append(String.format("%02X", macAddress[byteIndex]));
        if((byteIndex < macAddress.length - 1)){
            sb.append("-");
        }
    }
    

    从android初始化cmd,比如JsonObject:

    mouseMove : {"cmd" : "mouseMove", "data" : [100, 200]}
    mousePress : {"cmd" : "mousePress", "data" : [100]}
    keyPress : {"cmd" : "keyPress", "data" : [100]}
    

    在两个设备之间建立连接: 对于安卓:

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

    对于电脑:

    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();
    

    使用机器人执行命令创建服务:

    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);
    }
    

    希望对你有帮助!

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

    @Faisal Feroz:Moving the cursor in Java

    【讨论】:

    • 感谢您的回复。无论您建议什么,我已经实现了该解决方案,但我不想在计算机上运行任何其他应用程序,我的计算机应该将 android 设备检测为无线鼠标,而不是将其检测为 android 设备。
    • 天哪!这是不可能的
    • 可以通过与司机互动来实现。
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多