Mo G.
Mo G.

Reputation: 21

ESP32_BLE_Arduino | BLEAdvertisedDevice.getServiceData() does not return what i expect

Iam stuck in the trial to setup a esp as a BLE amplifier. Iam trying to fetch the broadcasted/ advertised data of a sensorbeacon (Sent under a service) and to advertise this same data for a short time. The whole process should work without a connection to the Client.

The problem is that iam not able to correctly read the data from the sensorbeacon (client). Iam running a scanner on a Rpi3 with noble to check and here everything works as expected. The noble output of

noble.on('discover', async function(bleResult) {

if(bleResult.advertisement.localName 
  && bleResult.advertisement.localName.localeCompare("RDL52832") == 0 {
       console.log(bleResult.advertisement.serviceData);
}
...

is

[ { uuid: '0318',
    data: <Buffer 13 1b 2e 2f 01 00 00 02 00 00 00 01 00 00 09 07> } ]

The Code to fetch the same data on the ESP32 looks as

BLEScanResults foundDevices = pBLEScan->start(scanTime, true);

  for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
    if (foundDevices.getDevice(i).getName() == ("RDL52832")) {
       Serial.printf("\nAddress: %s \n", foundDevices.getDevice(i).getAddress().toString().c_str());
       Serial.printf("ServiceData as Hex: %X \n", foundDevices.getDevice(i).getServiceData().c_str());
       startServerWithCharacteristics(foundDevices.getDevice(i).getServiceData(), "ant_node");
    };
  }

The ouptut of this code is

Address: e9:81:f5:86:63:a5 
ServiceData as Hex: 3FFE7514 
Try Broadcasting ant_node 

How can i get the advertisedData from the beacon on the ESP32? Where is my mistake?

Thanks in advance!

Upvotes: 2

Views: 1707

Answers (1)

Łukasz W
Łukasz W

Reputation: 21

I had similar problem with Xiaomi Ble Gateway on ESP32. My assumption was that problem is somewhere inside BLEAdvertisedDevice class. I found out that getServiceData() return string and that can be problematic.

/**
* @brief Get the service data.
* @return The ServiceData of the advertised device.
*/
std::string BLEAdvertisedDevice::getServiceData() {
return m_serviceData;
} //getServiceData

If there is a 0x0 in payload then string will be terminated and You don't get full payload.

It was my case and it also looks pretty much as Yours.

My solution for that was:

uint8_t cServiceData[100];
uint8_t* payloadPtr = advertisedDevice.getPayload();

for (int i = 0; i < advertisedDevice.getPayloadLength(); i++)
{
    cServiceData[i] = *(payloadPtr + i);
}

I hope it also works for You.

Upvotes: 2

Related Questions