BLE local device name automated in Sketch

So it correctly reads the BLE device name. Unfortunately, the app does not send the values 5 and 9 to arduino, or arduino does not receive them. Leave a sketch in arduino what is now. Install the app that worked for you and in the terminal see if the name changes after clicking the toggle button.

I haven't changed the arduino code, only the phone code. And the old phone code continues to work, in the sense that I can toggle the state on the arduino by using the toggle button on the phone. Only sub-optimal feature of old phone code is that the appearance of the toggle button and connected to: label are not reactive to the arduino state. Here are some pictures of the old phone code's operation to make sure I'm conveying this clearly:

default appearance of app upon launch:

appearance after pressing scan:

appearance after selecting an ON device:

appearance after connecting to ON device:

Note that the toggle button is still red and incorrectly says that the pin state is LOW because there's not yet any working logic to make the button appearance reflect the arduino's characteristic value (which can be inferred from the local device name)

appearance after pressing red toggle button

Note that because the arduino is already in an ON state, this first button press doesn't do anything to the arduino - it remains in an ON state

appearance after pressing the toggle button again

Note that the arduino is now in an OFF state and is consistent with the toggle button appearance (although the status label is also non-reactive and therefore still reads (incorrectly) that the phone is connected to an ON device even though the current, advertised name of the arduino is Dev1_OFF).

Does this give you the info you were looking for? I wasn't quite sure what you wanted so I thought I'd try to be explicit about the behavior.

I want you to load the old blocks and see the condition of the arduino in the terminal. You wrote that in the older application the toggle button correctly sent 5 and 9 to arduino and arduino correctly changed its name from Dev1_OFF to Dev1_ON and vice versa. I want to see it in the terminal if the toggle button actually worked correctly with arduino and if the arduino responded to it.

Yes, I know that the arduino immediately reacts to a push of the toggle button on the phone because I'm printing out the local name to the serial monitor every second and I see it change immediately.

Has it reacted on old app inventor blocks, does not react to new ones?

These are old blocks from the first post.

These are new blocks.

The only significant difference is the values. So why do you think the first block works and the second doesn't. In my opinion it is not possible that the new block does not change the state of arduino, if the old block does.

Unless we have communication problems and misunderstood something.

It's best to paste the arduino code and aia file here, which correctly changes the arduino ON / OFF state.

Hi Patryk. When I wrote "old code" I didn't mean the oldest, I meant the attached code which incorporates some of your and / or Chris' earlier suggestions. It does, indeed, use the "new blocks" you attached above and it works as shown in all the phone screenshots in my last post. I'm sorry I was unclear.
scratch_for_ai2_help_5.aia (195.0 KB)

Try it:

scratch_for_ai2_help (7).aia (195.8 KB)

Truly weird behavior. Here are screen shots and descriptions of the aia file you just sent.

Freshly launched app:

After pressing Scan:

After selecting Dev1 for which the current state is OFF:

After connecting to Dev1:

After pressing the "toggle button":

Now the arduino has correctly been set to ON and is advertising the name Dev1_ON, BUT the button will only work once, i.e. when I press it a 2nd time it stays red and also has no effect at all on the arduino which stays in the ON state.

Similarly, here are the screenshots of going through the same process of connecting to the arduino when it's in the ON state:

Freshly launched app:

After pressing Scan:

After selecting Dev1 for which the current state is ON:

After connecting to Dev1:

After pressing the "toggle button":

Note that the button went from red and showing the OFF state to green and showing the ON state. This had no effect on the arduino, which is good because it was already in the ON state.

When I click the toggle button a 2nd time, the display is unchanged (see below) but the arduino state changes to OFF. As in the other case above, the toggle button is then inactive and cannot change the arduino state back to ON.

So, in both cases (i.e. connecting to the arduino in an OFF state or an ON state) the toggle button can change the state only one time.

It seems that this block:

component_set_get (7)

not return the currently advertised device name, only the one that was at the time of connection.A very simplified method. We will have to modify blocks to detect the current name.

Yes, you are right. That accounts for the behavior and it makes sense that ConnectedDeviceName is static after connection is made - not a live quantity. But the arduino code sets the Notfiy attribute of the characteristic:
BLEStringCharacteristic phoneTooClose(PHONE_TOO_CLOSE, BLERead | BLEWrite | BLENotify, 8);
so that the phone should be able to 'know' when the characteristic has changed (as per the Notify section here ). Somehow the phone code is not wired up to accept this Notification...

I modified blocks so that you can send the value with the toggle button, regardless of the label.

scratch_for_ai2_help (8).aia (196.0 KB)

You can try this AIA with a test button.

scratch_for_ai2_help (9).aia (196.3 KB)

Hi Patryk. Thank you for all your time and help with this. These last two AIA files don't improve the situation, unfortunately. I know that you are flying blind a bit because you don't have the arduino nano ble board(s) to test the AIA code against. Let me look into whether there's a reliable simulator which you or other people on this forum could use in lieu of physical boards to check the code - that would be a broadly useful tool and I would be happy if I could do some giving as well as taking... If there is a decent simulator, I will post it with some explanation and hope to resume our discussion. I will also post any solution I come up with to the problem we've been working on. Thanks again for all your help. I hope to post again soon :smiley:

Yes exactly. I do not have the NANO 33 BLE device. I have an ESP32 for this, I wonder if the sketch from Arduino will work in ESP32. Then I could adjust the blocks faster. I also have HM10, but it can not be changed name dynamically.

According to this, you can program the ESP32 from the Arduino IDE by adding an additional boards manager site and then adding the ESP32 to the list of boards the IDE knows about.

I have an Arduino environment adapted to ESP. Ok, will test your sketch. Experience with dynamic names can be instructive.

Sounds good! :+1:

What BLE Library do you use? Unfortunately, the ESP library is not compatible. It is a pity that they do not make libraries that are configured in the same way.

I use ArduinoBLE version 1.2.0 which I think I got on github here.

Try it:
scratch_for_ai2_help (11).aia (196.9 KB)

I went back to method, reading 5 and 9 after changing the arduino output state.

Hi Patryk. You're a star! This works like a charm. I just got back and tried it, but haven't gone through the sketch yet. Thank you very much for all your time and help. I hope the idea is useful to you, as well. And I hope to be able to return the favor... Best, Alex