StngBo
December 31, 2023, 1:49pm
1
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
StngBo
January 1, 2024, 1:14pm
2
Apologies,
I found the solution here I think:
The problem is that the BleCharacteristic callback is a BleOnDataReceivedCallback. Unlike some other classes, this unfortunately can’t take a C++ class instance directly so you need to implement it with two member functions, one static and one not.
// In BLEHandler class definition
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);
// Characteristic defnit…
1 Like
StngBo
January 1, 2024, 5:37pm
3
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);
?
StngBo
January 1, 2024, 6:33pm
4
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
system
Closed
February 1, 2024, 4:33am
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.