在 RxAndroidBle 中,如何 return ObservableSource<out TypeVariable<R>!>!来自 Observable.combineLatest() 而不是单位?
In RxAndroidBle, How do return ObservableSource<out TypeVariable<R>!>! from Observable.combineLatest() instead of Unit?
我正在尝试从特定设备上的 Device Information
服务获取所有特征。我可以选择任何单个设备并使用以下方法从中获取特性:
deviceConnection =
device.establishConnection(true)
.flatMapSingle { rxBleConnection ->
rxBleConnection
.readCharacteristic(java.util.UUID.fromString(c.uuid))
}
.subscribe(
{ onConnectionSuccess(it, v) },
{ onConnectionFailure(it, v) })
问题出在我尝试一次读取多个特征时。我尝试使用 Polidea's blog detailing multiple reads 中的示例文档、关于 SO 的多个问题以及 RxJava 可观察对象的文档,但我无法弄清楚我做错了什么。
我从 AndroidStudio 得到的错误是:
Type mismatch.
Required: ObservableSource<out TypeVariable<R>!>!
Found: Unit
据我所知,我正在以与所有其他示例相同的方式进行操作。我试图读取多个特征的代码如下:
device = BleApplication.getRxBleClient(this)
.getBleDevice(macAddress)
deviceConnection =
device.establishConnection(false)
.flatMap { rxBleConnection ->
Observable.combineLatest(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())),
::BoseDevice
)
}
.take(1)
.subscribe({}, {}) //onConnectionSuccess(it) }, { onConnectionFailure(it) })
class 我正在尝试将可观察对象输入:
internal class BoseDevice(
val manufacturer: ByteArray,
val modelNumber: ByteArray,
val serialNumber: ByteArray,
val hardwareRevision: ByteArray,
val firmwareRevision: ByteArray,
val softwareRevision: ByteArray,
val systemID: ByteArray,
val pnpID: ByteArray
)
这些是我在 gradle 文件中声明的依赖项:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
implementation 'com.google.android.material:material:1.1.0'
implementation "io.reactivex.rxjava2:rxjava:2.2.19"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
众所周知,这些类型问题很难用 Kotlin 调试(至少现在是 1.3.70)。
您有两个错误导致您无法使用工作代码:
首先 — 您尝试 .combineLatest()
类型 Single
的对象,而不是 Observable
。因此,您应该将 .flatMapSingle()
与 Single.zip()
结合使用,或者将每次读取更改为 Observable
,例如 rxBleConnection.readCharacteristic().toObservable()
.
其次——您尝试将构造函数用作 Function8
显然不能以这种方式使用的对象。您必须手动创建此功能
device.establishConnection(false)
.flatMapSingle { rxBleConnection ->
Single.zip(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())),
Function8 { a, b, c, d, e, f, g, h -> BoseDevice(a, b, c, d, e, f, g, h)}
)
}
.take(1)
.subscribe({}, {})
或
device.establishConnection(false)
.flatMap { rxBleConnection ->
Observable.combineLatest(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())).toObservable(),
Function8 { a, b, c, d, e, f, g, h -> BoseDevice(a, b, c, d, e, f, g, h)}
)
}
.take(1)
.subscribe({}, {})
我正在尝试从特定设备上的 Device Information
服务获取所有特征。我可以选择任何单个设备并使用以下方法从中获取特性:
deviceConnection =
device.establishConnection(true)
.flatMapSingle { rxBleConnection ->
rxBleConnection
.readCharacteristic(java.util.UUID.fromString(c.uuid))
}
.subscribe(
{ onConnectionSuccess(it, v) },
{ onConnectionFailure(it, v) })
问题出在我尝试一次读取多个特征时。我尝试使用 Polidea's blog detailing multiple reads 中的示例文档、关于 SO 的多个问题以及 RxJava 可观察对象的文档,但我无法弄清楚我做错了什么。
我从 AndroidStudio 得到的错误是:
Type mismatch.
Required: ObservableSource<out TypeVariable<R>!>!
Found: Unit
据我所知,我正在以与所有其他示例相同的方式进行操作。我试图读取多个特征的代码如下:
device = BleApplication.getRxBleClient(this)
.getBleDevice(macAddress)
deviceConnection =
device.establishConnection(false)
.flatMap { rxBleConnection ->
Observable.combineLatest(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())),
::BoseDevice
)
}
.take(1)
.subscribe({}, {}) //onConnectionSuccess(it) }, { onConnectionFailure(it) })
class 我正在尝试将可观察对象输入:
internal class BoseDevice(
val manufacturer: ByteArray,
val modelNumber: ByteArray,
val serialNumber: ByteArray,
val hardwareRevision: ByteArray,
val firmwareRevision: ByteArray,
val softwareRevision: ByteArray,
val systemID: ByteArray,
val pnpID: ByteArray
)
这些是我在 gradle 文件中声明的依赖项:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
implementation 'com.google.android.material:material:1.1.0'
implementation "io.reactivex.rxjava2:rxjava:2.2.19"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
众所周知,这些类型问题很难用 Kotlin 调试(至少现在是 1.3.70)。
您有两个错误导致您无法使用工作代码:
首先 — 您尝试 .combineLatest()
类型 Single
的对象,而不是 Observable
。因此,您应该将 .flatMapSingle()
与 Single.zip()
结合使用,或者将每次读取更改为 Observable
,例如 rxBleConnection.readCharacteristic().toObservable()
.
其次——您尝试将构造函数用作 Function8
显然不能以这种方式使用的对象。您必须手动创建此功能
device.establishConnection(false)
.flatMapSingle { rxBleConnection ->
Single.zip(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())),
Function8 { a, b, c, d, e, f, g, h -> BoseDevice(a, b, c, d, e, f, g, h)}
)
}
.take(1)
.subscribe({}, {})
或
device.establishConnection(false)
.flatMap { rxBleConnection ->
Observable.combineLatest(
rxBleConnection
.readCharacteristic(jUuid(ManufacturerNameString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(ModelNumberString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SerialNumberString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(HardwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(FirmwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SoftwareRevisionString.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(SystemID.toString())).toObservable(),
rxBleConnection
.readCharacteristic(jUuid(PnPID.toString())).toObservable(),
Function8 { a, b, c, d, e, f, g, h -> BoseDevice(a, b, c, d, e, f, g, h)}
)
}
.take(1)
.subscribe({}, {})