BLE扫描仪无法找到BLE服务器。 (能够检测其他设备,而不是服务器。)(均使用ESP32)

发布于 2025-01-24 19:47:04 字数 3418 浏览 4 评论 0原文

我正在尝试为某些设备制作蓝牙扫描仪。但是,除了我的ESP32 BLE服务器外,我可以检测其他蓝牙设备。我可以使用“ NRF Connect”检测服务器,但无法在串行中显示。我可以知道扫描仪或服务器是一个问题吗?如果是这样,我如何能够纠正它?感谢您抽出宝贵的时间阅读我的问题。

编辑:我意识到原始未经编辑的扫描仪代码允许我查看我的服务器。但是,当恢复到此JSON代码时,它似乎无法检测到我的服务器。

这是扫描仪的代码:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan();//create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); 
  pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  Serial.println("Devices found: ");
  Serial.println(foundDevices.getCount());
  for (int i = 0; i < foundDevices.getCount(); ++i)
  {
    std::string address = foundDevices.getDevice(i).getAddress().toString();
    std::string DeviceName = foundDevices.getDevice(i).getName();
    int Rssi = foundDevices.getDevice(i).getRSSI();
    String data = "{\"""Device Name\":\"" + String(DeviceName.c_str()) + "\""",\"EDID\":\"" + String(address.c_str()) + "\""",\"RTID\":\"FF005802600001F\",\"RSSI\":\"" + int(Rssi) + "\"""}";
    Serial.println(data);    
  }
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}

这是服务器的代码:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID        "f84b1b5f-7655-476e-a3d4-a5aba18db511"
#define CHARACTERISTIC_UUID "78afb5d4-a4f8-46ab-99cb-f434f96d21f8"

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

  BLEDevice::init("**********");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello, my name is ESP32.");
  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);
}

I am trying to make a Bluetooth scanner for certain devices. However, I can detect other Bluetooth devices except for my ESP32 BLE server. I can detect my server using "nRF Connect" but it is unable to be shown in the Serial out. May I know is an issue with the scanner or the server? If so, how am I able to rectify it? Thank you for taking the time to read my question.

Edit: I realised that the original, unedited scanner code allows me to see my server. However, when reverted back to this Json code, it seems to be unable to detect my server.

Here is the code for the scanner:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan();//create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); 
  pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  Serial.println("Devices found: ");
  Serial.println(foundDevices.getCount());
  for (int i = 0; i < foundDevices.getCount(); ++i)
  {
    std::string address = foundDevices.getDevice(i).getAddress().toString();
    std::string DeviceName = foundDevices.getDevice(i).getName();
    int Rssi = foundDevices.getDevice(i).getRSSI();
    String data = "{\"""Device Name\":\"" + String(DeviceName.c_str()) + "\""",\"EDID\":\"" + String(address.c_str()) + "\""",\"RTID\":\"FF005802600001F\",\"RSSI\":\"" + int(Rssi) + "\"""}";
    Serial.println(data);    
  }
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}

and here is the code for the server:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID        "f84b1b5f-7655-476e-a3d4-a5aba18db511"
#define CHARACTERISTIC_UUID "78afb5d4-a4f8-46ab-99cb-f434f96d21f8"

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

  BLEDevice::init("**********");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello, my name is ESP32.");
  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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文