You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter新手使用flutter_blue_plus通过BLE连接ESP32发送数据失败求助

解决方案

1. 修正ESP32端BLE特征的属性配置

报错的核心是目标BLE特征未开启WRITE属性,这是ESP32端的配置问题,和数据读取方式(getValue()/getData())无关。

在ESP32定义BLE特征时,必须显式添加BLECharacteristic::PROPERTY_WRITE或BLECharacteristic::PROPERTY_WRITE_NR(无响应写入)属性,示例代码如下:

// 正确的特征定义(同时开启WRITE和NOTIFY属性,按需调整)
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
    "你的特征UUID",
    BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY
);

同时要确保注册了写入回调,才能接收Flutter端发送的数据:

// 注册回调
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());

// 回调类实现
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) {
    // 两种数据读取方式都可用,按需选择
    std::string strValue = pCharacteristic->getValue();
    uint8_t* byteData = pCharacteristic->getData();
    size_t dataLen = pCharacteristic->getLength();
    
    // 这里添加你的数据处理逻辑
  }
};

2. Flutter端写入逻辑检查

  • 先确认目标特征确实支持WRITE属性,避免无效写入:
// 获取特征后先校验属性
if (characteristic.properties.write) {
  // 执行写入(默认带响应)
  await characteristic.write(utf8.encode("测试数据"));
} else {
  print("当前特征不支持WRITE操作");
}
  • 如果ESP32端配置的是PROPERTY_WRITE_NR(无响应写入),需要在Flutter端传入对应参数:
// 无响应写入
await characteristic.write(utf8.encode("测试数据"), withoutResponse: true);

3. 关键调试步骤

  • 用BLE调试工具(如nRF Connect)连接ESP32,查看目标特征的属性列表,确认WRITE属性是否存在。如果工具中都看不到WRITE属性,说明ESP32端配置未生效,需重新烧录代码并重启设备。
  • 核对Flutter端与ESP32端的服务UUID、特征UUID必须完全一致(注意十六进制的大小写统一)。

内容的提问来源于stack exchange,提问作者Ahminmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:24:59