• A wrapper around the Bluetooth Low Energy protocol for the ESP32 microcontroller.
• Originally it was part of another project, so if previous versions are necessary, you may visit its git history on the other directory.
- setDeviceName
- add_characteristic
- use_characteristic
- begin
- receivedDataAsIntArray
- receivedDataAsDoubleArray
- receivedDataAsFloatArray
- receivedDataAsString
- sendDataArray
- sendDataPoint
- releaseMemoryToSystem
void setDeviceName(std::string name);
• It takes an std::string
and sets it as the name which the bluetooth device will be discoverable by other devices.
void add_characteristic(String characteristic_name, const char* uuid);
• It takes the characteristic name (used for future reference) as a String
and the UUID as a const char*
.
Both are stored in a map structure with the characteristic_name as the key and the uuid as the value.
void use_characteristic(String characteristic_name);
• Changes the working UUID to the one referenced by the String
key characteristic_name
passed as
referenced.
void begin();
• It sets up and initializes the whole Bluetooth low energy server and characteristic callbacks with the following UUIDs:
data type | variable | value |
---|---|---|
const char* |
SERVICE_UUID | ab0828b1-198e-4351-b779-901fa0e0371e |
int* receivedDataAsIntArray();
• Retrieves the last data passed by the other device as a pointer to an int array.
• If there is no data to be processed it will return a NULL
pointer*.
• It is highly recommended that you check rather the response was a null pointer before doing any data processing, to avoid errors and exceptions.
float* receivedDataAsDoubleArray();
• Retrieves the last data passed by the other device as a pointer to an array of doubles.
• If there is no data to be processed it will return a NULL
pointer*.
• It is highly recommended that you check rather the response was a null pointer before doing any data processing, to avoid errors and exceptions.
float* receivedDataAsFloatArray();
• Retrieves the last data passed by the other device as a pointer to a float array.
• If there is no data to be processed it will return a NULL
pointer*.
• It is highly recommended that you check rather the response was a null pointer before doing any data processing, to avoid errors and exceptions.
String receivedDataAsString();
• Retrieves the last data passed by the other device as a String
.
template <typename T>
void sendDataArray(T data[], int array_size){...}
• A template method that converts an array of any data type into a CSV formatted c string(char*
) and sends it via BLE.
template <typename T>
void sendDataPoint(T data_point){...}
• A template method that converts a value of any data type into a c string(char*
) and sends it via BLE.
template <typename T>
void static releaseMemoryToSystem(T* data){...}
• A template method that takes a pointer to an array that was allocated dynamically, and releases the memory back to the system.
• The only public variable is a custom datatype(struct) used to store all the information related to the received data, that will be used to parse and convert it.
// Create struct that will hold all essential infromation about the parsed data
struct data_information{
int array_size;
std::string* data_array;
};
•The only private method is the parsing algorithm. It takes the incoming std::string
data in a CSV format and splits the data points into an array of std::strings
. It return the struct containing the size of the array and the array itself.
•Since this method is private it cannot be accessed by the end user, and it's used inside other methods (e.g receiveDataAsIntArray)
BLE::data_information parseData(std::string data);
• A static member is a variable or a method that can be accessed without having the need to instantiate the class nor use an object. Furthermore, it is common to all instances of the class (i.e They share the same variable).
• bool
variable that is true
when there is a device connected via BLE, or false
when there is no device.
// If there is a device connected
if (Bluetooth::BLE::deviceConnected){...}
// If there is no device...
else{...}
• bool
variable that is true
when there is an unread message and false
otherwise.
// If there is a new message
if (Bluetooth::BLE::new_message){...}
// If there is no new messages...
else{...}
// Include the .h file
#inlude "BLE.h"
// instantiate an object
Bluetooth::BLE bluetooth;
void setUp(){
// initialize the serial monitor
Serial.begin(115200);
// Add the characteristics that will be used
bluetooth.setDeviceName("Pedro's RunPod");
bluetooth.add_characteristic("Angle", "fbed8ddc-109f-11eb-adc1-0242ac120002");
bluetooth.add_characteristic("Time", "16dc549c-10a1-11eb-adc1-0242ac120002");
// Initialize the BLE
bluetooth.begin();
}
void loop(){
delay(1000);
//If a device connected to the ESP32, begin the processes, otherwise keep scanning.
if(Bluetooth::BLE::deviceConnected )
{
log("Device connected!!");
// Counter that send every odd number to the Angle characteristic and every even to the Time characteristic
int i = 0;
while(true){
i++;
if(i%2 == 0){
bluetooth.use_characteristic("Angle"); // Change to the specific characteristic
}
else{
bluetooth.use_characteristic("Time"); // Change to the specific characteristic
}
// Send the value of i and notify
bluetooth.sendDataPoint(i);
delay(500);
}
}
}