bluetoothBLE StringsReceived not receiving data

AC_Protect.aia (191.8 KB)

I am learning how to use bluetoothBLE to exchange information with ESP32.
I followed what I found in my searches but the bluetoothBLE.stringsReceived is not displaying the data sent from ESP32.
When I press Read(meant "send to ESP32" :frowning: ) I receive the message on the ESP32.
When I connect using nrf connect for mobile, I do see the message sent from the ESP32.

the esp32 code:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb52ae1-36e1-4688-b7f5-ea07361b26a8"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();
      if (value.length() > 0) {
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);
        Serial.println();
      }
    }
};

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

  BLEDevice::init("MyESP32");
  pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );
  pCharacteristic->setCallbacks(new MyCallbacks());
  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

uint32_t value=0;
void loop() {
  // put your main code here, to run repeatedly:
  char toSend[100];
  sprintf(toSend,"%s:%d","Value: ",value);
    pCharacteristic->setValue((char *)toSend);
    pCharacteristic->notify();
    Serial.println(toSend);
  delay(5000);
  value++;
}

If you are not receiving strings in AI2, it might be because you
did not register for strings in BLE on the AI2 side.

You only need to do it once per run.

Thank you ABG, this fixed it. So I need to register for any data type before I use it.
Where is the best place to register the data types?
Appreciate the help

Well, Screen1.Initialize is probably too early, because you haven't yet set up communication with anything in BLE yet.

Avoid doing it in anything repetitive, like Clock Timers, because they pile up and can clog .

Look for the events that tell you that you connected successfully.

Thank you. I tried it in .connected

Hello Sam

The Bluetooth packets of data are of limited size, typically 32 bytes, not all of which is actual payload. Given that, is there any point in prefixing the word "Value" to your data? It's more work for the App too. The Data from sprintf() will look like this:

Value: :1
.
What's happening with the CallBack - is that the real code to be used once you have got the test working?

It's best not to use Delay() in most circumstances because everything is blocked, but for the purposes of your test it's OK.

Thank you Chris for your assistance. This code is for me learning how to use ESp32 and android to talk over bluetooth BLE. because I am using one characteristics, the first part of the message is the type of message ( the Value: :1 was for testing only). The delay is only for testing.
I will be sending AC voltages, currents every 15 seconds, 1 minute average every minute, and faults when they occur. But before I incorporate into my program I decided to try it in a smaller program, to better learn it. I am still deciding if it is easier to use one characteristics UUID and prepend the message with a Type field, e.g. [V], or [A], etc... Most messages will have around 12 characters. the Faults and averages will have a timestamp in addition to the values.
I am starting today to search for a good tutorial on callbacks in ESP32 Bluetooth BLE library.

Again thank you for the insight