努力将 java RxBle 代码转换为 Kotlin

Struggling converting java RxBle Code to Kotlin

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

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

我当前的实现无法编译,因为 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 */ }
                }

你能指出我的错误,这样我就能明白我做错了什么吗?

提前致谢! 乔纳斯

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

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

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

.flatMap { observable: Observable<ByteArray> -> observable.map { bytes -> Pair(characteristic, bytes) }}
  1. 此外,当您尝试从期望 SingleSource 的 lambda 中 return 一个 Observable 时,代码将无法编译——正如您的编译器所说。如果你有一个 Single 并且你会 .flatMap 它 — 它的 lambda 应该 return 另一个 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 class。包 java.util 中有一个 Observable class 但我们在这里使用来自 RxJava 2 的包 io.reactivex