WHAT I HAVE

I have a hardware sensor and an HC-05 connected to Arduino. Arduino collects sensor data and transmits it in realtime over Bluetooth HC 05 to an Android device for analysis.

Each frame of sensor data is 44 Bytes.

Data transfer rate on average is 2200 Bytes per second.

There is a custom Android app that handles the sensor data.


THE ISSUE

Once the Android app is connected to hardware data stream starts flowing at normal rate, until, Android app send a single command from app to Arduino. After that, Every few hundred frames a big lag of 450-950ms is observed before further data is received.

note: This command does not affect the function mentioned above in any way. There are other commands sent time to time from Android phone to Arduino.


OTHER OBSERVATIONS

  1. From Arduino, data appears to be going at normal rate, no lag observed.

  2. If Android app does not send any command, no lag has been observed for as long as the analysis is performed(around 12 mins).

  3. No frame has been missed/dropped/corrupted during either of the scenarios(with or without lag). i.e. all data is transmitted and received.

can anyone help me understand why this huge lag is generated?

If any information is required please comment and i will be happy to provide.

P.S. The Android side code is standard text book stuff, once connected, there are two Threads operating, one keeps polling(reading) data from InputStream and the other is for writing. Both of the threads are used for only one purpose( the writer thread is hardly engaged as one might expect).

Relevant code :

Once connected, thread that reads from InputStream

inner class ReaderThread : Thread("CBT_reader") {
    private var bytesRead: Int = 0
    private var buffer = ByteArray(2)
    private lateinit var trimmedBuffer: ByteArray

    override fun run() {
        try {

            while (true) {
                bytesRead = mmInStream.read(buffer)

                trimmedBuffer = ByteArray(bytesRead)
                System.arraycopy(buffer, 0, trimmedBuffer, 0, bytesRead)
                observer.notify(trimmedBuffer)
            }

        } catch (e: IOException) {
            logger.logException(e)
            logger.logw(e.message ?: "Read error")
            tryDisconnect()

        } catch (e2: InterruptedException) {
            logger.logd("Reader has been interrupted")

        }


    }

    fun abort() {
        interrupt()
        mmOutStream.close()
        mmInStream.close()
        btSocket.close()
    }
}

Writer Thread,

inner class WriterThread : Thread() {
    private val queue = LinkedBlockingQueue<ByteArray>()


    fun putInQueue(b: ByteArray) {
        queue.put(b)
    }

    override fun run() {
        while (true) {
            try {
                mmOutStream.write(queue.take())
                logger.logd("Data frame written")

            } catch (e: IOException) {
                //write failed
                logger.logException(e)
                logger.logw(e.message ?: "Write error")
                tryDisconnect()
                break

            } catch (e2: InterruptedException) {
                logger.logd("Writer has been interrupted")
                break
            }
        }
    }

    fun abort() {
        interrupt()

    }
}
0

There are 0 answers