Trying to understand data transfer behaviour (lag) on a BlueTooth connection using an HC-05 board (connected...












0















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()

}
}









share|improve this question

























  • you need to share the code from android app

    – NIKHIL MAURYA
    Nov 21 '18 at 9:20











  • @NIKHILMAURYA Relevant code added.

    – Ankit
    Nov 21 '18 at 9:31
















0















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()

}
}









share|improve this question

























  • you need to share the code from android app

    – NIKHIL MAURYA
    Nov 21 '18 at 9:20











  • @NIKHILMAURYA Relevant code added.

    – Ankit
    Nov 21 '18 at 9:31














0












0








0








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()

}
}









share|improve this question
















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()

}
}






android android-bluetooth hc-05






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:49









Fantômas

32.8k156490




32.8k156490










asked Nov 21 '18 at 9:16









AnkitAnkit

18612




18612













  • you need to share the code from android app

    – NIKHIL MAURYA
    Nov 21 '18 at 9:20











  • @NIKHILMAURYA Relevant code added.

    – Ankit
    Nov 21 '18 at 9:31



















  • you need to share the code from android app

    – NIKHIL MAURYA
    Nov 21 '18 at 9:20











  • @NIKHILMAURYA Relevant code added.

    – Ankit
    Nov 21 '18 at 9:31

















you need to share the code from android app

– NIKHIL MAURYA
Nov 21 '18 at 9:20





you need to share the code from android app

– NIKHIL MAURYA
Nov 21 '18 at 9:20













@NIKHILMAURYA Relevant code added.

– Ankit
Nov 21 '18 at 9:31





@NIKHILMAURYA Relevant code added.

– Ankit
Nov 21 '18 at 9:31












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408680%2ftrying-to-understand-data-transfer-behaviour-lag-on-a-bluetooth-connection-usi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408680%2ftrying-to-understand-data-transfer-behaviour-lag-on-a-bluetooth-connection-usi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word