简体   繁体   中英

Convert byte array to string? ESP32 BLE firmware development

I am using an android app (mobile phone) and an ESP32, connected to each other via BLE. I am trying to send a string from the mobile phone to the ESP32. The android app sends a string in byte array form and the esp receives it. But I am having trouble retrieving the value on the ESP firmware.

Below is the code for the method that I tried. The whole point of this code is for the string that was sent from the android app to be stored in this variable: INCOMING_STRING1 . I tried setting up the receiving variable as a character array (I think that's what this variable is for), and then used strcpy assuming that the contents of the incoming data (passed in through the character parameter) would be copied to INCOMING_STRING1 , however, it doesn't work.

    //Variables
    const uint8_t *character;
    char INCOMING_STRING1[64];

    //Elsewhere in code...
    //event comes from the ESP BLE module when a BLE event happens
    switch(event)
    {
        case ESP_GATTS_WRITE_EVT:
            writeHandle(param->write.handle);
            break;
        ...
    }

    //Elsewhere in code...
    static void writeHandle(uint16_t handle)
    {
        get_attr_ret = esp_ble_gatts_get_attr_value(handle, &length, &character);

        //There's different handles for different "channels"
        if(handle == 45)
        {
            //supposed to take string that was received
            strcpy(INCOMING_STRING1, character);
            //then print it to make sure
            printf("%s", INCOMING_STRING1);
        }
    }

I'm aware that this is probably a type miss match, since the incoming data is of type uint8_t and the variable to store the string is type char . I've always struggled with type casting and type manipulation in general, any help is appreciated!

Here's the error messages I get at this moment:

error: pointer targets in passing argument 2 of 'strcpy' differ in signedness [-Werror=pointer-sign] strcpy(INCOMING_STRING1, character); ^ note: expected 'const char * restrict' but argument is of type 'const uint8_t * {aka const unsigned char *}'

Have you tried this way?

//supposed to take string that was received
strcpy(INCOMING_STRING1.c_str(), character);

its a simple conversion method, you might wanna include the string library if you are not on arduino IDE.

#include <string>

Notice I didn't put the ".h" on the end of string, try it this way

I think you are close with your solution. It needs to minor fixes:

The first fix is to declare character differently:

const char *character;

uint8_t is an unsigned byte, char is usually a signed byte (it's unfortunately implementation defined).

The second problem is that the transmitted string is most likely not terminated with a 0 byte. So you cannot use strcpy and have to add the 0 byte yourself. So instead of strcpy :

memcpy(INCOMING_STRING1, character, length);
INCOMING_STRING1[length] = 0;

A further potential problem is the length of the string. By default, Android BLE will only transmit messages with a payload of up to 20 bytes. If the string is longer, is transmitted in several messages. So each time you call esp_ble_gatts_get_attr_value , you will only get a piece of the string.

So start your tests with small strings. If you run into a problem with longer strings, then change your Android code to increase the MTU of the BLE connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM