1

I am reading data from bluetooth gatt characteristic. First data is one byte and I am successfully reading it by code:

val strValue = characteristic.value[0].toUByte()

characteristic.value[1] contains most significant byte of uint16

characteristic.value[2] contains least significant byte of uint16

What I want to do is get uint16 and put it into strValue.

I've tried to use shl function but it brings me this error: IMAGE1

I also tried this: IMAGE2

How to proper do this in Kotlin? I am good in C but Kotlin is new for me.

NIXIE_123
  • 11
  • 2
  • 1
    Insert a `toInt()` call right before the `shl` call. Use `or` to combine the numbers. And a uint16 is a `UShort` in Kotlin. `val strValue = (characteristic.value[1].toInt().shl(8) or characteristic.value[2].toInt()).toUShort()` – Tenfour04 Jan 11 '23 at 18:17
  • [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – gidds Jan 11 '23 at 20:44
  • It almost works. There is one problem: 123 124 125 126 127 65408 65409 65410 – NIXIE_123 Jan 19 '23 at 14:56

1 Answers1

0

Solution:

val strValue :UShort = (characteristic.getIntValue(FORMAT_UINT16,1) or characteristic.getIntValue(FORMAT_UINT16,2).shl(8)).toUShort()
NIXIE_123
  • 11
  • 2