read Bluetooth inputstream in kotlin - First character is missing

Viewed 209

I am a newbie in using Bluetooth. Actually I am trying some app for two way communication (sending/receiving strings between app and Computer terminal), but I am stuck. All parts works well, but in reading data from inputstream is first symbol/character missing. For example: SEND: "HELLO" is RECEIVED: "ELLO".

I am using the example of here .

I´ve been searching the solution for two days, but without any progress :/

my code :

 inner class BTConnected(BTSCT:BluetoothSocket): Thread(){
        private val BTINstream:InputStream = BTSCT.inputStream
        private val BTOUTstream:OutputStream = BTSCT.outputStream
        private var BTbuffer = ByteArray(1024)

        override fun run(){
            var Bytes:Int = 0

            while (true) {
                try{
                Bytes = BTINstream.read(BTbuffer)
            }
                catch (IOE:IOException){
                    break
                }
                val readMsg =  myHandler.obtainMessage(
                    MESSAGE_READ, Bytes, -1,BTbuffer)
                readMsg.sendToTarget()
            }
    }

I think my Handler is OK

val myHandler = object:  Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when(msg.what){
                MESSAGE_READ -> {
                    val readBuff = msg.obj as ByteArray
                    val tempMsg = String(readBuff, 0, msg.arg1)
                    PrintMessage(tempMsg)
                }
...rest of code

the PrintMessage() function is for TOAST message. Many thanks for any advices!

0 Answers
Related