How to programmatically pair and connect a HID bluetooth device(Bluetooth Keyboard) on Android

Passiondroid

I am able to pair a bluetooth keyboard but not able to connect so as to make it an input device. I went through the documentation provided at developer site - http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles

It says that the Android Bluetooth API provides implementations for the following Bluetooth profiles but you can implement the interface BluetoothProfile to write your own classes to support a particular Bluetooth profile.

  • Headset
  • A2DP
  • Health Device

There is no documentation how to implement BluetoothProfile for HID bluetooth device(Keyboard)

Android has itself implemented bluetooth connection for HID devices but those API's are hidden. I tried reflection to use them too. I do not get any error but keyboard does not get connected as input device. This is what i have done -

private void connect(final BluetoothDevice bluetoothDevice) {
    if(bluetoothDevice.getBluetoothClass().getDeviceClass() == 1344){
        final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
                @Override
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    Log.i("btclass", profile + "");

                    if (profile == getInputDeviceHiddenConstant()) {
                        Class instance = null;
                        try {
                            //instance = Class.forName("android.bluetooth.IBluetoothInputDevice");
                            instance = Class.forName("android.bluetooth.BluetoothInputDevice");
                            Method connect = instance.getDeclaredMethod("connect", BluetoothDevice.class);
                            Object value = connect.invoke(proxy, bluetoothDevice);
                            Log.e("btclass", value.toString());
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }



                    }
                }

                @Override
                public void onServiceDisconnected(int profile) {

                }
            };

            mBluetoothAdapter.getProfileProxy(this, mProfileListener,getInputDeviceHiddenConstant());


    }

}

public static int getInputDeviceHiddenConstant() {
    Class<BluetoothProfile> clazz = BluetoothProfile.class;
    for (Field f : clazz.getFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
            try {
                if (f.getName().equals("INPUT_DEVICE")) {
                    return f.getInt(null);
                }
            } catch (Exception e) {
                Log.e("", e.toString(), e);
            }
        }
    }
    return -1;
}
Passiondroid

Due to security reasons, it is not possible for third party applications to connect to a bluetooth keyboard as the application can be a keylogger. So it can be only done manually by the user.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to connect a mobile and a printer via bluetooth in android?

From Dev

Programmatically connect to bluetooth headset from android app

From Dev

How to connect the Bluetooth device by click the item of listview in Android?

From Dev

How to connect the Bluetooth device in Android

From Dev

Android 4.3: How to connect to multiple Bluetooth Low Energy devices

From Dev

Android Connect Bluetooth device automatically after pairing programmatically

From Dev

Enable bluetooth tethering android programmatically

From Dev

How to programmatically hide/disable emoticons on Android soft keyboard

From Dev

How to connect with CT10 bluetooth barcode scanner with android mobile?

From Dev

Connect to Bluetooth programmatically

From Dev

How to get Bluetooth HID host on Android 5.0+ working

From Dev

Android - show keyboard programmatically

From Dev

Android: How to rotate the keyboard 180 degrees programmatically?

From Dev

How to programmatically connect to paired Bluetooth device once connection is lost in Windows 10 UWP

From Dev

How to enable or disable incognito keyboard programmatically in android?

From Dev

How to use bluetooth keyboard with both Android and WIndows 8?

From Dev

How to connect the Bluetooth device in Android

From Dev

Connect Bluetooth headset programmatically in iOS?

From Dev

How to pair bluetooth device and connect with it?

From Dev

Enable bluetooth tethering android programmatically

From Dev

Programmatically connect to bluetooth headset from android app

From Dev

Programmatically pair bluetooth devices

From Dev

Ubuntu 15.04 Can't pair apple bluetooth keyboard

From Dev

Connect to Bluetooth programmatically

From Dev

Android: How to rotate the keyboard 180 degrees programmatically?

From Dev

Android - change keyboard programmatically

From Dev

Connect the keyboard and mouse reciever into a bluetooth dongle

From Dev

How do you close/hide the Android soft keyboard programmatically?

From Dev

connect to HID keyboard device as input device in android pragmatically

Related Related

  1. 1

    How to connect a mobile and a printer via bluetooth in android?

  2. 2

    Programmatically connect to bluetooth headset from android app

  3. 3

    How to connect the Bluetooth device by click the item of listview in Android?

  4. 4

    How to connect the Bluetooth device in Android

  5. 5

    Android 4.3: How to connect to multiple Bluetooth Low Energy devices

  6. 6

    Android Connect Bluetooth device automatically after pairing programmatically

  7. 7

    Enable bluetooth tethering android programmatically

  8. 8

    How to programmatically hide/disable emoticons on Android soft keyboard

  9. 9

    How to connect with CT10 bluetooth barcode scanner with android mobile?

  10. 10

    Connect to Bluetooth programmatically

  11. 11

    How to get Bluetooth HID host on Android 5.0+ working

  12. 12

    Android - show keyboard programmatically

  13. 13

    Android: How to rotate the keyboard 180 degrees programmatically?

  14. 14

    How to programmatically connect to paired Bluetooth device once connection is lost in Windows 10 UWP

  15. 15

    How to enable or disable incognito keyboard programmatically in android?

  16. 16

    How to use bluetooth keyboard with both Android and WIndows 8?

  17. 17

    How to connect the Bluetooth device in Android

  18. 18

    Connect Bluetooth headset programmatically in iOS?

  19. 19

    How to pair bluetooth device and connect with it?

  20. 20

    Enable bluetooth tethering android programmatically

  21. 21

    Programmatically connect to bluetooth headset from android app

  22. 22

    Programmatically pair bluetooth devices

  23. 23

    Ubuntu 15.04 Can't pair apple bluetooth keyboard

  24. 24

    Connect to Bluetooth programmatically

  25. 25

    Android: How to rotate the keyboard 180 degrees programmatically?

  26. 26

    Android - change keyboard programmatically

  27. 27

    Connect the keyboard and mouse reciever into a bluetooth dongle

  28. 28

    How do you close/hide the Android soft keyboard programmatically?

  29. 29

    connect to HID keyboard device as input device in android pragmatically

HotTag

Archive