title: set bluetooth authentication PIN when listening from adapter
body: <p>I'm making an app that needs to connect with a bluetooth device and get data from it... that device is set as master, so I needed to implement a <code>Thread</code>, where I <code>listenUsingRfcommWithServiceRecord</code> and wait for a connection from it:</p>
  
  <pre><code>    public AcceptThread(Context context, String serverName, UUID myUUID) {
      this.context = context;
      BluetoothServerSocket tmp = null;
  
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      try {
  
          // MY_UUID is the app's UUID string, also used by the client code
          tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, myUUID);
      } catch (IOException e) { }
      mmServerSocket = tmp;
  }
  </code></pre>
  
  <p>Then on <code>run</code> I run the code <code>socket = mmServerSocket.accept(5000)</code> to wait until it starts pairing with the device:</p>
  
  <pre><code>    public void run() {
      BluetoothSocket socket = null;
      while (true) {
          try {
              socket = mmServerSocket.accept(5000);
          } catch (IOException e) {
              Log.e(TAG,"IOException: " + e);
          }
  
          // If a connection was accepted
          if (socket != null) {
  
              // Manage the connection
              ManageConnectedSocket manageConnectedSocket = new ManageConnectedSocket(socket);
              manageConnectedSocket.run();
  
              try {
                  mmServerSocket.close();
              } catch (IOException e) {
                  Log.e(TAG, "IOException: " + e);
              }
              break;
          }
      }
  }
  </code></pre>
  
  <p>The Device asks for an authentication PIN, and I need to have an automatic procedure... for that I though of implementing a broadcast receiver to know when the device is asked to par with another device:</p>
  
  <pre><code>IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
  context.registerReceiver(mPairReceiver, filter);
  </code></pre>
  
  <p>and receive it:</p>
  
  <pre><code>private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
  
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (ACTION_PAIRING_REQUEST.equals(action)) {
              Log.e(TAG,"ACTION_PAIRING_REQUEST");
              setBluetoothPairingPin(device);
          }
      }
  };
  </code></pre>
  
  <p>In my <code>setBluetoothPairingPin</code> method I receive a <code>BluetoothDevice</code> object : </p>
  
  <pre><code>public void setBluetoothPairingPin(BluetoothDevice device) {
      byte[] pinBytes = convertPinToBytes("0000");
      try {
          Log.e(TAG, "Try to set the PIN");
          Method m = device.getClass().getMethod("setPin", byte[].class);
          m.invoke(device, pinBytes);
          Log.e(TAG, "Success to add the PIN.");
          try {
              device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
              Log.e(TAG, "Success to setPairingConfirmation.");
          } catch (Exception e) {
              // TODO Auto-generated catch block
              Log.e(TAG, e.getMessage());
              e.printStackTrace();
          }
      } catch (Exception e) {
          Log.e(TAG, e.getMessage());
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>The problem is that I can't know when my socket receives information, and consecutively, can't know what is my <code>BluetoothDevice</code> to set Pairing Pin before it's connected...
  Can someone help me on how to surpass this? Or is there other way to put the pin authentication when I'm listenning from <code>BluetoothServerSocket</code>?</p>
  
  <p>If I'm not explaining correctly, please let me know... </p>
  
  <p>Thanks in advance</p>