ESP32蓝牙模块配对后无法连接,图像传输功能异常求助
ESP32蓝牙配对后无法连接问题排查
我想用ESP32模块实现蓝牙图像传输,但写完代码后,模块能配对却无法和设备连接。这是我第一次碰到配对后连不上的蓝牙模块问题,实在搞不懂原因,附上代码,麻烦帮忙排查。

编辑1:我是新手,为我的失误致歉。
#include "esp_camera.h" #define CAMERA_MODEL_AI_THINKER #include "camera_pins.h" #include "BluetoothSerial.h" BluetoothSerial SerialBT; void setup() { Serial.begin(115200); initBT(); initCamera(); } void initBT(){ if(!SerialBT.begin("ESP32CAM-CLASSIC-BT")){ Serial.println("An error occurred initializing Bluetooth"); ESP.restart(); }else{ Serial.println("Bluetooth initialized"); } SerialBT.register_callback(btCallback); Serial.println("The device started, now you can pair it with bluetooth"); } void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){ if(event == ESP_SPP_SRV_OPEN_EVT){ Serial.println("Client Connected!"); }else if(event == ESP_SPP_DATA_IND_EVT){ Serial.println("bağlanmadı"); Serial.printf("ESP_SPP_DATA_IND_EVT len=%d, handle=%d\n\n", param->data_ind.len, param->data_ind.handle); String stringRead = String(*param->data_ind.data); int paramInt = stringRead.toInt() - 48; Serial.printf("paramInt: %d\n", paramInt); setCameraParam(paramInt); } } void writeSerialBT(camera_fb_t *fb){ SerialBT.write(fb->buf, fb->len); SerialBT.flush(); } void initCamera(){ camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; //init with high specs to pre-allocate larger buffers if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } // camera init esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); ESP.restart(); } } void setCameraParam(int paramInt){ sensor_t *s = esp_camera_sensor_get(); switch(paramInt){ case 4: s->set_framesize(s, FRAMESIZE_UXGA); break; case 3: s->set_framesize(s, FRAMESIZE_SXGA); break; case 2: s->set_framesize(s, FRAMESIZE_XGA); break; case 1: s->set_framesize(s, FRAMESIZE_SVGA); break; case 0: default: s->set_framesize(s, FRAMESIZE_VGA); break; } capture(); } void capture(){ camera_fb_t *fb = NULL; esp_err_t res = ESP_OK; fb = esp_camera_fb_get(); if(!fb){ esp_camera_fb_return(fb); return; } if(fb->format != PIXFORMAT_JPEG){ return; } writeSerialBT(fb); esp_camera_fb_return(fb); } void loop() { }
排查与修复要点
- 补全蓝牙回调事件日志:当前回调仅处理了2种事件,添加
ESP_SPP_CONN_EVT、ESP_SPP_CLOSE_EVT、ESP_SPP_DISCON_EVT等事件的打印,通过串口日志确认连接过程中触发的事件及错误码,定位连接失败环节。 - 检查GPIO资源冲突:ESP32-CAM的部分引脚与蓝牙功能存在复用可能,确认AI-Thinker摄像头的引脚配置未占用蓝牙射频或UART的默认引脚(如GPIO16、GPIO17)。
- 确认SPP连接发起动作:配对成功≠SPP连接建立,部分设备配对后需手动触发SPP连接请求,检查客户端设备是否主动发起了SPP连接操作。
- 修复代码逻辑漏洞:
capture函数中,若fb为空,调用esp_camera_fb_return(fb)会触发空指针错误,需移除该行。String stringRead = String(*param->data_ind.data);仅读取了数据的第一个字节,若发送多字节指令会导致解析错误,需根据param->data_ind.len读取完整数据。
- 简化蓝牙名称:尝试将蓝牙名称改为短字符串(如
ESP32-CAM-BT),避免特殊字符或过长名称导致设备识别异常。 - 验证资源占用情况:在摄像头初始化前后添加日志,确认摄像头初始化是否占用过多资源导致蓝牙服务异常,若摄像头初始化失败,可调整摄像头配置参数(如降低分辨率)后重试。
内容的提问来源于stack exchange,提问作者narniapick
相关产品推荐
相关产品推荐

