arrays - 将字节数组转换为字符串?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 *}'
解决方案
你试过这种方法吗?
//supposed to take string that was received
strcpy(INCOMING_STRING1.c_str(), character);
它是一种简单的转换方法,如果您不在 arduino IDE 上,您可能想要包含字符串库。
#include <string>
注意我没有把“.h”放在字符串的末尾,这样试试
推荐阅读
- firebase - Imagemagick 在 Firebase 云功能 Spawn Child Process 中转换失败
- c - 当我尝试在某个目录中创建一个 txt 文件时,它的末尾有垃圾字符
- sql - 如何总结sqlite中的连接表?
- ionic-framework - Ionic 4:bgImage 未显示在 StreamingAudioOptions
- azure - Azure Function - Out 绑定后如何触发函数
- javascript - 即使指定了 IP 和 Geo,位置也不会改变
- swift3 - xcode 10.2 Swift 中没有这样的模块“IBMCloudAppID”
- python - 二等分递归解决方案以找到 pow(x,n)
- c - 答案总是零
- android - Espresso - 如何定位并单击 ListView 中的第一个项目