【问题标题】:Struggling converting java RxBle Code to Kotlin努力将 java RxBle 代码转换为 Kotlin
【发布时间】:2019-09-08 12:00:17
【问题描述】:

我目前正在使用 Kotlin 开发一个 Android 应用程序。这是我对这种编程语言的第一次体验,我目前正在努力将 Java 代码示例翻译成 Kotlin。

我想在 Kotlin 中实现this 问题的答案。

我当前的实现无法编译,因为 Ovservable::from 方法似乎已被删除。

这是我目前的做法:

connectionObservable!!.flatMap { connection ->
                    connection.discoverServices()
                            .flatMap { services ->
                                services.getService(UUID.fromString("My UUID")).map(BluetoothGattService::getCharacteristics)
                                          //here occurs the error, he wants a Single Source but got a observable with the ble characteristic
                                          .flatMap { characteristics: MutableList<BluetoothGattCharacteristic> -> Observable.fromIterable(characteristics) }
                                        .flatMap { characteristic: BluetoothGattCharacteristic ->
                                            connection.setupNotification(characteristic)
                                                    .flatMap { observable: Observable<ByteArray> -> observable ,Pair<BluetoothGattCharacteristic, ByteArray>(characteristic, observable)}

                                        }
                            }
                }.subscribe { pair: Pair<BluetoothGattCharacteristic, ByteArray> ->
                    genericModel[pair.first.uuid] = pair.second
                    throwable -> { /* handle errors */ }
                }

您能指出我的错误,以便我了解我做错了什么吗?

提前致谢! 乔纳斯

【问题讨论】:

  • 平面图 {Observable.from(getCharacteristics) 不够?
  • 我更新了代码 tro clearify。他想要一个 SingleSource 但在这行之后得到了一个 Observable

标签: android kotlin reactivex rxandroidble


【解决方案1】:

您的代码存在几个潜在问题。

  1. 首先,代码在语法上不正确——请看这一行:
.flatMap { observable: Observable<ByteArray> -> observable ,Pair<BluetoothGattCharacteristic, ByteArray>(characteristic, observable)}

我假设您可能想要flatMap observable: Observable&lt;ByteArray&gt;(为清楚起见我添加的类型)从中获取ByteArray 对象。这看起来像这样:

.flatMap { observable: Observable<ByteArray> -> observable.map { bytes -> Pair(characteristic, bytes) }}
  1. 此外,当您尝试从需要 SingleSource 的 lambda 中返回 Observable 时,代码将无法编译——正如您的编译器所说的那样。如果你有一个 Single 并且你会 .flatMap 它——它的 lambda 应该返回另一个 SingleSource。有一个 .flatMapObservable 函数需要 ObservableSource 这是你应该使用的。最终结果如下所示:
    connectionObservable!!.flatMap { connection ->
        connection.discoverServices()
            .flatMapObservable { services ->
                services.getService(UUID.fromString("My UUID")).map(BluetoothGattService::getCharacteristics)
                    //here occurs the error, he wants a Single Source but got a observable with the ble characteristic
                    .flatMapObservable { characteristics: MutableList<BluetoothGattCharacteristic> -> Observable.fromIterable(characteristics) }
                    .flatMap { characteristic: BluetoothGattCharacteristic ->
                        connection.setupNotification(characteristic)
                            .flatMap { observable: Observable<ByteArray> -> observable.map { bytes -> Pair(characteristic, bytes) }}

                    }
            }
    }.subscribe { pair: Pair<BluetoothGattCharacteristic, ByteArray> ->
        genericModel[pair.first.uuid] = pair.second
        throwable -> { /* handle errors */ }
    }
  1. Observable.fromIterable() 仍然是 Observable 的 API。您可能不使用正确包中的Observable 类。包java.util 中有一个Observable 类,但我们在这里使用来自RxJava 2 的类,它有包io.reactivex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2019-11-19
    • 2018-06-24
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多