ArduinoBLE icon indicating copy to clipboard operation
ArduinoBLE copied to clipboard

Sending classes

Open skelstar opened this issue 5 years ago • 2 comments

Trying to send my own object from peripheral to central and from what I can work out, this should work:

peripheral:

class TestClass
{
  public:
    unsigned long id;
};

TestClass t;

void sendData()
{
  testDataChar.writeValue(&t, sizeof(TestClass)); // and update the battery level characteristic

  t.id = t.id + 1;
  Serial.printf("id: %lu\n", t.id);
}

central:

class TestClass
{
  public:
    unsigned long id;
};

void readData()
{
    TestClass t;
    characteristic.readValue(&t, sizeof(TestClass));
    Serial.printf("id: %lu\n", t.id);
}

... but all I'm getting back is id: 0 for each packet sent. Is there something I'm doing wrong? I couldn't find any examples anywhere.

skelstar avatar Dec 30 '19 01:12 skelstar

You're probably doing something wrong in initializing the characteristic.

I am using an undocumented BLETypedCharacteristic, which has a template parameter.

BLETypedCharacteristic<FieldStrength> field_strength_char(
  "c478c8cc-1287-4b01-b503-87399d9d835f",
  BLERead | BLENotify
);

joonazan avatar Feb 05 '20 17:02 joonazan

Hi @skelstar, All looking fine but one I will suggest is that please check parameters characteristic.readValue(&t, sizeof(TestClass)); this function should accept. Hope it helps you

thekunalsaini avatar Mar 30 '20 08:03 thekunalsaini