I've used setLocalName in the past and it works just fine but I've only ever used it with double quotation marks around whatever name I was advertising. I recently changed to a variable that I pass in. When I try to compile I get this:
exit status 1
no matching function for call to 'BLELocalDevice::setLocalName(arduino::String&)'
Please tell me I am doing something wrong and that you can pass in a String() type variable. Right now it looks like you can only pass in a array of characters declared as a constant (can't be updated - not what I want!), even if I declare the variable as a String.
fluffymarshmallow26:
you can only pass in a array of characters declared as a constant (can't be updated - not what I want!)
Hi @fluffymarshmallow26. I think you're misunderstanding how const works. It's true that a variable declared const can't be assigned later, and this does mean that a global variable can never be updated, but you can do things like this well enough:
As you can see, the value of x changes on every loop. This is because x is local to the loop() scope. So it's true that it can't be changed after the assignment, but that's not a problem. And in fact this is a very useful thing because it prevents you from doing things like this accidentally:
if(x = 42) {
when you meant to do this:
if(x == 42) {
As for the function not accepting the String data type, you can convert String to the C string the function takes using c_str():
Or you may find it easier to just use C strings (char array) from the beginning:
Follow-up question related to this thread: When I use the following the service reported in nRF Connect is 0000:
BLE.setAdvertisedServiceUuid(pUUID.c_str());
UUID is declared as String initially with its initialiser set to {}. I populate it later. When I check it with Serial.println() I see the value I expect (FE9C) but that is not what nRF Connect shows.
My question: Is there something weird going on conversion-wise with setAdvertisedServiceUuid()?
Wow - this seemed unnecessarily complicated. I used the c_str() converter but it always produced "0000". What's strange is setLocalName() worked with the c_str() convert attached to its variable. Anyway, here's what I did to make it operational:
String pUUID = {};
char myUUID[5] = {};
pUUID = "fe9c"; // actually, I pass in a substring of a long String value
pUUID.toUpperCase(); // I guess this converts the variable in-place???
pUUID.toCharArray(myUUID, 5); // thank you Delta_G, topic=351180.0, Oct 2015
BLE.setAdvertisedServiceUuid(myUUID); // no need for c_str conversion, already char type