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

Characteristic constructor and setValue use strlen to calculate characteristic length #110

Closed mrvanes closed 7 years ago

mrvanes commented 7 years ago

I have a BLEFixedLengthCharacteristic that has several 0's in the characteristic value which caused me some headache on constructing and updating using the char prototype as the length of the characteristic value gets calculated from strlen instead of sizeof. I now use the constructor giving the length and setValue using the char value[] and length prototype, but it took me some time before I understood what went wrong using the char prototype.

I tried fixing it using sizeof in BLECharacteristic.cpp and it works for the constructor, but setValue char prototype is a no-go, since sizeof char always returns 4 (the pointer) instead of the size of the array and there is no way to get the sizeof the original array since it's calculated compile-time.

I'm not that literate in C to solve this problem, but maybe you have an idea to alleviate this potential brain breaker for future victims? Maybe a solution would be to deprecate the char* prototype and require the length to always be set, this is how the BLE Nano BLE API works IIRC.

sandeepmistry commented 7 years ago

@mrvanes that's why there's 2 constructors, one is for "C strings" the other for "byte buffers". If you think something is unclear in the API documentation, please submit a pull request with a suggestion.

https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/API.md

Here's what's in there now:

BLECharacteristic

Contructor

BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize);

BLECharacteristic(const char* uuid, unsigned char properties, const char* value);
uuid - UUID of characteristic
properties - combination of (|'ed):
BLEBroadcast
BLERead
BLEWriteWithoutResponse
BLEWrite
BLENotify
BLEIndicate
Choice of:

valueSize - size of characteristic in bytes (max: 20 characters on nRF8001 and nRF51822)
or

value - string value (max: 20 characters on nRF8001 and nRF51822)