title: Unstable connection from Honor 8x to microchip rn4020 Bluetooth
body: <p>Here is programm to connect to rn4020 bluetooth module.
  My code is fine work at Honor 9 device its fast connection and stable.
  But its very long time to connect from Honor 8x and sometimes its connect only the third-fifth time. I can upload all project to github if it's necessary.</p>
  
  <p>Here is connection </p>
  
  <pre class="lang-java prettyprint-override"><code>android.bluetooth.le.ScanCallback scanCallback =
      new ScanCallback() {
          @Override
          public void onScanResult(int callbackType, final ScanResult result) {
          runOnUiThread(new Runnable() {                                              
              @Override
              public void run() {
              if(result.getDevice().getName()!=null){
                  if(!devs.contains(result.getDevice().getName())) {
                      devs.add(result.getDevice().getName());
                      adapter.notifyDataSetChanged();
                  }
              }
              if(!devices_adress.contains(result.getDevice())) {
                  if(result.getDevice().getName()!=null) {
                      devices_adress.add(result.getDevice());
                      String dev_temp = result.getDevice().getName();
                  try {
                      if(number_dev.contains(bonded_devise)){
                          mBluetoothAdapter.cancelDiscovery();
                          mBluetoothGatt = result.getDevice().connectGatt(getContext(), true, mGattCallback);
                          mBluetoothGatt.connect();
                          bluetoothLeScanner.stopScan(scanCallback);
                      }
                  }
                  catch(Exception e){
                      Log.w("err1",e.getMessage());
                  }
  
                  }
  
              }
              }
              });
          }
      };
  </code></pre>
  
  <p>And then I waiting to find all services and start transmition</p>
  
  <pre class="lang-java prettyprint-override"><code>private void findMldpGattService(List&lt;BluetoothGattService&gt; gattServices) {
          if (gattServices == null) {
              Log.d(TAG, "findMldpGattService found no Services");
              return;
          }
          String uuid;
  
  
          for (BluetoothGattService gattService : gattServices) {
              uuid = gattService.getUuid().toString();
              if (uuid.equals(MLDP_PRIVATE_SERVICE)) {
                  List&lt;BluetoothGattCharacteristic&gt; gattCharacteristics = gattService.getCharacteristics();
                  for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                      uuid = gattCharacteristic.getUuid().toString();
                      if (uuid.equals(MLDP_DATA_PRIVATE_CHAR)) {
                          mDataMDLP = gattCharacteristic;
  
                          Log.d("777", "Found MLDP data characteristics");
                      }
                      else if (uuid.equals(MLDP_CONTROL_PRIVATE_CHAR)) {
                          mControlMLDP = gattCharacteristic;
                          //bondedBL.createBond();
                          Log.d("777", "Found MLDP control characteristics");
                          runOnUiThread(new Runnable() {
  
                                  @Override
                                  public void run() {
  
                                      final TextView connection = (TextView) findViewById(R.id.textView4);
                                      mHandler.postDelayed(new Runnable() {                                      
                                          @Override
                                          public void run() {
                                              connection.setVisibility(View.INVISIBLE);
                                          }
                                      }, 3000);
  
                                  }
                          });
  
                      }
                      final int characteristicProperties = gattCharacteristic.getProperties();
                      if ((characteristicProperties &amp; (BluetoothGattCharacteristic.PROPERTY_NOTIFY)) &gt; 0) {
                          mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
                          BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
                          descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                          mBluetoothGatt.writeDescriptor(descriptor);
                      }
                      if ((characteristicProperties &amp; (BluetoothGattCharacteristic.PROPERTY_INDICATE)) &gt; 0) {
                          mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
                          BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
                          descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                          mBluetoothGatt.writeDescriptor(descriptor);
                      }
                      if ((characteristicProperties &amp; (BluetoothGattCharacteristic.PROPERTY_WRITE)) &gt; 0) {
                          gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                      }
                      if ((characteristicProperties &amp; (BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) &gt; 0) {
                          gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                      }
                  }
                  break;
              }
          }
          if (mDataMDLP == null) {
              Log.d(TAG, "findMldpGattService found no MLDP service");
              finish();
          }
      }
  </code></pre>