为什么先调用方法发现后会自动断开连接

why call the method first will disconnect automatically after discovery

我是RxJava新手,看rxandroidble库的demo时,很纳闷为什么第一个可以断开ble。

@OnClick(R.id.connect)
    public void onConnectToggleClick() {
        bleDevice.establishConnection(false)
                .flatMap(RxBleConnection::discoverServices)
                .first() // Disconnect automatically after discovery
                .compose(bindUntilEvent(PAUSE))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnUnsubscribe(this::updateUI)
                .subscribe(adapter::swapScanResult, this::onConnectionFailure);

        updateUI();
    }

establishConnection()RxAndroidBle Observable behaviour 声明,仅发出一个值,而流未完成。还声明取消订阅可观察对象会断开连接。

Whenever you are no longer interested in keeping the connection open you should unsubscribe from it which will cause disconnection and cleanup of resources.

但是 first() 运算符发出单个值并在之后完成流。由于 bindUntilEvent(PAUSE) 也会在暂停时完成流,因此您根本不需要使用 first()。所以只要删除它,一切都应该没问题。