ArduinoBLE
ArduinoBLE copied to clipboard
Sending classes
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.
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
);
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