Only 6 values are fitting into one line over BLE

I have been working on a tool that is reading certain inputs and is transferring data from Arduino to Android phone over BLE (Bluetooth). For some reason only 6 values are fitting into one line, then 7th value is always coming separately on the next line. Why so? Is there some limitation in terms of string length over BLE? Should I send this in some other format and then how?

#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>

static const int RXPin = 2, TXPin = 3;
SoftwareSerial ss(RXPin, TXPin);

void setup() 
{
  Serial.begin(115200);  

  sensors.begin(); 
  ss.begin(115200);
}
 
void loop()
{  
  int value0 = analogRead(A0);
  int value1 = analogRead(A1);
  int value2 = analogRead(A2);
  int value3 = analogRead(A3);
  int value4 = analogRead(A4);
  int value5 = analogRead(A5);

  int value6 = round(gps.speed.kmph());
  
  Serial.print(value0); 
  Serial.print(",");  
  Serial.print(value1);
  Serial.print(",");   
  Serial.print(value2);
  Serial.print(",");   
  Serial.print(value3);
  Serial.print(",");  
  Serial.print(value4);
  Serial.print(",");  
  Serial.print(value5);
  Serial.print(",");  
  Serial.print(value6);
}

Last value always comes on a separate line after previous 6.

not sure, but it looks like there could be some delay between prints that (such as starting to send the buffered data) before the final print.

why not create a single string and execute a single print.

    char s[100];
    sprint (s, "%d,%d,%d,%d,%d,%d,%d",
        value0, value1, value2, value3, value4, value5, value6);
    Serial.print (s);
1 Like

I think I have tried sometimes sprint, but I am constantly getting exceptions while recording sketch to Arduino.

Any ideas how to fix it?

   ^~~~~~
   isprint
exit status 1
'sprint' was not declared in this scope

I have tried to include #include <Stdio.h>, but it is not found either. Nor I can find this package from Library manager...

Ok it seems to be chnaged... should be: sprintf
I will test

sprintf, not sprint (autocorrect probably).

thanks

This is strange...

char s[100];
sprintf (s, "%d,%d,%d,%d,%d,%d,%d",
    value0, value1, value2, value3, value4, value5, value6);
Serial.print (s);

I am getting as an output in my Android device while debugging following string "530,983,62,128,0,26,"
As you can see there is a comma, but no 7th value even tho it is there if I check from my PC Arduino Serial Monitor

That's 20 characters. Is that a BLE packet limit?

Try sending "1,2,3,4,5,6,7"

Yes, your suggested number of ints goes fine through the Bluetooth. I found this:

Characteristics have a maximum length of 20 bytes. Since 16 bit and 8-bit data types are easy to pass around in C++.

It is worth noting that Bluetooth Low Energy has a maximum data packet size of 20
bytes, with a 1 Mbit/s speed.

Is that the case? Then how do I send more than 7 values over Bluetooth? :thinking:

Apparently so, I found the same information on Stack Exchange. You could fit the binary integer values into a packet (two bytes each) as long as your Android program knows how to deal with them. Or you need to figure out how to process two packets on the Android.

Send two packets, with identifiers 1 or 2, plus four values each.

Any code example how to send data in two packets with identifiers? I was just happy to get the connection working successfully, I am not that advanced yet. I have tried to search on google some example, but could not find anything suitable so far.

You have the code required to transmit. Just put a "1" or "2" in place of the first data item and populate the remaining data items accordingly.

The receiver needs to sort out the two packets.

You mean like this?

#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>

static const int RXPin = 2, TXPin = 3;
SoftwareSerial ss(RXPin, TXPin);

void setup() 
{
  Serial.begin(115200);  

  sensors.begin(); 
  ss.begin(115200);
}
 
void loop()
{  
  int value0 = analogRead(A0);
  int value1 = analogRead(A1);
  int value2 = analogRead(A2);
  int value3 = analogRead(A3);
  int value4 = analogRead(A4);
  int value5 = analogRead(A5);

  int value6 = round(gps.speed.kmph());
  
  char s[100];
  sprintf(s, "1,%d,%d,%d,%d,2,%d,%d,%d", value0, value1, value2, value3, value4, value5, value6);
  Serial.print (s);
}

However I think if I do this way I will get something like 1,%d,%d,%d,%d,2,%d, and then separately %d,%d . Probably some line break is needed or?

What is receiving this data?

Xamarin Forms Android app, written on C#. I have there logic for reading incoming string, something like:

  string parsedString = args.Characteristic.StringValue;

and string pattern validation, to verify that data is in correct format before parsing.

Then do as @jremington said in post #13 and deal with it in C#.

I read somewhere while I was looking for the max BLE packet size that you should have some elapsed time between packets. No idea if that's accurate.

No, you are posting here because that doesn't work.

Figure out how to send TWO packets, one that begins with "1" and the other that begins with "2". You are limited to 20 bytes per packet.

You forgot to explain the details of how the data are received, so we can't help with that.

I just have wired HM-19 and it seems to be working with >20 bytes. However it is operating on 9600 baud rate and it seems like it is not possible to change it. I would prefer as fast data transfer as possible. What would you say, is it better to use old Bluetooth 4.0 with 115200 and sending data in portions as you suggested or use HM-19 on 9600 with data >20 bytes in one "string"?

The HM-19 data sheet disagrees. Use the appropriate AT command to set the Baud rate.
http://www.dsdtech-global.com/2018/06/dsdtech-hm-19.html