BLE local device name string gymnastics

I am cross-posting this here and at the MIT App Inventor Community discussion forum because the question concerns both. But I think the issue will be solved at the Arduino end, not in the Android app.

I have an Android phone app, built using the MIT App Inventor 2 tool, that I use to control a host of arduino nano 33 BLE boards which are running in peripheral mode. I'm happy to post both AI2 code and arduino sketch code, but I think I have a better chance of getting help if I zoom in on the relevant bits. My AI2 code scans for and displays in a listView all the arduino boards that are advertising the right service and characteristic UUIDS. I ignore the physical address and rssi info and put it in the listView using this section of code:

Screenshot from 2021-04-06 16-48-311814×266 35.4 KB

Everything works fine if, on the arduino side, my sketch contains something like this:

(String) nameVal = (String) "myName";

BLE.setLocalName(nameVal.c_str());

In other words, if the arduino sketch is as above then the listView on my phone will contain a row showing "myName" as an available device.
If, on the other hand, my arduino code creates the variable nameVal programmatically then the phone will show either nothing at all or some gibberish wingding character. For example, this seemingly reasonable code on the arduino will lead the phone to show nothing at all in the listView (even though the empty-seeming row remains clickable and allows me to connect to the arduino).

(String) nameVal = (String) "myName";


String myName() {

return (String)nameVal;
}

BLE.setLocalName(myName().c_str());

This is just the simplest example I could concoct demonstrating a point I dont' understand. But what I'd actually like to do is create the device name that the arduino broadcasts programmatically, using string concatenation so that the name includes state information that allows me to decide whether I want to connect. But I'm obviously missing something basic.
Can anyone help? If anyone cares, I'm attaching both arduino sketch and AI2 code.

Whoops. It wouldn't let me attach the .aia file for the App Inventor code. Here's a link

sketch_for_ai2_help.ino (1.63 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.