How to take ESP32 BLE Notify

HuyYuh
Posts: 2
Joined: Tue Mar 22, 2022 2:31 am

How to take ESP32 BLE Notify

Postby HuyYuh » Wed Jun 01, 2022 2:57 pm

Hello, I'm trying to take data whenever BLE Server noyify, here is my callback function

Code: Select all

static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic,
                            uint8_t* pData, size_t length, 
                            bool isNotify)
{
  Serial.print("Length: ");
  Serial.println(length);
  Serial.print("pData :");
  Serial.println((char *)pData);
  char temp[length];
  for (int i=0; i<length; i++) {
    temp[i] = (char)pData[i];
  }
  Serial.print("temp: ");
  Serial.println(temp);
}
However, I cannot take notify data:
Length: 3
pData :chi⸮⸮?
temp: chi⸮⸮⸮?⸮⸮⸮?8⸮⸮?⸮Z
⸮⸮⸮⸮?
Length: 3
pData :hay⸮⸮?
temp: hay⸮⸮⸮?⸮⸮⸮?8⸮⸮?⸮Z
⸮⸮⸮⸮?
Length: 9
pData :heyGoogle
temp: heyGoogle⸮⸮?8⸮⸮?⸮Z
⸮⸮⸮⸮?
Length: 2
pData :hiyGoogle
temp: hi
⸮⸮⸮?⸮⸮⸮?8⸮⸮?⸮Z
⸮⸮⸮⸮?
Length: 5
pData :motayogle
temp: motay⸮⸮?⸮⸮⸮?8⸮⸮?⸮Z
⸮⸮⸮⸮?
Length: 6
I don't know how to do it. Please help me.
Thanks in advance

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to take ESP32 BLE Notify

Postby chegewara » Thu Jun 02, 2022 6:34 am

From what i can see it is working good. The problem is in your code.

You need to create array with size length + 1, to have null terminated string.

Who is online

Users browsing this forum: Baidu [Spider] and 76 guests