title: Android stops finding BLE devices: onClientRegistered() - status=133 clientIf=0
body: <p>I am developing an app in which I can both find and configure BLE devices. I am using standard Android BLE API, but recently I've encountered some strange problems. </p>
  
  <p>When I turn on my app the BLE scan works OK. I am scanning using:</p>
  
  <pre><code>mBluetoothAdapter.startLeScan(mLeScanCallback); // for Kitkat and below
  </code></pre>
  
  <p>and</p>
  
  <pre><code>mBluetoothAdapter.getBluetoothLeScanner().startScan(mScanCallback); // for Lollipop and above
  </code></pre>
  
  <p>In the Logcat I am getting following messages (I guess this is important for this issue):</p>
  
  <pre><code>D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
  </code></pre>
  
  <p>In my app I can also read certain characteristics from my BLE devices (eg. battery state). I connect to a device to read this characteristic in a separate fragment using:</p>
  
  <pre><code>mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
  mBluetoothAdapter = mBluetoothManager.getAdapter();
  mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
  mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
  </code></pre>
  
  <p>The characteristics are read correctly. In the <code>onCharacteristicRead</code> callback I also disconnect and close Gatt:</p>
  
  <pre><code>mBluetoothGatt.disconnect();
  mBluetoothGatt.close();
  </code></pre>
  
  <p>Each time I open a fragment to read a characteristic (no matter whether it is the same device or not) <code>clientIf</code> value increases. I can see in the LogCat:</p>
  
  <pre><code>D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
  D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
  D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
  D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
  D/BluetoothGatt: onClientRegistered() - status=0 clientIf=10
  </code></pre>
  
  <p>Everything works fine until the <code>clientIf</code> value equals 10. BLE scan stops finding any devices, I can't connect to any of my devices to read any characteristics etc. And the LogCat infinitely displays these messages:</p>
  
  <pre><code>D/BluetoothGatt: unregisterApp() - mClientIf=0
  D/BluetoothGatt: onClientRegistered() - status=133 clientIf=0
  </code></pre>
  
  <p>The only way to fix it is to kill the app and relaunch it or restart Bluetooth by turning it off and on manually. I've encountered this issue only on certain devices such as Xperia Z1 (Android KitKat) and Galaxy S4 (Android Lollipop). I couldn't reproduce this issue on Xperia Z3 Compact running Android Marshmallow...</p>
  
  <p>Is there anything I can do about it? I've already tried multiple "solutions" such as - calling all BLE methods from the UI thread and closing/disconnecting GATT, but nothing helps. How can I fix it?</p>