App Runtime error - Characteristic are not published by the connected device

Hi, I am connecting an Arduino Nano 33IoT Blue Tooth to an App Inventor app fairly successfully. I´m having some issues, though, at the App inventor end when the Arduino code includes more than the BlueTooth advertising, e.g. when advertising from the blue tooth and sending data to a serial port, both actions are within the same loop. If only the Blue tooth is active within the main loop no issues at all and everything runs neatly. However, when the serial comm is included in the Arduino code, the app fails and sends out a TIMEOUT runtime error, as follows:

Runtime error

When connecting to the device through a different app, no runtime issue at all is present. I have used the LightBlue app, which never fails to connect, but takes sometimes up to 35 seconds to establish a Blue Tooth connection and matching. Test of proof as follows:

LightBlue

Regarding the App Blocks, I attach the file as follows:

BT_Communication_V1_6.aia (240.9 KB)

And the Arduino Main Loop code, as follows =>

void loop()
{

static long preMillis = 0;

// listen for BLE centrals devices
BLEDevice central = BLE.central();

// central device connected?
if (central)
{
digitalWrite(LED_BUILTIN, HIGH); // turn on the onboard led
Serial.print("Connected to central: ");
Serial.println(central.address()); // central device MAC address

// while the central is connected to peripheral
while (central.connected()) 
  {

  long curMillis = millis();
       
  if (preMillis>curMillis) {preMillis=0;} // millis() rollover?
  
  if (curMillis - preMillis >= 6000)     // check values every 500mS .... Originally was 10 ms (Changed By JPV)
                                         // If the time slot is too short, the APP at the Mobile Phone behaves unstable
     {
       preMillis = curMillis;

       bapPercentage = 100*(ay/PPM_ALARM_Base);  // Percentage of Status ALARM warning [%]
                                                 // Careful with decimals after dividing. At The APP side this variable has no decimals

       // ***   Sensors measurements transfered into Main Variables for BleuTooth TX to MobilePhone App   ***

           ReadAllSensors();            // Data retreival
                               
           SGP40_VOC=0;                // Must delete later, when SGP40 working
                                                                               
           //S_ax = (S_day+":"+S_month+":"+S_year+" "+sep+" "+S_hour+":"+S_minute); // Date & Time
           S_ax ="101"; 
           ay=float(CO2);                                                         // CO2 [PPM]
           az=float(SGP40_VOC);                                                   // VOC Index
           gx=BME280Temperature;                                                  // Temperature [°C]
           gy=BME280Pressure/100;                                                 // Pressure [hPa]    
           gz=BME280_RH;                                                          // RH [%]
           ba=float(CO2);                                                         // Used to present CO2 Level together with Warning Message
           bap=bapPercentage;                                                     // Hazard Percentage [%]

       
       // ***   END measurements transfered into Main Variables ***
       
       SendDataToApp(S_ax, ay, az, gx, gy, gz, ay, bap); // Data refresh function
       
                
      }   // End of IF loop, regarding millis delay
          // Serial.println("Within the If loop");
        
          
    }  // End of loop of Central Connected  
          
        //Serial.println("Within the CENTRAL CONNECTED loop");  

  } 
  else
  {
  // central disconnected:
  digitalWrite(LED_BUILTIN, LOW);
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
  // no central

  ReadAllSensors(); 
  BuildDDBBMessage();
  ReadWriteSERCOMM2(); 

  }

//----- MAIN END down Below --------------------------------------------------------------------------------------------------------------
}

If I adjust the time gap in the following line, the app attempts to connect but a new runtime error pops up, as follows =>

SecondRuntimeError

// while the central is connected to peripheral
while (central.connected())
{

  long curMillis = millis();
       
  if (preMillis>curMillis) {preMillis=0;} // millis() rollover?
  
    if (curMillis - preMillis >= 20000)     // check values every 500mS .... Originally was 10 ms (Changed By JPV)
                                         // If the time slot is too short, the APP at the Mobile Phone behaves unstable
     {
       preMillis = curMillis;

Can somebody indicate what the issue might be or what thread to follow-up to sort out this issue ?

It seems there is a TIMEOUT control at the App-Inventor app, I have not been able to find. I do not have saunded skills in App-Inventor, so I have adapted another code to make it work.

The appinventor code reads and then shows collected data from several sensors linked to the Arduino Nano 33IoT and then sending it from the Arduino Nano 33IoT through its BlueTooth device.

I include the whole App-Inventor Code and the BLOCK code segment,at a glance, where I asume the issue might be related to.

Thanks in advance,

JP