设备连接后,ESP32 BLE 广告就会停止:

Ann*_*gam 5 esp32

我正在研究 ESP32 BLE。我正在使用 Arduino IDE 进行编程。

在我的项目中,我想在连续广告模式下使用 BLE。但实际上发生的情况意味着,一旦设备连接到 BLE 应用程序一次,BLE 广告就会停止。此后 BLE 广告停止。此后我无法连接到该 BLE 设备。我什至在 BLE 移动应用程序中看不到它的广告(使用开源移动应用程序:nRF connect、BLE 扫描仪..)。然后,在 ESP32 板上仅发生硬重置后,它就开始其广告过程。

当我的笔记本电脑中编译 BLE 代码时,我遇到了这个问题。我已经检查了 3 个或更多系统,其中在一个系统中编译的基本示例 BLE_server(内置 ESP32 Arduino IDE 示例)代码运行良好。在其余 3 个系统中编译的代码导致了我上面提到的问题。我已在此处附上 Arduino IDE 配置详细信息 - 图片。请找到附件。

这里我使用的是Arduino IDE版本1.8.13。在我的笔记本电脑中,我使用了 Java(TM) SE 开发套件 18.0.1.1(64 位)和 python 版本 2.7.15。

我已将 Arduino IDE 版本和 ESP32 板版本更新为。

出现这个问题的原因是什么?我的 Arduino IDE 中是否缺少任何配置?

编译过程中可能有什么问题吗?

请帮我解决这个问题吗?

等待您的积极答复。

提前致谢!!!

小智 4

一旦设备连接,广告就会停止,一旦发生这种情况,您需要重新启动它

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      pServer->startAdvertising(); // restart advertising
    };

    void onDisconnect(BLEServer* pServer) {
      pServer->startAdvertising(); // restart advertising
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  
  pServer->setCallbacks(new MyServerCallbacks()); //set the callback function
  
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}
Run Code Online (Sandbox Code Playgroud)