首页 > 解决方案 > 将字节数组转换为字符串?ESP32 BLE 固件开发

问题描述

我正在使用一个 android 应用程序(手机)和一个 ESP32,通过 BLE 相互连接。我正在尝试将字符串从手机发送到 ESP32。android 应用程序以字节数组形式发送一个字符串,esp 接收它。但我无法检索 ESP 固件上的值。

下面是我尝试过的方法的代码。这段代码的全部意义在于将从 android 应用程序发送的字符串存储在这个变量中:INCOMING_STRING1. 我尝试将接收变量设置为字符数组(我认为这就是这个变量的用途),然后strcpy假设传入数据的内容(通过字符参数传入)将被复制到INCOMING_STRING1,但是,它没有不行。

    //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);
        }
    }

我知道这可能是类型未匹配,因为传入的数据是 typeuint8_t并且存储字符串的变量是 type char。一般来说,我一直在为类型转换和类型操作而苦苦挣扎,感谢任何帮助!

这是我此时收到的错误消息:

错误:传递“strcpy”的参数 2 的指针目标在符号上不同 [-Werror=pointer-sign] strcpy(INCOMING_STRING1, character); ^ 注意:预期为 'const char * restrict' 但参数的类型为 'const uint8_t * {aka const unsigned char *}'

标签: arraysstringbluetooth-lowenergyesp32bluetooth-gatt

解决方案


你试过这种方法吗?

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

它是一种简单的转换方法,如果您不在 arduino IDE 上,您可能想要包含字符串库。

#include <string>

注意我没有把“.h”放在字符串的末尾,这样试试


推荐阅读