Making a BLE singleton

Hi All,

I am currently making a singleton class using the application note generator to house the Bluetooth code in my application.

I am having difficulty knowing where to place the following code:

const BleUuid serviceUuid("<my service id>");
BleCharacteristic emailCharacteristic("Email", BleCharacteristicProperty::WRITE, BleUuid("3b2c9a89-cd44-453f-a76e-0e5ef9598e6b"), serviceUuid, &Bluetooth::onDataReceived, NULL);

My onDataReceived function is in my Bluetooth.cpp file (which was generated with the application note generator),

void Bluetooth::onDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer, void *context){
    ...
}

Where should I place the BleCharacteristic emailCharacteristic(...) ?

Is this the same issue when trying to initialise particle functions within a singleton where I need to place &Bluetooth:: before onDataReceived ?

Thanks

Apologies,

I found the solution here I think:

1 Like

I'm pretty sure the above is the solution, but still having an issue.

I have defined the following in my singleton .h file

void onDataReceived(const uint8_t *data, size_t len, const BlePeerDevice &peer);
static void onDataReceivedStatic(const uint8_t* data, size_t len, const BlePeerDevice& peer, void* context);

as per Rick's example.

But where should I define the following characteristic. If defined in the public section in the singleton .h file, then it throws errors. If I define it at the top of my singleton .cpp file then I have to reference the onDataReceivedStatic as Bluetooth::onDataReceivedStatic but then I can no longer use this as the last argument in the BleCharacteristic.

Does anyone know where I am going wrong?

BleCharacteristic emailCharacteristic("Email", BleCharacteristicProperty::WRITE, BleUuid("3b2c9a89-cd44-453f-a76e-0e5ef9598e6b"), serviceUuid, onDataReceived, this);

?

Ok I got there in the end. I defined the BleCharacteristic in my singleton .cpp file as the following:

BleCharacteristic emailCharacteristic("Email", BleCharacteristicProperty::WRITE, BleUuid("3b2c9a89-cd44-453f-a76e-0e5ef9598e6b"), serviceUuid, &bluetoothHandler::onDataReceivedStatic, &bluetoothHandler::instance());

So I handed in &bluetoothHandler::instance() in place of this.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.