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

How to use set value #238

Open rahulkhatri98 opened 6 years ago

rahulkhatri98 commented 6 years ago

Hello i am using this BLE library.Now my problem is i am unable to use set value for longer bytes

Ex :+1: int sensor_id = 0x44332211;

BLEUnsignedLongCharacteristic sensorid = BLEUnsignedLongCharacteristic("27763B11-999C-4D6A-9FC4-C7272BE10900", BLERead | BLEWrite); sensorid.setValue(sensor_id); Till this ok

Now i want to write like 0x03069756724645286248258257 using setValue.How can i do ??

I had tried like uint8_t midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00}; BLECharCharacteristic Reserve_storage5 =BLECharCharacteristic("27763B2B-999C-4D6A-9FC4-C7272BE10900",BLERead | BLEWrite);

Reserve_storage5.setValue(midiData,5);

but not working

i need Help

TamojitSaha commented 5 years ago

@rahulkhatri98

I had the same issue. I had to change the base class from BLECharCharacteristic to BLECharacteristic. BLECharacteristic has the .setValue() function to pass char array as parameter. See the following: https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/161a4163f565be3cd5b62bbc59f0c2b522d82b02/src/BLECharacteristic.cpp#L73-L85

Give this a try:

const unsigned char midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00};
BLECharCharacteristic Reserve_storage5 =BLECharCharacteristic("27763B2B-999C-4D6A-9FC4-C7272BE10900",BLERead | BLEWrite);

Reserve_storage5.setValue(midiData, sizeof(midiData)/sizeof *(midiData));
melvyniandrag commented 5 years ago

Just glanced at this issue.

The base function call doesn't work for me either, not sure why. I had to add a function

BLETypedCharacterisitc<T>::setValue( const unsigned char* arr, unsigned char len) {
    this->BLECharacteristic::setValue( arr, len );
}

in BLETypedCharacteristic.h . . . not sure why this is because the inheritance is:

BLECharacteristic -> BLEFixedLengthCharacteristic -> BLETypedCharacteristic -> BLECharCharacteristic

... looking into why the derived class doesnt see the method.

But after implementing this the BLECharCharacteristic only shows one byte.

That is because BLECharacteristic::setValue() memcpy()s based on min(length, this->_valueSize) and _valueSize is set in the constructor call

template<typename T> BLETypedCharacteristic<T>::BLETypedCharacteristic(const char* uuid, unsigned char properties) :
  BLEFixedLengthCharacteristic(uuid, properties, sizeof(T))

to be the size of the type T, which in the case of BLECharCharacteristic is 1.

So this needs a bit of thought and testing I think before implementing.

melvyniandrag commented 5 years ago

OK just banged together something that seems to work but I have no idea if it's stable.

https://github.com/melvyniandrag/arduino-BLEPeripheral

See examples/longCharacteristic

and

src/BLECharArrayCharacteristic.*

from commit 6ad9f8d . It working now, but I'm going to bang on it some more, no guarantees it will keep working.