How to talk to a Bluetooth keyboard?

后端 未结 3 1041
南笙
南笙 2020-12-14 13:16

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.

         


        
3条回答
  •  我在风中等你
    2020-12-14 14:06

    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 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.

提交回复
热议问题