Esp32-snippets: How to detect RSSI from BLE on ESP32?

Created on 29 Nov 2017  Â·  9Comments  Â·  Source: nkolban/esp32-snippets

I have a question.
How to detect RSSI from BLE on ESP32 ?
I want to know RSSI from ESP32.
But I wasn`t able to understand a program.
I am a beginner...sorry.

enhancement study required

Most helpful comment

You can get RSSI by modifying BLE_scan example like below:

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

      if (advertisedDevice.haveRSSI()){
        Serial.printf("Rssi: %d \n", (int)advertisedDevice.getRSSI());
      }
      else Serial.printf("\n");

    }
};

It is advised to modify your BLEAdvertisedDevice.toString() method like given below:
Path of file: \espressif\esp32\libraries\BLE\src\BLEAdvertisedDevice.h

std::string BLEAdvertisedDevice::toString() {
    std::stringstream ss;
    ss << "Name: " << getName() << ", Address: " << getAddress().toString();
    if (haveAppearance()) {
        ss << ", appearance: " << getAppearance();
    }
    if (haveManufacturerData()) {
        char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
        ss << ", manufacturer data: " << pHex;
        free(pHex);
    }
    if (haveServiceUUID()) {
        ss << ", serviceUUID: " << getServiceUUID().toString();
    }
    if (haveTXPower()) {
        ss << ", txPower: " << (int)getTXPower();
    }
        if (haveRSSI()) {
        ss << ", Rssi: " << (int)getRSSI();
    }   

    return ss.str();
} // toString

All 9 comments

When you are connected to a BLE Server with the ESP32 acting as a BLEClient, there is a method on BLEClient called getRssi(). When called, it returns the RSSI value associated the partner.

If you are truly a beginner, then the above may not make overly much sense. There is little I can offer in an issue response other than to offer encouragement to persevere. There many great communities on programming, C programming, C++ programming and of course ESP32 specific. I always follow the paradigm of crawl/walk/run ... get something really simple going and then build upon it.

I appreciate for your advice.
I try to write a program like this picture.
I used getRssi();.
But error happened...
problem

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

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t value = 0;

// 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) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};



void setup() {
  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("MyESP32");

  // Create the BLE Server
  BLEServer *pServer = new BLEServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
  float h = getRssi();
  Serial.printf(h);
  if (deviceConnected) {
    Serial.printf("*** NOTIFY: %d ***\n", value);
    pCharacteristic->setValue(&value, 1);
    pCharacteristic->notify();
    //pCharacteristic->indicate();
    value++;
  }
  delay(2000);
}

I used a sample program, "BLE_notify".

Howdy,
The getRSSI() function is part of the BLEClient class ...

https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/BLEClient.cpp#L281

What I need to do is study and see if I can use the same technology and expose a getRSSI() on a BLEServer object. Flagging this issue for study.

MINWINMIN , DID YOU GOT THE RSSI VALUE FOR THE CODE WHICH YOU HAVE MENTIONED ABOVE ??
please help me it is throwing an error as getRssi() as not declared

this is the code

include

include

include

include

include

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t value = 0;

// 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) {
deviceConnected = true;
};

void onDisconnect(BLEServer* pServer) {
  deviceConnected = false;
}

};

void setup() {
Serial.begin(115200);

// Create the BLE Device
BLEDevice::init("MyESP32");

// Create the BLE Server
BLEServer *pServer = new BLEServer();
pServer->setCallbacks(new MyServerCallbacks());

// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);

// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);

// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());

// Start the service
pService->start();

// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}

void loop() {
float h = getRssi();
Serial.printf(h);
if (deviceConnected) {
Serial.printf("* NOTIFY: %d *\n", value);
pCharacteristic->setValue(&value, 1);
pCharacteristic->notify();
//pCharacteristic->indicate();
value++;
}
delay(2000);
}

please help me to find

You can get RSSI by modifying BLE_scan example like below:

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

      if (advertisedDevice.haveRSSI()){
        Serial.printf("Rssi: %d \n", (int)advertisedDevice.getRSSI());
      }
      else Serial.printf("\n");

    }
};

It is advised to modify your BLEAdvertisedDevice.toString() method like given below:
Path of file: \espressif\esp32\libraries\BLE\src\BLEAdvertisedDevice.h

std::string BLEAdvertisedDevice::toString() {
    std::stringstream ss;
    ss << "Name: " << getName() << ", Address: " << getAddress().toString();
    if (haveAppearance()) {
        ss << ", appearance: " << getAppearance();
    }
    if (haveManufacturerData()) {
        char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());
        ss << ", manufacturer data: " << pHex;
        free(pHex);
    }
    if (haveServiceUUID()) {
        ss << ", serviceUUID: " << getServiceUUID().toString();
    }
    if (haveTXPower()) {
        ss << ", txPower: " << (int)getTXPower();
    }
        if (haveRSSI()) {
        ss << ", Rssi: " << (int)getRSSI();
    }   

    return ss.str();
} // toString

I want to get address of BLE which i scan,but i dont know which method can do that,can you give me some advice about that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HarrisonOfTheNorth picture HarrisonOfTheNorth  Â·  8Comments

monteslu picture monteslu  Â·  4Comments

mahdikan picture mahdikan  Â·  4Comments

d3cline picture d3cline  Â·  4Comments

vicatcu picture vicatcu  Â·  4Comments