I am trying to communicate between Arduino UNO and my android smartphone using USB Host API of android. So far everything is good. I am sending a test string "Try data" from my Arduino through the function Serial.print() every second. Baud rate has been set to 9600 for both the devices. Android smartphone is acting as Host.
Some tutorials I used are :
http://android.serverbox.ch/?p=549
http://android-er.blogspot.in/2014/09/send-data-from-android-to-arduino-uno.html
along with the android usb host documentation. I have tried physicaloid and another library by mik3y but they are not working. Device doesn't open in both the cases.
I am using a thread over which I receive data from Arduino(all code below is in onCreate()
val reader = object : Thread(){
override fun run() {
var buf = ByteArray(endpointIn.maxPacketSize)
connection.bulkTransfer(endpointIn, buf, buf.size, 0)
tv.text = String(buf)//tv is the id of a textview
}
}
reader.start()
The problem is that only letter T from the test string is being received. If instead, I move the declaration of buf out of the thread class, only 4 characters are being read. I am unable to understand why only limited characters are being read. If I instead put the last 2 lines of code of the run method in an infinite while loop, the app crashes (not able to figure out why). I know that threads appear to be running simultaneously but actually its the main thread running for some time and then the reader, both of them don't run at the same time. Its the transition which is very fast and thus, they appear as if they appear to be running simultaneously. Can this be the reason why some information is being lost?
Please help in writing the code for reader thread or is there any better way to do that?
Thanks in advance