String from mobile phone to arduino + HC05

I have a HC05 bluetooth module connected to an arduino and it is working correctly at least as far as entering AT commands when in configuration mode and as far succesfully pairing with it and connecting to it from a mobile phone.

I have created an android app that sends a string to my HC05 and it is read in my arduino code via this function:

void processBTData()
{
    String strData, strTemp, strField;
    uint8_t nDOW = 0, nDOM = 0, nMon = 0, nYear = 0, nHr = 0, nMin = 0, nSec = 0;
    bool bDateReceived = false, bTimeReceived = false;

    while (serialBT.available() > 0)
        strData += serialBT.readString();

#ifdef DEBUG    
    Serial.println(strData.length());
#endif

    if (strData.indexOf(F("DATE")) > -1)
    {
Serial.println("@@@@@@@@@@@@@@@@@@@");      
        strTemp = strData.substring(strData.indexOf(F("DATE=")) + 5, strData.indexOf(F("TIME=")));

        strField = strTemp.substring(0, strTemp.indexOf(F(" ")));
        nDOW = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F(" ")) + 1);

        strField = strTemp.substring(0, strTemp.indexOf(F("/")));
        nDOM = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F("/")) + 1);

        strField = strTemp.substring(0, strTemp.indexOf(F("/")));
        nMon = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F("/")) + 1);

        strField = strTemp.substring(0, strTemp.indexOf(F("/")));
        nYear = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F("/")) + 1);
        bDateReceived = true;
    }        
    if (strData.indexOf(F("TIME=")) > -1)
    {
        strTemp = strData.substring(strData.indexOf(F("TIME=")) + 5, strData.length());

        strField = strTemp.substring(0, strTemp.indexOf(F(":")));
        nHr = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F(":")) + 1);

        strField = strTemp.substring(0, strTemp.indexOf(F(":")));
        nMin = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F(":")) + 1);

        strField = strTemp.substring(0, strTemp.indexOf(F(" ")));
        nSec = strField.toInt();
        strTemp = strTemp.substring(strTemp.indexOf(F(" ")) + 1);
        Serial.print(F("nSec = "));
        Serial.println(nSec);
        Serial.print(F("strTemp = "));
        Serial.println(strTemp);
        strField = strTemp;
        
        if ((strField == F("PM")) && (nHr >= 1) && (nHr <= 11))
            nHr += 12;
        else if ((strField == F("AM")) && (nHr == 12))
            nHr = 0;
            
        bTimeReceived = true;
    }
        
    if (bTimeReceived && bDateReceived)
        serialBT.print(F("OK"));
    else if (bTimeReceived)
        serialBT.print(F("DATE ERROR"));
    else if (bDateReceived)
        serialBT.print(F("TIME ERROR"));
    else        
        serialBT.print(F("ERROR"));

#ifdef DEBUG
    Serial.println("##################");
    Serial.print("nDOW = ");
    Serial.println(nDOW);
    Serial.print("nDOM = ");
    Serial.println(nDOM);
    Serial.print("nMon = ");
    Serial.println(nMon);
    Serial.print("nYear = ");
    Serial.println(nYear);
    Serial.print("nHr = ");
    Serial.println(nHr);
    Serial.print("nMin = ");
    Serial.println(nMin);
    Serial.print("nSec = ");
    Serial.println(nSec);
    Serial.println("##################");
#endif

    clock.setDateTime(nDOW, nDOM, nMon, nYear, nHr, nMin, nSec);
}

In my android app I have confirmed that the string being sent is as expected.

But at the arduino end I don’t know what I am getting.

#ifdef DEBUG
Serial.println(strData.length());
#endif

This bit of code outputs 63 in the serial monitor so I am getting some sort of string.

However there seems to be no way of outputing the contents of that string - all I get is a blank line.

if (strData.indexOf(F(“DATE”)) > -1)
{
Serial.println("@@@@@@@@@@@@@@@@@@@");

The string I am getting at the arduino end does not contain the marker “DATE” but at the android app end the string being sent does contain this marker.

With no apparent way to see what the string is that is being received on the arduino I have no way of resolving this bug.

Any suggestions?

Could this be a character encoding problem?

E.G. 2 bytes per char at the android end but 1 byte per char expected at the arduino end.

Actually I found that this works:

Serial.println("@@@@@@@@@@@@@@@@@@@");      
    Serial.println(strData.length());
    for (int nI = 0; nI < strData.length(); nI++)
    {
        Serial.print(strData[nI], HEX);
        Serial.print(" ");
    }
Serial.println("");      
Serial.println("@@@@@@@@@@@@@@@@@@@");

And results in this output:

@@@@@@@@@@@@@@@@@@@
63
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 29 0 0 0 0 57 1 5 5 0 0 0 1E 0 0 6 0 6 31 F 0 57 1 29 0 0 0 0 57 1 5 5
@@@@@@@@@@@@@@@@@@@

It's got to be something to do with character encoding because the ASCII code for the string amounts to garbage.

Since you are using .length() method I assume strData is a String.

Can you really just pretend a String is a char array and access individual characters like this?

strData[nI]

Since you seem to understand how to work with char arrays, why not drop the String class and make strData a proper char array.