sandeepmistry / arduino-BLEPeripheral

An Arduino library for creating custom BLE peripherals with Nordic Semiconductor's nRF8001 or nR51822.
MIT License
462 stars 179 forks source link

Update API.md #112

Open mrvanes opened 7 years ago

mrvanes commented 7 years ago

Attempt at clarifying the BLECharacteristic initialisation when using non-string values containing 0's.

sandeepmistry commented 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?

mrvanes commented 7 years ago

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.

sandeepmistry commented 7 years ago

@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);
mrvanes commented 7 years ago

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?

MarqueIV commented 7 years ago

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' };

mrvanes commented 7 years ago

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...