I'm using the Arduino IDE to program an Adafruit Feather Sense nRF52840 chip. I'm porting code works on an Arduino Nano and an ESP32. This project involves sending MCU data to a smartphone app via BLE GATT.
The problem is I the BLE Characteristic update won't work for a 'string' variable type.
I believe the issue is because I'm using std::string as a variable type but the Bluefruit.h library (Adafruits BLE library) doesn't seem to support this. The compiler returns the following error:
no matching function for call to 'BLECharacteristic::notify(std::string&)'
By using std::string all the data can be compressed into 1 BLE packet (18 bytes in total). This is important for the project. Here is a simplified version of the function to do this and do a BLE update:
void setAdvData() {
std::string strServiceData = "";
strServiceData += (char)(transmission_num & 0xff); // Lower byte of transmission number
strServiceData += (char)((transmission_num >> 8) & 0xff); // Upper byte of transmission number
strServiceData += (char)(temp & 0xff); // Lower byte of temperature
strServiceData += (char)((temp >> 8) & 0xff); // Upper byte of temperature
strServiceData += (char)(humid & 0xff); // Lower byte of Humidity
strServiceData += (char)((humid >> 8) & 0xff); // Upper byte of Humidity
myCharacteristic.notify(strServiceData); // Set characteristic message, and notify client
}
With this method each measurement only and always takes up 2 bytes, a lower byte and upper byte.
I have tried changing the variable type to 'String' (capital S) and to a 'char' array but these return similiar compilation errors:
no matching function for call to 'BLECharacteristic::notify(String&)'
I've also tried converting the infomarion using '.c_str()' but this results in a variable length greater than the size of 1 BLE packet, and corrupt data/strange behavior on the BLE central (smartphone). Also looked into understanding the differences between std::string, String, and char * but there are many conflicting answers on the Arduino forums and nothing yet leading to a solution
Any help or ideas would be great, thanks
PS: I have already asked this on the Adafruit Forums. There is a smaller community there, and time is a factor so I have posted this here too.