Not all BLEdevice around me are recognized by ES32 WROOM board

gabryumts
Posts: 1
Joined: Sat Mar 20, 2021 11:37 am

Not all BLEdevice around me are recognized by ES32 WROOM board

Postby gabryumts » Sat Mar 20, 2021 12:16 pm

Hi all,
it is already few days I'm struggling to understand why some of the BLE device I want to connect to my ESP32 WROOM module are not detected during the scanning process.
Let me explain:

1) I have two BLE device (an Heart rate monitor and a bike cadence sensor). Both of them are properly working and detected if I'm using a debugging app running on my smartphone (nRF Connect). So both device are properly advertising themself and properly detected by my smartphone.

2) I have implemented a basic arduino skatch using the following BLE libraries:

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

3) The problem is that the scan procedure on the ESP32, recognize the "cadence" sensor while the hear rate monitor IS NOT RECOGNIZED AT ALL !! .....I have no clue why the ESP32 properly see the "candence sensor" while the hear rate monitor is completely ignored....

4) I have also tried to do a test with only one device in the range and still, If the device is the candence sensor, everything works fine but if the device is the heartrate sensor, no device is detected by the ESP32.

...how can I debug this situation ?

thanks to anybody that could advice me on how to proceed.

regards
Gabriele

TXEMA69
Posts: 2
Joined: Wed May 12, 2021 7:05 pm

Re: Not all BLEdevice around me are recognized by ES32 WROOM board

Postby TXEMA69 » Wed May 12, 2021 7:22 pm

It seems very strange .
I am working in the same project, I am trying to connect two BLE servers (Server 1: Heart Rate Sensor // Server2 : Cadence sensor) with ESP in client mode. I am unable to connect the two servers with one client, I am able to connect separately and I am able to get the characteristics separately. try my project..

#include "BLEDevice.h"

// Servicio y Caracteristicas Bluetooth --HRM-- al que nos queremos conectar (0x180D)
static BLEUUID serviceUUID_HRM(BLEUUID((uint16_t)0x180D)); //pulsaciones 0x180D
static BLEUUID charUUID_HRM(BLEUUID((uint16_t)0x2A37));//pulsaciones 0x2A37
static BLERemoteCharacteristic* pRemote_HR_Characteristic;
//-------------------------------------------------------------------------------------------
// Servicios y Caracteristicas Bluettoth --BATERIA-- que queremos consultar(0X180F)
static BLEUUID serviceUUID_BAT(BLEUUID((uint16_t)0x180F)); //bateria 0x180F
static BLEUUID charUUID_BAT(BLEUUID((uint16_t)0x2A19));//bateria 0x2A19
static BLERemoteCharacteristic* pRemote_BAT_Characteristic;
//-------------------------------------------------------------------------------------------
// Servicios y Caracteristicas Bluettoth --CADENCIA-- que queremos consultar(0X1816)
static BLEUUID serviceUUID_CAD(BLEUUID((uint16_t)0x1816)); //CADENCIA 0x1816
static BLEUUID charUUID_CAD(BLEUUID((uint16_t)0x2A5B));//CADENCIA 0x2A5B
static BLERemoteCharacteristic* pRemote_CAD_Characteristic;
//-------------------------------------------------------------------------------------------
//Device connection
static boolean doConnect = false;
static boolean connected = false;
static boolean notification = false;
static boolean doScan = false;
static boolean EncontradoServicioHRM=false;
static boolean EncontradoServicioCAD=false;
static boolean HRConnected=false;
static boolean CADConnected=false;
static boolean BuscaHRM=false;
static boolean BuscaCAD=false;
String caracteristica="";
String servidorHR="";
String servidorCAD="";
String dispositivoencontrado="";
const long interval = 6;
uint16_t tiempoActual=0;
uint8_t ultimasvueltas=0;
uint16_t tiempodesdeultimavuelta=0;
uint16_t milistranscurridos=0;
unsigned long previousMillis = 0; // Tiempo anterior 0
static BLEAdvertisedDevice* myDeviceHR;
static BLEAdvertisedDevice* myDeviceCAD;

//=====================================PROCEDIMIENTO NOTIFYCALLBACK===========================================
//-----Esta funcion se ejecuta cada vez que se recibe una trama del cliente conectado-------
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,uint8_t* pData,size_t length,bool isNotify) {
Serial.print("Recibida Trama de la caracteristica: ");
caracteristica=pBLERemoteCharacteristic->getUUID().toString().c_str();
Serial.print(caracteristica);
Serial.print(" de longitud (Bytes): ");
Serial.println(length);
//----------------------------callback HRM---------------------------------------------------------------
if(caracteristica.indexOf("2a37") > 0){ // la trama es de HRM
uint8_t HR_val = 0; //Inicialization HR value
//------Rutina que comprueba si hay contacto con el sensor y si las pulsaciones estan en rango-----
if (pData[0] == 22 || pData[0] == 6) { //if contact is detected.
if (pData[1] > 10 && pData[1] < 254) { // HR must be between 10 and 254 bpm
HR_val = pData[1]; // If the HR value is in the interval, then is valid.
} else HR_val =0; // HR value out of range.
} else HR_val = 0; //no hay contacto con el sensor.
//---------------------------------------------------------------------------------------------------
//Muestra valores.
if (length == 6) {
Serial.print("Heart Rate ");
Serial.print(HR_val, DEC);
Serial.println("bpm");
}
}; //----------------------detectada trama HRM-----------------------------------------------------------
//---------------------------callback CAD----------------------------------------------------------------
if(caracteristica.indexOf("2a5b") > 0){ // la trama es de CAD
long CAD_val = 0; //Inicialization CAD value
//------Rutina que comprueba si la cadencia esta en rango-----
tiempoActual = 256*pData[4] + pData[3];
if (tiempoActual>tiempodesdeultimavuelta) { // CAD must be between 0 and 254 rpm
milistranscurridos=((tiempoActual-tiempodesdeultimavuelta));
CAD_val= (60000*(pData[1]-ultimasvueltas))/milistranscurridos;

//CAD_val= ((pData[1]-ultimasvueltas)/minutostranscurridos);
ultimasvueltas=pData[1];
tiempodesdeultimavuelta=tiempoActual;
} else CAD_val =0; // CAD value out of range

//---------------------------------------------------------------------------------------------------
//Muestra valores.
if (length == 5) {
Serial.print("Cadence ");
Serial.print(CAD_val, DEC);
Serial.println("rpm");
}
}; //----------------------------detectada trama CAD-----------------------------------------------------
}//----------------------------------FIN PROCEDIMIENTO CALLBACK-----------------------------------------------
//------------------------------------------------------------------------------------------------------------
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclientHR) {
}
void onDisconnect(BLEClient* pclientHR) {
connected = false;
Serial.println("onDisconnect");
}
};
//----------FUNCION CONECTAR A SERVIDOR-----------------------------------------------------------------
bool connectToServer() {
Serial.print("Connecting to.. ");
Serial.print(dispositivoencontrado);




//Serial.print(MyDeviceCAD);

//-----------------------------------HRM--------------------------------------------------------------------------------------------
if ((HRConnected==false) && BuscaHRM){ //Conectar Servicio HRM........................
servidorHR=(myDeviceHR->getAddress().toString().c_str());
Serial.println(servidorHR);
BLEClient* pClientHR = BLEDevice::createClient();// create a new client
Serial.println(" - Created client");
pClientHR->setClientCallbacks(new MyClientCallback());
// Connect to the remote BLE Server.
pClientHR->connect(myDeviceHR); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClientHR->getService(serviceUUID_HRM);
if (pRemoteService == nullptr) {
Serial.print("Failed to find HR service UUID: ");
Serial.println(serviceUUID_HRM.toString().c_str());
pClientHR->disconnect();
return false;
}
Serial.println(" - Found HR service");
// Obtain a reference to the HR characteristic in the service of the remote BLE server.
pRemote_HR_Characteristic = pRemoteService->getCharacteristic(charUUID_HRM);
if (pRemote_HR_Characteristic == nullptr) {
Serial.print("Failed to find HR characteristic UUID: ");
Serial.println(charUUID_HRM.toString().c_str());
pClientHR->disconnect();
return false;
}
Serial.println(" - Found HR characteristic");
// Read the value of the characteristic.
if(pRemote_HR_Characteristic->canRead()) {
std::string value = pRemote_HR_Characteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pRemote_HR_Characteristic->canNotify())
pRemote_HR_Characteristic->registerForNotify(notifyCallback);
connected = true;
HRConnected=true;
return true;
}
//-----------------------------------------FIN HRM---------------------------------------------------------------------------------------

//-----------------------------------CAD--------------------------------------------------------------------------------------------
if ((CADConnected==false)&& BuscaCAD){ //Conectar Servicio CAD........................
servidorCAD=(myDeviceCAD->getAddress().toString().c_str());
Serial.println(servidorCAD);
BLEClient* pClientCAD = BLEDevice::createClient();// create a new client
Serial.println(" - Created client");
pClientCAD->setClientCallbacks(new MyClientCallback());
// Connect to the remote BLE Server.
pClientCAD->connect(myDeviceCAD); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClientCAD->getService(serviceUUID_CAD);
if (pRemoteService == nullptr) {
Serial.print("Failed to find CAD service UUID: ");
Serial.println(serviceUUID_CAD.toString().c_str());
pClientCAD->disconnect();
return false;
}
Serial.println(" - Found CAD service");
// Obtain a reference to the CAD characteristic in the service of the remote BLE server.
pRemote_CAD_Characteristic = pRemoteService->getCharacteristic(charUUID_CAD);
if (pRemote_CAD_Characteristic == nullptr) {
Serial.print("Failed to find CAD characteristic UUID: ");
Serial.println(charUUID_CAD.toString().c_str());
pClientCAD->disconnect();
return false;
}
Serial.println(" - Found CAD characteristic");
// Read the value of the characteristic.
if(pRemote_CAD_Characteristic->canRead()) {
std::string value = pRemote_CAD_Characteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pRemote_CAD_Characteristic->canNotify())
pRemote_CAD_Characteristic->registerForNotify(notifyCallback);
connected = true;
CADConnected=true;
return true;
}
//-----------------------------------------FIN HRM---------------------------------------------------------------------------------------


}
//-------------------------------------------------------------------------------------------------------------------
// -----------Scan for BLE servers and find the first one that advertises the service we are looking for.------------
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { //Called for each advertising BLE server.

void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
dispositivoencontrado=advertisedDevice.toString().c_str();
Serial.println(dispositivoencontrado);
if (dispositivoencontrado.indexOf("32631-113")>0) {
BuscaCAD=true;
BuscaHRM=false;
};
if (dispositivoencontrado.indexOf("20396-209")>0) {
BuscaCAD=false;
BuscaHRM=true;
};

// We have found a device, let us now see if it contains the service we are looking for.
if ((EncontradoServicioHRM==false)&& BuscaHRM) {
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID_HRM)) {
EncontradoServicioHRM=true;
Serial.print("HRM Service found: ");
//BLEDevice::getScan()->stop();
myDeviceHR = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
//doScan = true;
}; //Server HRM Found
} // Service HRM Scan
if ((EncontradoServicioCAD==false)&& BuscaCAD) {
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID_CAD)) {
EncontradoServicioCAD=true;
Serial.print("CAD Service found: ");
BLEDevice::getScan()->stop();
myDeviceCAD = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;
}; //Server CAD Found
} // Service CAD Scan
if ((EncontradoServicioHRM)&&(EncontradoServicioCAD)) {
Serial.print("Both Services found, stopping Scan: ");
BLEDevice::getScan()->stop();
doConnect = true;
doScan = true;
} // Services Stop



} // onResult
}; // MyAdvertisedDeviceCallbacks class

//=============================================================================================================
void setup() {
Serial.begin(115200);
Serial.println("Starting ESP32 BLE Client application...");
BLEDevice::init(""); //ESP32 BLE Initialitation.

// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds.
BLEScan* pBLEScan = BLEDevice::getScan(); //Se crea un objeto para cada dispositivo BLE detectado.
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //funcion a la que llamar cuando se reciban tramas
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449); // less or equal Interval value.
pBLEScan->setActiveScan(true); //active scan uses more power, but get better results
pBLEScan->start(5, false);
} // End of setup.

//=============================================================================================================
void loop() {

// If the flag "doConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true) {
if (connectToServer()) {
Serial.println("Connected to the BLE Server.");
} else {
Serial.println("Server Connection Failure, Exit.");
}
doConnect = false;
}
if (connected) {
if (notification == false) {
Serial.println(F("Turning Notifocation On"));
const uint8_t onPacket[] = {0x1, 0x0};
if(BuscaHRM) pRemote_HR_Characteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)onPacket, 2, true);
if(BuscaCAD) pRemote_CAD_Characteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)onPacket, 2, true);
notification = true;
}
}
delay(1000); // Delay a second between loops.
} // End of loop

shame_balamut
Posts: 1
Joined: Fri Jul 16, 2021 5:07 pm

Re: Not all BLEdevice around me are recognized by ES32 WROOM board

Postby shame_balamut » Fri Jul 16, 2021 5:10 pm

Same problem. No data from VC601 Ble Tpms

Who is online

Users browsing this forum: No registered users and 55 guests