private void directConnect()

in rx-central-ble-sample/src/main/java/com/uber/rxcentralble/sample/MainActivity.java [219:264]


  private void directConnect() {
    if (connection == null) {
      String name = nameEditText.getEditableText().toString();
      if (!TextUtils.isEmpty(name)) {
        Timber.i("Connect to:  " + name);
        connectButton.setText("Cancel");

        connection = connectionManager
                .connect(new NameScanMatcher(name),
                        DEFAULT_SCAN_TIMEOUT * 20,
                        DEFAULT_CONNECTION_TIMEOUT)
                .retryWhen(errors ->
                        errors.flatMap(
                            error -> {
                              if (error instanceof ConnectionError) {
                                return Observable.just(Irrelevant.INSTANCE);
                              }

                              return Observable.error(error);
                            }))
                .subscribe(
                        peripheral -> {
                          peripheralManager.setPeripheral(peripheral);

                          AndroidSchedulers.mainThread().scheduleDirect(() -> connectButton.setText("Disconnect"));
                          Timber.i("Connected to: " + name);
                          Timber.i("Max Write Length (MTU): " + peripheral.getMaxWriteLength());
                        },
                        error -> {
                          connection.dispose();
                          connection = null;

                          AndroidSchedulers.mainThread().scheduleDirect(() -> connectButton.setText("Connect"));
                          Timber.i("Connection error: " + error.getMessage());
                        }
                );

      }
    } else {
      connection.dispose();
      connection = null;

      connectButton.setText("Connect");
      Timber.i("Disconnected");
    }
  }