BLE setValue syntax... please!

I’m developing a BLE project with an ESP32-SE. I need to change from sending byte characteristics to integer characteristics, but there is an error each time I build with an integer syntax to test. The h file is BLECharacteristic.h

For bytes this works: pTxLBATL->setValue(&Data_LBatVLow, 1);

where Data_LBatVLow is an unsigned byte. In the h file the format is shown as setValue(const uint8_t *data, size_t size);

I can get this to compile with ‘2’ after the comma but as soon as I make an array of 2 bytes for the variable Data_LBatVLow there is an error.

For integers I have tried different combinations, looking at the syntax stated in the h file:

setValue(uint16_t data16);

Fails:

pTxINTEGER->setValue((uint16_t) &IntegerToSend);

pTxINTEGER->setValue(uint16_t &IntegerToSend);

pTxINTEGER->setValue(&IntegerToSend);

pTxINTEGER->setValue(IntegerToSend, 2);

pTxINTEGER->setValue(&IntegerToSend, 2);

Start by reading the pinned post re 'How to get the most from the forum'. Then you will now how and what to post to pose your question.

what is the error ? You probably don't need to make it an array.

Typing here without testing, but if you declare the characteristic as a BLECharacteristic with the usual BLE UUID and properties; you can probably just send the integer as bytes using setValue. For example:

BLECharacteristic *pTxINTEGER = pService->createCharacteristic(
  "UUID-HERE",
  BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);

Then to send a 2 byte integer:

uint16_t valueToSend = 1234;
pTxINTEGER->setValue((uint8_t*)&valueToSend, sizeof(valueToSend));
pTxINTEGER->notify();

would that work ?

Don’t answer any question or comment again posted by me.

What you suggest compiles first time, I will test it today thank you.

Regarding the h file, these are the formats set out:

  void setValue(const uint8_t *data, size_t size);

  void setValue(const String &value);

  void setValue(uint16_t data16);

  void setValue(uint32_t data32);

  void setValue(int data32);

  void setValue(float data32);

  void setValue(double data64);

When I found errors when trying to send over 8 characteristics, I decided to multiplex the data and use one characteristic only.

Maybe someone who has worked through this error will post. The method which compiles is different to what the created formats for >8 bits are.

The MIT App project receives an open and a closed bracket when the 1234 is sent. I tried changing that number, still the same result.

I set up code to check for bytes and for integers, only the IntegersReceived responds (as I wanted), but not with a number. The received integers go straight into a text label OK, but sending to a slider shows the error

The operation ThumbPosition cannot accept the arguments: ‘ [()]

Which looks like a pointer error to me.

My ESP32 code:

IntegerToSend = 1234; 

Serial.println(F("here"));

pTxINTEGER->setValue((uint8_t*)&IntegerToSend, sizeof(IntegerToSend)); 

pTxINTEGER->notify();

it's sent in binary - so two bytes 1234 is 0x04D2 so on the receiving end you should get two bytes as part of the payload. The transmission is not an ASCII text.

(its better to post the full code or a minimum code demonstrating what you are doing when you post - that was @sonofcy's ask - as very often the issue is not where you think it is and might be elsewhere in the code).

If it sends bytes, App Inventor does not receives them. But immediately fires the IntegersReceived event. Same response/error no matter what I send. I also checked to see if it receives it as a string but no.

I think in this case the h file is misleading.

Do you think the code that compiles is incorrect for the data? Does it need to be converted to a different form?

AppInventor might be expecting ASCII text ? snprintf() the value in a char buffer and send a string to see if it works?

I don’t want to use text strings for this.

I will wait to see what other responses there are.

The MIT App project receives an open and a closed bracket when the 1234 is sent.

What do you see when you use nrfConnect or LightBlue instead of Your custom App?

setValue((uint8_t*)&IntegerToSend, sizeof(IntegerToSend));

This looks like correct syntax, but what is the type declaration of IntegerToSend?

can you show the code of your MIT app ?

it is uint16_t

That’s overwritten now, and I’m using bytes without problem as I was before.

At the time I gave up on integers I had replaced the bytes reception with integers reception. But that code was never executed.

With nRF Connect I didn’t check the integers sending.

The basic idea if anyone else is struggling, is to send an ID/index byte with each data byte. Flag them when received and process when both have been received. More overheads so I’m not totally happy about it.

Glad it’s working for you but I don’t understand what you are suggesting.

With no full code for both side, I don’t see how this thread will help anyone.