1

I am using this example to communicate between arduino and android, and it works well.

The problem is that i would make sure all data has been received before outputting it.

As it is now android will simply output everything when it is being received. I need to collect whatever is received so that i for example can compare it to another string.

Currently it works like this

UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read.
    @Override
    public void onReceivedData(byte[] arg0) {
        String data = null;
        try {
            data = new String(arg0, "UTF-8");
            data.concat("/n");
            tvAppend(textView, data);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
};

And arduino:

void setup()  
 {  
  Serial.begin(9600);       
 }  
 void loop()  
 {  
  char c;
  if(Serial.available())  
  {  
   c = Serial.read();  
   Serial.print(c);    
   }
 }  

Example:

Sent to arduino: TEST

Received from arduino: TEST

However in reality its more like this

Sent to arduino: TEST

Received from arduino: T E ST

So the received "TEST" will come in bytes of one or two which then needs to be gathered into a string.

I assume the reason for this is due to the rate/speed of incoming bytes so that it will just output it as soon as it gets the data.

I would like to make sure that all incoming data is received before I output it, so that I can work with the incoming data.

In theory in need something like while(serial.available())... for android.

Any help is appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JPJens
  • 1,185
  • 1
  • 17
  • 39

0 Answers0