Wio Terminal as a BLE Server (send a boolean with the buttons)

Hello,

I would like to program the Wio Terminal so that it sends by bluetooth low energy a bool value that is changed with the button. I have tried by modifying the example BLE_server from the Arduino rpcBLE library. The problem is that I need the line pCharacteristic->setValue inside the void loop instead of the void setup. But then pCharacteristic and thus pServer and pService have to be declared in the header. I have tried the following program but it makes the Wio Terminal disappearing from the available ports when compiling and I have to reset it.

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
*/

#include <rpcBLEDevice.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "180f"
#define CHARACTERISTIC_UUID "2a19"
#define DESCRIPTOR_UUID     "4545"


bool button_save = false; // a bool that becomes true when button B is pressed 
//                             and false when button C is pressed
int i=0; // the integer that is send by BLE for button_save

BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                       CHARACTERISTIC_UUID,
                                       BLECharacteristic::PROPERTY_READ |
                                       BLECharacteristic::PROPERTY_WRITE
                                     );
                                     
//class MyCallbacks: public BLECharacteristicCallbacks {
//    void onWrite(BLECharacteristic *pCharacteristic) {
//      std::string rxValue = pCharacteristic->getValue();
//
//      if (rxValue.length() > 0) {
//        Serial.println("*********");
//        Serial.print("Received Value: ");
//        for (int i = 0; i < rxValue.length(); i++)
//          Serial.print(rxValue[i]);
//
//        Serial.println();
//        Serial.println("*********");
//      }
//    }
//};

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

  pinMode(WIO_KEY_A, INPUT_PULLUP);
  pinMode(WIO_KEY_B, INPUT_PULLUP);
  pinMode(WIO_KEY_C, INPUT_PULLUP);

  while(!Serial){};
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");


  pCharacteristic->setAccessPermissions(GATT_PERM_READ | GATT_PERM_WRITE);
//  BLEDescriptor *pDescriptor = pCharacteristic->createDescriptor(
//                                         DESCRIPTOR_UUID,
//                                          ATTRIB_FLAG_VOID | ATTRIB_FLAG_ASCII_Z,
//                                         GATT_PERM_READ | GATT_PERM_WRITE,
//                                         2
//                                         );
  //pCharacteristic->setValue("Hello World says Neil");
  pCharacteristic->setValue(i);
  //pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();

  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() { 

  if (digitalRead(WIO_KEY_B) == LOW) {
    if (button_save == false){
      button_save = true;    
      Serial.println("true");
    }
  }

  if (digitalRead(WIO_KEY_A) == LOW) {
    if (button_save == true){
      button_save = false;    
      Serial.println("false");
      }      
      
  i=(int)button_save;
  pCharacteristic->setValue(i);
  //pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();
  }

}

Does anyone could help me?