0

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);
}
Koo ZH
  • 11
  • 3

0 Answers0