Problem with String Append Operator when building a string out of an array of ch

Hello,
Using MeetAndroid library to receive a set of characters sent from Android via Bluetooth, I can’t work it out how to build a string out of all characters with String Append Operator. Say the characters set sent from Android is in this format: A1B2 and I want the whole set to appear in a string so create an array of char to store each character then add each character one by one to the string. There is not a problem with hard coding but in the future the characters set will be much larger than A1B2 so I tried += operator but having no luck. The code is as followed:

#include <MeetAndroid.h>
MeetAndroid meetAndroid;

String stringOne = String("a1b1");
String stringTwo = String("a1"); 

void setup() 
{
  Serial.begin(115200);
  meetAndroid.registerFunction(stringValue, 'A');  
}

void loop() 
{
  meetAndroid.receive(); 
}


void stringValue(byte flag, byte numOfValues)
{

  int length = meetAndroid.stringLength();
  char droidData[length];
  meetAndroid.getString(droidData);

  String str = "";

  // APPEND OPERATOR DOESN’T WORK 
  for (int i=0; i<length+1; i++)
  {
    str += droidData[i];
  }


  /*
   // HARD CODING WORKS
   str = str+droidData[0]+droidData[1]+droidData[2]+droidData[3];
   */

  if (str.equals(stringOne))
  {
    blink_one(); 
  }

  else if (str.equals(stringTwo))
  {
    blink_two(); 
  }
  else 
  {
    ;
  }
  delay(100);
}

Any help with this would be greatly appreciated.
Thanks for your time.

and I want the whole set to appear in a string so create an array of char to store each character then add each character one by one to the string.

If you are going to append the characters to a String, why do you need a char array?

If you are going to append all the chars in a char array to a String, why not make the char array one element larger, and put a NULL in the last spot, and then append the string to the String?

What, exactly, is in droidData? What, exactly, is in str after the loop to append the chars in the char array?

What, exactly, is the problem?

To answer your question, the append operator does not work because there is a String object on one side of it and a char array on the other. I know that getString() takes a char array as its argument. You should be able to pass that right to the String constructor.

meetAndroid.getString(droidData);

String str = String(droidData);

To answer your question, the append operator does not work because there is a String object on one side of it and a char array on the other.

There are multiple overloads of the += operator:

	String & operator += (const String &rhs)	{concat(rhs); return (*this);}
	String & operator += (const char *cstr)		{concat(cstr); return (*this);}
	String & operator += (char c)			{concat(c); return (*this);}
	String & operator += (unsigned char num)		{concat(num); return (*this);}
	String & operator += (int num)			{concat(num); return (*this);}
	String & operator += (unsigned int num)		{concat(num); return (*this);}
	String & operator += (long num)			{concat(num); return (*this);}
	String & operator += (unsigned long num)	{concat(num); return (*this);}

The second one takes a char array. That is not the version that OP is calling, anyway. OP is calling the third version, appending one character at a time.

Why it is failing is not clear, since no proof has been offered that it IS failing. Any sort of evidence that a failure HAS occurred or what that failure mode looks like will make it trivial to solve the problem.

Thanks to PaulS and Blue Eyes, I've just had time to try your advice this weekend and it's working without the char array. And instead, the String just simply does well.

cheers.

And instead, the String just simply does well.

I'm sorry. You misspelled "hasn't bitten me in the ass yet". Fear not. It will. Sooner or later.

The android library was smart enough not to use a String. I fail to see why you can't be equally smart and use the char array you were given. Using strcmp() instead of String::equals() requires so little extra effort.