kotlin中可重复的值types

我试图从api中恢复电动滑板车的值,但api返回可观察types,我不明白为什么值得重复的值应该重复。 当应用程序从另一个字段中恢复一个值时,他们订阅其余的也会改变。 捕获的应用程序

主要活动

private lateinit var api: MijiaBluetoothApi val POS_BATTERY: Int = 0x22 val COUNT_BATTERY: Int = 1 val POS_SERIAL: Int = 0x10 val COUNT_SERIAL: Int = 7 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var bateryText: TextView = findViewById(R.id.bateryText) as TextView var mileageText: TextView = findViewById(R.id.mileageText) as TextView var serialText: TextView = findViewById(R.id.serialText) as TextView var memuryDumpText: TextView = findViewById(R.id.memuryDumpText) as TextView RxPermissions(this) .request(Manifest.permission.ACCESS_COARSE_LOCATION) .filter { it == true } .flatMap { RxBleClient.create(this).scanBleDevices() }.first { // TODO: change me // hardcoded bluetooth device mac address //it.bleDevice.macAddress == "FE:03:4D:46:52:39" it.bleDevice.macAddress == "F6:DE:A2:E1:8E:65" }.flatMap { it.bleDevice.establishConnection(this, false) }.doOnNext { onConnected(it) }.subscribe() findViewById(R.id.send).setOnClickListener { api.dumpMemory().subscribe({ println("Memory dump: ${it!!.toHexString()}") memuryDumpText.text = "Memory dump: ${it!!.toHexString()}" }, Throwable::printStackTrace) api.readValue(0x10, 10).subscribe({ serialText.text = it!!.toString() }, Throwable::printStackTrace) //lee el valor de la bateria, coge el primer byte del array y lo pasa a string. api.readValue(POS_BATTERY, COUNT_BATTERY).subscribe({ var test: String; //test = it!!.data[0].toString() +"%"; bateryText.text = it!!.toString() }, Throwable::printStackTrace) } } private fun onConnected(connection: RxBleConnection) { api = MijiaBluetoothApi(connection) } 

MijiaBluetoothApi.kt

 class MijiaBluetoothApi(val connection: RxBleConnection) { private val parser = ResponseParser() fun readValue(from: Int, count: Int): Observable { val message = ReadRequest(from, listOf(count * 2)) return sendMessage(message.bytes).map { try { parser.parse(it) as ReadResponse } catch (e: Exception) { e.printStackTrace() return@map null } } } fun dumpMemory(): Observable { // dump all values by reading 32 value chunks // one value - 16-bit unsigned int // 8 times x 32 values x 16-bit = 512 bytes return Observable .range(0, 8) .map { it * 32 } .concatMap { readValue(it, 32) } .filter { it != null } .map { it!!.data } .reduce { left: ByteArray?, right: ByteArray? -> if (left == null && right != null) { right } else if (right == null && left != null) { left } else { left!! + right!! } } } private fun sendMessage(bytes: ByteArray): Observable { println("Sending: ${bytes.toHexString()}") val result = connection .setupNotification(UUID.fromString(READ_CHARACTERISTIC_UUID)) .flatMap { it } .doOnNext { println("Response part: ${it.toHexString()}") } .timeout(200, TimeUnit.MILLISECONDS) .onErrorResumeNext(Observable.empty()) .collect({ mutableListOf() }) { list, bytes -> list.add(bytes) } .map { list -> val length = list.sumBy { it.size } val array = ByteArray(length) var currentIndex = 0 list.forEach { System.arraycopy(it, 0, array, currentIndex, it.size) currentIndex += it.size } array } .doOnNext { println("Response: ${it.toHexString()}") } connection.writeCharacteristic(UUID.fromString(WRITE_CHARACTERISTIC_UUID), bytes).subscribe() return result }