title: Programmatically pair Bluetooth device without the user entering pin
body: <p>The Bluetooth device I am trying to connect has always the same pincode. This should make it possible to pair the device by setting the pin programmatically.</p>
  
  <p>After trying to search how this could be done, I ended up with the code below:</p>
  
  <pre><code>BluetoothDevice device = getDevice();
  
  //To avoid the popup notification:
  device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
  device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true);
  byte[] pin = ByteBuffer.allocate(4).putInt(1234).array();
  //int pinn = 1234;
  
  //Entering pin programmatically:  
  Method ms = device.getClass().getMethod("setPin", byte[].class);
  //Method ms = device.getClass().getMethod("setPasskey", int.class);
  ms.invoke(device, pin);
  
  //Bonding the device:
  Method mm = device.getClass().getMethod("createBond", (Class[]) null);
  mm.invoke(device, (Object[]) null);
  </code></pre>
  
  <p><code>cancelPairingUserInput</code> gives me a <code>NoSuchMethodException</code>, which is weird because the method does exist in <code>BluetoothDevice</code> class.</p>
  
  <p>Is looks like <code>Setpin</code> or <code>SetPasskey</code> doesn't do anything. The device just wont pair. It only pairs after manually entering the pin.</p>
  
  <p>So the only line of code that works is:</p>
  
  <pre><code>//Bonding the device:
  Method mm = device.getClass().getMethod("createBond", (Class[]) null);
  mm.invoke(device, (Object[]) null);
  </code></pre>
  
  <p>Logcat output:</p>
  
  <pre><code>09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean]
          java.lang.NoSuchMethodException: cancelPairingUserInput [boolean]
          at java.lang.Class.getConstructorOrMethod(Class.java:460)
          at java.lang.Class.getMethod(Class.java:915)
          at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97)
          at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25)
          at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79)
          at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756)
          at android.os.Handler.handleCallback(Handler.java:615)
          at android.os.Handler.dispatchMessage(Handler.java:92)
          at android.os.Looper.loop(Looper.java:137)
          at android.app.ActivityThread.main(ActivityThread.java:4921)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:511)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>So what am I doing wrong?</p>