title: Galaxy s6 Android LL: onConnectionStateChange return me 133 Error Status
body: <p>I have developed an app that it have to reconnect to a server app that is installed on a certain device. I have tested my app on some device, but on galaxy s6 with Lollipop I have some problem.</p>
  
  <p>This are the combination :</p>
  
  <pre><code>client (peripherall) is installed on galaxy tab 10.1 kitkat 
  server (central role) is installed on galaxy s6 with lollipop 
  ok
  
  client (peripherall) is installed on galaxy nexus 6 lollipop
  server (central role) is installed on galaxy s6 with lollipop 
  ok
  
  client (peripherall) is installed on galaxy s6 lollipop
  server (central role) is installed on galaxy nexus 6 with lollipop 
  not working
  </code></pre>
  
  <p>On the last combination the connection function return me 133 status.</p>
  
  <p>This is my code :</p>
  
  <pre><code>public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
      super.onConnectionStateChange(gatt, status, newState);
      Log.d("connect", "status = " + status + " newState = " + newState);
  ....
  </code></pre>
  
  <p>What is the difference between Kitkat and Lollipop ? Do I have to handle the connection in different way ?</p>
  
  <pre><code>public void new_automatization() {
      automatizationInProgress = true;
      Log.d("connect","new auto: si parte");
      this.init();
      Bluetooth.getMe().stopScan();
      final String MACaddress=getDataFromPreferences("address");
      final String keycode=getDataFromPreferences("unlockcode");  
  
      if(!Bluetooth.getMe().isScanning()) {
          Bluetooth.getMe().startScan(new Bluetooth.ScanCallback() {
              boolean deviceIsFound = false;
  
              @Override
              public void onDeviceFound(final BluetoothDevice device, int rssi,byte[] scanRecord) {
                  Log.d("testAlpha","dentro onDeviceFound");
                  if(device != null) {
                      String address=new String();                    
                      if(device.getName() != null) {  
                          Log.d("testAlpha","trovato name: "+device.getName()+"mac: "+device.getAddress());
                          address=device.getName();
                          if(address.equals(MACaddress)) {
                              Log.d("testAlpha","mi connetto a: "+device.getName()+"mac: "+device.getAddress());
                              Bluetooth.getMe().stopScan();//possiamo riconnetterci
                              deviceIsFound=true;
                              setDevice(device);
                              unlock(keycode);
                              automatizationInProgress = false;
                          }
                      }
                  } else {
                      Log.i("connect","dentro onDeviceFound null");
                      search_ended=true;
                      sendMessage(DEVICE_NOT_FOUND);
                      automatizationInProgress = false;
                  }   
              }
          });
      }
  }
  </code></pre>
  
  <p>This is the startscan and stopscan function :</p>
  
  <pre><code>    @SuppressWarnings("deprecation")
      public boolean startScan(ScanCallback callback, final long timeout) {
  
          stopScan();         
          scanCallback = callback;
          /*
            if (isScanning()) {
              stopScan();
              return true;
            }
            scanCallback = callback;
  
          // Stops scanning after a pre-defined scan period (10s).
          stopTimer = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      Thread.sleep(timeout);
                  } catch (InterruptedException e) {
                      Log.i("debug","interrupted");
                      return;
                  }
  
                  Log.i("debug","bluetooth: inizia lo stopscan()");
  
                  if(!isScanning())
                      return;
  
                  Log.i("debug","amgbluetooth: pre scansione");
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                      BluetoothLeScanner scanner = bluetoothAdapter
                              .getBluetoothLeScanner();
                      scanner.stopScan(scanCallbackAfterLollipop);
                      scanning = false;
                  } else {
                      bluetoothAdapter.stopLeScan(scanCallbackBeforeLollipop);
                      scanning = false;
                  }
                  Log.i("debug","amgbluetooth:stopTimer, invio null");
  
                  if(!Thread.currentThread().isInterrupted())
                      scanCallback.onDeviceFound(null, 0, null);
  
              }
          });
  
          stopTimer.start();*/
          pippo.removeCallbacks(solPippo);
          pippo.postDelayed(solPippo, timeout);
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
  
              Log.i("finalT","amgbluetooth: inizia lo startscan() LL");
  
              BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
              scanning = true;
  
              scanCallbackAfterLollipop = new ScanCallback() {
                  @Override
                  public void onScanResult(int callbackType, ScanResult result) {
                      super.onScanResult(callbackType, result);
                      Log.d("finalT", "Single Result: "+result.getDevice().getName());
  
                      processResult(result);
                      /*
                      scanCallback.onDeviceFound(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes());*/
                  }
  
                  @Override
                  public void onBatchScanResults(List&lt;ScanResult&gt; results) {
                      Log.d("finalT", "onBatchScanResults: "+results.size()+" results");
                      /*
                        for (ScanResult result : results) {
                            processResult(result);
                        }*/
                    }
  
                    @Override
                    public void onScanFailed(int errorCode) {
                        Log.w("finalT", "LE Scan Failed: "+errorCode);
                    }
  
                    private void processResult(ScanResult result) {
                          BluetoothDevice device = result.getDevice();
                          Log.i("finalT", "New LE Device: " + device.getName() + " @ " + result.getRssi());
  
                          scanCallback.onDeviceFound(result.getDevice(),
                                  result.getRssi(), result.getScanRecord()
                                          .getBytes());
  
                          //stopScan();
                      }
                  };
  
                  ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).setReportDelay(0).build();
                  scanner.flushPendingScanResults(scanCallbackAfterLollipop);
                  scanner.startScan(null,settings,scanCallbackAfterLollipop);
                  return true;
              } else {
                  Log.i("finalT","amgbluetooth: inizia lo startscan() &lt; LL");
                  scanning = true;
                  scanCallbackBeforeLollipop = new LeScanCallback() {
  
                  @Override
                  public void onLeScan(BluetoothDevice device, int rssi,
                          byte[] scanRecord) {
  
                      Log.i("finalT","device trovato: "+device.getName());
                      scanCallback.onDeviceFound(device, rssi, scanRecord);
                  }
              };
  
  
              return bluetoothAdapter.startLeScan(scanCallbackBeforeLollipop);
          }
      }
  
      public void stopScan() {
          Log.i("debug1","stopScan()");
          if(isScanning()) {
              Log.i("debug","stopScan() inside if");
              //stopTimer.interrupt();
              pippo.removeCallbacks(solPippo);
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                  BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
                  scanner.stopScan(scanCallbackAfterLollipop);
                  scanning = false;
              } else {
                  bluetoothAdapter.stopLeScan(scanCallbackBeforeLollipop);
                  scanning = false;
              }
          }
      }
  </code></pre>
  
  <p>and in the <code>unlock()</code> I call connect function.</p>
  
  <p>P.S.: the device's name that I want to connect to(nexus 6) is in the macaddress variable. </p>