title: How to solve the error onClientConnectionState() - status=22 clientIf=7 in BLE
body: <p>I have an Android application which uses <strong>Bluetooth Low Energy</strong> (BLE) to connect with my phone. The application worked well in almost all phones such as Galaxy S5 (Android 5.0.1), Galaxy S6 (Android 6.0). However, it has a critical issue in Galaxy Note 3 (Android 5.0.1).</p>
  <p>When I call the <code>connect()</code> function, and then call the <code>readCharacteristic()</code>, it returns the error:</p>
  <blockquote>
  <p>onClientConnectionState() - status=22 clientIf=7</p>
  </blockquote>
  <p>The error happens after I call the <code>connect</code> function for about 2 seconds.   I look up some solution, such as making a delay before calling<code> readCharacteristic()</code>, but it cannot solve.</p>
  <p>Could you have any way to solve my issue?</p>
  <p>Thank all.</p>
  <p>This is my code:</p>
  <pre><code> private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
     @Override
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
              
       if (newState == BluetoothProfile.STATE_CONNECTED) {               
          mBluetoothGatt.discoverServices();      
       } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {}
       }
  
       @Override
       public void onServicesDiscovered(BluetoothGatt gatt, int status) {
         if (status == BluetoothGatt.GATT_SUCCESS) {
            try {
              Thread.sleep(500);
              readCharacteristic();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
  
         } else {
             Log.w(TAG, &quot;onServicesDiscovered received: &quot; + status);
         }
       }
  
       @Override
       public void onCharacteristicRead(BluetoothGatt gatt,
                                           BluetoothGattCharacteristic characteristic,
                                           int status) {           
       }
  
       @Override
       public void onCharacteristicChanged(BluetoothGatt gatt,
                                              BluetoothGattCharacteristic characteristic) {            
       }
   };
  
   public void readCharacteristic() {
     /*check if the service is available on the device*/
     BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString(&quot;00001c00-d102-11e1-9b23-00025b00123&quot;));
     /*get the read characteristic from the service*/
     BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString(&quot;00001233-d102-11e1-9b23-00025b00a5a5&quot;));
     mBluetoothGatt.setCharacteristicNotification(mReadCharacteristic, true);
     BluetoothGattDescriptor descriptor = mReadCharacteristic.getDescriptor(
                  UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     mBluetoothGatt.writeDescriptor(descriptor);
   }
  </code></pre>
  <p>This is my full service function file to connect BLE</p>
  <pre><code>public class ConnectionService extends Service{
      private BluetoothLeService mBluetoothLeService;
      public String mDeviceAddress=&quot;0A:1B:6C:22:11:3D&quot;;
      private ServiceMonitor serviceMonitor = ServiceMonitor.getInstance();
      private final ServiceConnection mServiceConnection = new ServiceConnection() {
  
          @Override
          public void onServiceConnected(ComponentName componentName, IBinder service) {           
              mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
              if (!mBluetoothLeService.initialize()) {
                  Log.e(TAG, &quot;Unable to initialize Bluetooth&quot;);
              }
              mBluetoothLeService.connect(mDeviceAddress);
          }
          @Override
          public void onServiceDisconnected(ComponentName componentName) {
              mBluetoothLeService = null;
          }
      };
  
      @Override
      public void onCreate() {
          super.onCreate();        
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
      @Override
      public void onDestroy() {
          if(mGattUpdateReceiver!=null) {
              unregisterReceiver(mGattUpdateReceiver);
          }
          if(mServiceConnection!=null) {
              unbindService(mServiceConnection);
          }
      }
  
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId)
      {      
          //If some device is connecting to phone, let disconnect 
          if(mConnected==true) {
              mBluetoothLeService.disconnect();           
          } 
          //If bluetooth is enable    
          if (BleUtils.getBluetoothAdapter(getApplicationContext()).enable()) {
              Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
              bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
              registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
              //Try to reconnect if the connect is not successful
              if (mBluetoothLeService != null) {
                  new CountDownTimer(3000,1000) {
                      public void onTick(long millisUntilFinished) {}
                      public void onFinish() {
                          final boolean result = mBluetoothLeService.connect(mDeviceAddress);
                          Log.d(TAG, &quot;Connect request result=&quot; + result);
                      }
                  }.start();
              }            
          }
          return START_STICKY;
  }
  </code></pre>