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

ESP32蓝牙连接带配对码的Printer001设备问题求助

ESP32蓝牙配对码设置与连接解决方案

要让ESP32连接需要配对码(1234)的蓝牙设备,BluetoothSerial库本身没有直接暴露配对码设置接口,需通过BlueDroid底层API配置固定配对码并处理配对请求回调。以下是修改后的完整代码及关键说明:

#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

#define BT_DISCOVER_TIME 10000
#define PAIRING_PIN "1234" // 目标设备的配对码

static bool btScanAsync = true;
static bool btScanSync = true;
static bool isConnected = false;

// 配对请求回调函数
esp_err_t auth_req_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) {
    if (event == ESP_BT_GAP_AUTH_CMPL_EVT) {
        if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
            Serial.println("配对成功");
            isConnected = true;
        } else {
            Serial.printf("配对失败,错误码: %d\n", param->auth_cmpl.stat);
            isConnected = false;
        }
    } else if (event == ESP_BT_GAP_PIN_REQ_EVT) {
        // 响应配对码请求,传入预设的配对码
        esp_bt_pin_code_t pin_code;
        memcpy(pin_code, PAIRING_PIN, 4);
        esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
        Serial.println("发送配对码");
    }
    return ESP_OK;
}

void btAdvertisedDeviceFound(BTAdvertisedDevice *pDevice) {
  Serial.printf("异步发现设备: %s\n", pDevice->toString().c_str());
  // 如果找到目标设备,尝试连接
  if (strcmp(pDevice->getName().c_str(), "Printer001") == 0 && !isConnected) {
      Serial.println("开始连接目标设备...");
      if (SerialBT.connect(pDevice->getAddress())) {
          Serial.println("连接请求已发送,等待配对");
      } else {
          Serial.println("连接请求发送失败");
      }
  }
}

void setup() {
  Serial.begin(115200);
  
  // 注册蓝牙配对回调
  esp_bt_gap_register_callback(auth_req_callback);
  // 设置配对模式为固定码模式
  esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
  esp_bt_gap_set_pin(pin_type, 4, (uint8_t *)PAIRING_PIN);
  
  SerialBT.begin("ESP32test");  // Bluetooth device name
  Serial.println("设备已启动,现在可通过蓝牙配对!");

  if (btScanAsync) {
    Serial.print("开始异步扫描... ");
    if (SerialBT.discoverAsync(btAdvertisedDeviceFound)) {
      Serial.println("扫描结果将在\"btAdvertisedDeviceFound\"函数中返回");
      delay(10000);
      Serial.print("停止异步扫描... ");
      SerialBT.discoverAsyncStop();
      Serial.println("已停止");
    } else {
      Serial.println("异步扫描启动失败,例如连接后无法扫描");
    }
  }

  // 同步扫描可保留,用于确认设备存在
  if (btScanSync) {
    Serial.println("开始同步扫描... ");
    BTScanResults *pResults = SerialBT.discover(BT_DISCOVER_TIME);
    if (pResults) {
      pResults->dump(&Serial);
    } else {
      Serial.println("蓝牙扫描错误,无结果!");
    }
  }
}

void loop() {
  // 周期性检查连接状态
  if (isConnected) {
    Serial.println("设备已连接");
  }
  delay(2000);
}

关键代码说明

  • 配对配置:调用esp_bt_gap_set_pin()将ESP32设置为固定配对码模式,指定配对码为1234;通过esp_bt_gap_register_callback()注册配对回调函数,处理配对过程中的请求与结果。
  • 配对回调逻辑:
    • 收到配对码请求事件ESP_BT_GAP_PIN_REQ_EVT时,自动回复预设的1234配对码。
    • 配对完成事件ESP_BT_GAP_AUTH_CMPL_EVT触发时,输出配对状态并更新连接标记。
  • 自动连接触发:在扫描回调btAdvertisedDeviceFound中,识别到目标设备"Printer001"且未连接时,自动发起连接请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:17:07