首页 > 解决方案 > MTU 更改如下示例

问题描述

我正在编写一个使用 RxAndroidBle 的 Android 应用程序,为了支持我的设备,我需要更高的 MTU

我按照提供的库示例:https ://github.com/Polidea/RxAndroidBle/wiki/Tutorial:-MTU-negotiation

但它没有编译


private ObservableTransformer<RxBleConnection, RxBleConnection> mtuNegotiationObservableTransformer = upstream -> {
        return upstream.doOnSubscribe(ignoredDisposable -> Log.i("MTU", "MTU negotiation is supported")
                     .flatMapSingle(connection ->
                                connection.requestMtu(GATT_MTU_MAXIMUM)
                                        .doOnSubscribe(ignoredDisposable -> Log.i("MTU", "Negotiating MTU started"))
                                        .doOnSuccess(mtu -> Log.i("MTU", "Negotiated MTU: " + mtu))
                                        .ignoreElement()
                                        .andThen(Single.just(connection)));
    };

编译器消息是:无法解析方法“flatmapsingle”

为什么它不起作用?在我的代码的其他部分,我使用 .flatMapSingle 没有问题。感谢您的帮助!

标签: javaandroidrx-java2rxandroidblemtu

解决方案


右括号的数量似乎有误。试试下面的代码:

private ObservableTransformer<RxBleConnection, RxBleConnection> mtuNegotiationObservableTransformer = upstream -> {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return upstream.doOnSubscribe(ignoredDisposable -> Log.i("MTU", "MTU negotiation is not supported")); // added a closing bracket here
    }

    return upstream
            .doOnSubscribe(ignoredDisposable -> Log.i("MTU", "MTU negotiation is supported")) // and here
                    .flatMapSingle(connection ->
                            connection.requestMtu(GATT_MTU_MAXIMUM)
                                    .doOnSubscribe(ignoredDisposable -> Log.i("MTU", "Negotiating MTU started"))
                                    .doOnSuccess(mtu -> Log.i("MTU", "Negotiated MTU: " + mtu))
                                    .ignoreElement()
                                    .andThen(Single.just(connection)));
};

推荐阅读