@SuppressLint("NewApi") protected boolean createBond(){
  final BluetoothDevice device=mGatt.getDevice();
  if (device.getBondState() == BluetoothDevice.BOND_BONDED)   return true;
  boolean result;
  mRequestCompleted=false;
  mService.sendLogBroadcast(DfuBaseService.LOG_LEVEL_VERBOSE,"Starting pairing...");
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    mService.sendLogBroadcast(DfuBaseService.LOG_LEVEL_DEBUG,"gatt.getDevice().createBond()");
    result=device.createBond();
  }
 else {
    result=createBondApi18(device);
  }
  try {
synchronized (mLock) {
      while (!mRequestCompleted && !mAborted)       mLock.wait();
    }
  }
 catch (  final InterruptedException e) {
    loge("Sleeping interrupted",e);
  }
  return result;
}

@ SuppressLint ( "NewApi" ) protected boolean createBond ( ) { final BluetoothDevice device = mGatt . getDevice ( ) ; if ( device . getBondState ( ) == BluetoothDevice . BOND_BONDED ) return true ; boolean result ; mRequestCompleted = false ; mService . sendLogBroadcast ( DfuBaseService . LOG_LEVEL_VERBOSE , "Starting pairing..." ) ; if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . KITKAT ) { mService . sendLogBroadcast ( DfuBaseService . LOG_LEVEL_DEBUG , "gatt.getDevice().createBond()" ) ; result = device . createBond ( ) ; } else { result = createBondApi18 ( device ) ; } try { synchronized ( mLock ) { while ( ! mRequestCompleted && ! mAborted ) mLock . wait ( ) ; } } catch ( final InterruptedException e ) { loge ( "Sleeping interrupted" , e ) ; } return result ; }
creates bond to the device . works on all apis since 18th ( android 4 . 3 ) .
