I've been having a play with the AT-09 4.0 BLE Bluetooth device. I was a bit concerned that it could only send 20 bytes at a time which means data is lost. I created this to cope with it. It takes the message, no matter how long it is, and cuts it into smaller pieces and reassembles it in my iPhone app. The description is in the code. The LongMessage string should begin with a $ and end with a #. The SendLongMessage function adds an additional # at the end of the string so the iPhone app knows that it's the end of the completed message.
The reason for the post is........ is there a better way of doing it?
void SendLongMessage(String LongMessage){
/*
The AT-09 can only send 20 bytes per message so it gets cut up. I cut the
message into 18 charcter segments long and sends them consectively. The way I configured
the iPhone app is to look for a $ initially and ending in a #. This sigifies the
beginning of the message. The next nessages jest end in a #. The final message ends in ##
which means the message is completed. The app adds the strings / substrings
together and displays the complete message.
*/
softSerial.flush();
LongMessage.trim();
int Len = LongMessage.length(); //Length of the message to send
int OldPos = 0; //Start position of substring
int Leng = 18; //End position of the string
String SubStr;
while (SubStr.indexOf("#") == -1){
Serial.print("\nSubStr- ");
SubStr = LongMessage.substring(OldPos, Leng);
OldPos = OldPos + 18;
Leng = Leng + 18;
if (OldPos + 18 > Len){
Leng = OldPos - Len;
}
String NewStr = SubStr + "#";
softSerial.write(NewStr.c_str());
delay(50);
}
}