0

I have a third party Android application which connects to my target using Bluetooth SPP. Application sends JSON data, but number of data bytes sent are not known, also data received at my target is inconsistent (sometimes in chunks), there are no line endings to detect the end of data sent.

I have a thread running in my code to read the data over serial port.

int fd = open(rfcommDevice, O_RDWR | O_NOCTTY | O_SYNC);

void readData(int fd)
{
   while (1)
   {
      nbytes = read(fd, buf, buflen);
      if (nbytes < 0)
      {
         break;
      }
      else if (nbytes > 0)
      {
            showCompleteData();
      }
      else
      {
         close(fd);
         break;
      }
   }
}

Now showCompleteData() first parses the data and if found incomplete it fails. As data is received in chunks, so I am not able to understand how to accumulate the complete data and then call showCompleteData().

Also read() does not return 0, as its a blocking call.

Please suggest.

Daemon
  • 1,575
  • 1
  • 17
  • 37
  • No read is guaranteed to be complete. You have to loop. – user207421 Jun 08 '16 at 11:00
  • @EJP I am looping to read but there has to be some break condition. Currently I am parsing the JSON data and if count of opening and closing braces are same then I call showCompleteData(). But I am looking for some other solution. – Daemon Jun 08 '16 at 11:13

0 Answers0