title: Disconnecting while pairing with a remote Bluetooth Low Energy (BLE) device
body: <p>Currently I start the pairing procedure (well it starts automatically to be honest) when i try to do a GATT operation and it fails to successfully complete that operation. Once the pairing procedure is finished and i get bonded with the remote device i continue to do my GATT operations in which everything works just fine.</p>
  
  <p>What i have noticed is that while pairing:</p>
  
  <p><strong>1)</strong> if i disconnect from the remote device without closing the GATT client then it still pairs successfully and i go into the <code>BluetoothDevice.BOND_BONDED</code> state and my app continues to work just fine.
  <br><strong>2)</strong> if i disconnect and i also close the GATT client then once i go into the <code>BluetoothDevice.BOND_BONDED</code> state then my app crashes</p>
  
  <p>Why does my app crashes when i close the GATT client of the remote device while in the pairing procedure? is that normal or am i doing something wrong?</p>
  
  <p>This is my BroadcastReceiver implementation for getting the bonding state of the remote device</p>
  
  <pre><code>private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
  
          if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
              final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
  
              if(state == BluetoothDevice.BOND_BONDING){
                  DebugWrapper.debugMsg("On Bonding...", TAG);
                  mIsBonding = true;
                  onBonding();
              } else if(state == BluetoothDevice.BOND_BONDED){
                  DebugWrapper.debugMsg("On Bonded", TAG);
                  mIsBonded = true;
  
                      mActivity.unregisterReceiver(mReceiver);
  
                      /*
                       * finish what we started
                       */
                      if(mBluetoothGatt != null){
  
                          if(mOperationState == OperationState.READ_CHARACTERISTIC){
                              readCharacteristic(mCurrentCharacteristic);
                          } else if(mOperationState == OperationState.WRITE_CHARACTERISTIC){
                              writeCharacteristic(mCurrentCharacteristic);
                          } else if(mOperationState == OperationState.READ_DESCRIPTOR){
                              readDescriptor(mCurrentDescriptor);
                          } else if(mOperationState == OperationState.WRITE_DESCRIPTOR){
                              writeDescriptor(mCurrentDescriptor);
                          }
                          mOperationState = OperationState.NONE;
                      }
  
                      onBonded();
  
              } else if(state == BluetoothDevice.BOND_NONE){
                  DebugWrapper.debugMsg("Not Bonded", TAG);
                  notBonded();
              }
          }
      }
  };
  </code></pre>
  
  <p><strong>Update LogCat files (Links to Pastebin.com as the files where too big to be added here)</strong></p>
  
  <p><strong><a href="http://pastebin.com/PVFLRNLd" rel="nofollow">From this logCat you can see the steps i am following</a></strong></p>
  
  <p><strong><a href="http://pastebin.com/igb0qCBK" rel="nofollow">App LogCat, complete</a></strong></p>