Dear readers,
The Arduino IDE provides some basic examples for the ESP32 boards.
A BLE_client example can be found within the examples of the ESP32 BLE Arduino.
Within this example the following is done:
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
I have never seen this construction: class::method().
Looking it up in the guidance esp32BLEpdf.
The following is stated:
That is the principle of scanning. In our C++ BLE story, we have modeled this through the C++ class
called BLEScan. We get an instance of this class by asking the BLE device for it using:
BLEScan* pMyScan = BLEDevice::getScan();
The object returned to us is a singleton. This was chosen because we will only want to be scanning
once per ESP32 at any given time.
Okay, so it is some kind of singleton, but I still don't get it.
A singleton is normally not declared like this:
BLEDevice::init("");
Can someone clarify what is done here and why?
Thank you guys in advanced!
Greetings,
Niek
Are you asking about the syntax or the design pattern? Those two have nothing to do with each other.
BLEDevice::init("");
This is a syntax to invoke a public static method of the class.
BLEScan* pMyScan = BLEDevice::getScan()
Again, getScan() is a public static method of class: BLEDevice.
This design pattern is called: factory pattern.
Thank you for your answer. It helped me to understand the code a bit more.
But why use static functions in this case?
Greetings,
Niek
niekbeijloos:
Thank you for your answer. It helped me to understand the code a bit more.
But why use static functions in this case?
Greetings,
Niek
Because that's how the pattern is implemented.
(haven't looked at the class in question....) That appears to me a "factory" method, which is a very popular Java design pattern. You call the factory method, and it RETURNS an instance of the class. This is especially useful where the "factory" wants/needs to keep track of all outstanding instances of its class, so it can do some global operation or maintenance on all of them. Google "java factory pattern".
Regards,
Ray L.