title: How to programmatically pair and connect a HID bluetooth device(Bluetooth Keyboard) on Android
body: <p>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 - <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles" rel="noreferrer">http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles</a></p>
  
  <p>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.</p>
  
  <ul>
  <li>Headset</li>
  <li>A2DP</li>
  <li>Health Device</li>
  </ul>
  
  <p>There is no documentation how to implement BluetoothProfile for HID bluetooth device(Keyboard)</p>
  
  <p>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 - </p>
  
  <pre><code>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&lt;BluetoothProfile&gt; clazz = BluetoothProfile.class;
      for (Field f : clazz.getFields()) {
          int mod = f.getModifiers();
          if (Modifier.isStatic(mod) &amp;&amp; Modifier.isPublic(mod) &amp;&amp; Modifier.isFinal(mod)) {
              try {
                  if (f.getName().equals("INPUT_DEVICE")) {
                      return f.getInt(null);
                  }
              } catch (Exception e) {
                  Log.e("", e.toString(), e);
              }
          }
      }
      return -1;
  }
  </code></pre>