How to turn LED ON via BLE from ios app?

Here is the scenario. I have an esp32, 2 led's and a ios example app I found online here. Currently If I press a button on my esp32 it notifies the ios app to display "1", if pressed again it displays "0". This works perfectly.

The tricky part is that this ios app allows me to send a write command to the esp32. I'd like to make it so if '1' is sent LED A turns ON and LED B turns OFF, then LED A OFF and LED B ON when 0 is sent. I am unable to do this though. Try as I might I can't figure out where in the chain of this project something is wrong. Maybe the code on the esp32 or maybe the app i'm unsure.

Here is my arduino code. (There is more to the code not mentioned, I actually have 4 led's but I only want to turn on 2 certain ones when a write command is sent).

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

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
boolean oldState = LOW;
uint32_t value = 0;

#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;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.print("Received Value: ");

        for (int i = 0; i < rxValue.length(); i++) {
          Serial.print(rxValue[i]);
        }

        Serial.println();
        
        if (rxValue.find("1") != -1) {
          digitalWrite(13, HIGH);
          digitalWrite(27, LOW);
        }
        else if (rxValue.find("0") != -1) {
          digitalWrite(13, LOW);
          digitalWrite(27, HIGH);
        }
      }
    }
};

const int bt1 = 14;
boolean bt1g = true;
int bt1t = 0;


void setup() {
  pinMode(13, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(bt1, INPUT_PULLUP);
  Serial.begin(9600);

  BLEDevice::init("ESP32");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  pCharacteristic->addDescriptor(new BLE2902());

  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0);
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");
}

void loop()
{


  if (bt1g) {
    if (digitalRead(bt1) == LOW ) {
      bt1t = (bt1t + 1) % 2;
      Serial.println(bt1t);
      bt1g = false;

    }
  }
  if (!bt1g) {
    if (digitalRead(bt1) == HIGH) {
      bt1g = true;
    }
  }

  if (bt1t == 0) {
    digitalWrite(15, LOW);
    digitalWrite(33, HIGH);

  }
  }

  boolean newState = digitalRead(15);

  if (deviceConnected) {
    if (newState != oldState) {
      if (newState == LOW) {
        pCharacteristic->setValue("1");

      }
      else {
        pCharacteristic->setValue("0");

      }
      pCharacteristic->notify();
    };

    oldState = newState;

  }
  delay(50);
}

It looks like the entire code for the ios app is too long to submit to this post so here is the github

I'm really unsure and stuck in a rut. Any help is appreciated!

It looks like you never register your MyCallbacks.
Try adding pCharacteristic->setCallbacks(new MyCallbacks()).

Pieter

PieterP:
It looks like you never register your MyCallbacks.
Try adding pCharacteristic->setCallbacks(new MyCallbacks()).

Pieter

This along with some other small tweaks solved it

guanciale:
This along with some other small tweaks solved it

Could you post your solution so this thread is useful to others as well?