Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API.md #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Update API.md #112

wants to merge 2 commits into from

Conversation

mrvanes
Copy link

@mrvanes mrvanes commented Dec 6, 2016

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

Attempt at clarifying the BLECharacteristic initialisation when using non-string values containing 0's.
Correct typo
@sandeepmistry
Copy link
Owner

@mrvanes thanks for submitting this.

Just curious, why are you using a char array instead of a unsigned char or byte array?

@mrvanes
Copy link
Author

mrvanes commented Dec 11, 2016

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
Copy link
Owner

@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
Copy link
Author

mrvanes commented Dec 19, 2016

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
Copy link

MarqueIV commented Dec 23, 2016

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
Copy link
Author

mrvanes commented Dec 24, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants