Open mrvanes opened 7 years ago
@mrvanes thanks for submitting this.
Just curious, why are you using a char
array instead of a unsigned char
or byte
array?
I'm really not a C expert so I just use char as a shortcut for 1-byte values and it had the least casting work-arounds.
@mrvanes what if we add another setValue
method:
virtual bool setValue(const unsigned char value[], unsigned char length);
virtual bool setValue(const char value[], unsigned char length); // <-- new
virtual bool setValue(const char* value);
I must admit that I don't see the difference between char value[] and char* value. Both result in a pointer to an array if I'm not mistaken (please correct me if I'm wrong).
Anyway, the problems started when I did something like this:
char value[] = { 1, 2, 0, 0, 3, 4, 5 };
BLEFixedLengthCharacteristic MyChar = BLEFixedLengthCharacteristic(const char* uuid, unsigned char properties, const char* value);
Assuming I could construct AND initialise the Characteristic to the initial value I made (which fails, due to the 0's in value, the Characteristic will have length 2 and value { 1, 2 }).
The solution was to construct using
BLEFixedLengthCharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize);
And set the initial value using
setValue(const unsigned char value[], unsigned char length);
Having your extra setValue prototype doesn't help IMHO, because the assumption of constructing + initialisation in one command might still happen to newcomers?
Not sure this is your problem because I'm not quite sure what/how you're using the values in your array, but I think you're mistaking how you use chars. When you're putting in zeroes like that you're actually putting in the ASCII value of zero and not the char '0'. The problem is an ASCII value of zero means 'null' which signifies the end of a string, thus the string you're passing to the function is only two characters long.
Again though, maybe you know that and I'm misunderstanding the issue.
If I'm not however, simply try putting your values in single-quotes and then you'll be passing in the ASCII value (i.e. the char value) for '0' instead of the literal value 0 which again means null.
char value[] = { '1', '2', '0', '0', '3', '4', '5' };
Yes, my problem was completely caused by my "abuse" of an array of char, assuming I could use any char and rely on passing that to the constuctor. I did need 0x00's, just never realised I'd terminate the string and would fail initialising the characteristic. So the main point I try to raise in this issue is, be careful offering an array of chars as constructor, people (like me) may mistake it for an array of meaningless bytes...
Attempt at clarifying the BLECharacteristic initialisation when using non-string values containing 0's.