Wio Terminal: WiFi and BLE in one sketch

I am trying to write a sketch that would control some BLE devices, but from time to time would also connect to Wifi to get some data from the internet.

The problem seems to be that connecting to wifi disconnects BLE and I after that I cannot recover BLE connection.

Here is what I do:
I connect to BLE server according to example code. This is working and I can communicate with server. Then I connect to Wifi. This works, but the BLE client gets disconnected and I cannot restore connection.

I tried disabling BLE before connecting to wifi and then re-enabling it, but it also doesnt help. Below is the code I tried. It works, but the scan callbacks never get called.

Any idea what I’m doing wrong? Has anyone managed to get something like this working?

bleClient->disconnect();
BLEDevice::deinit();

WiFi.mode(WIFI_STA);
WiFi.disconnect();

Serial.println("Connecting to WiFi..");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
    WiFi.begin(ssid, password);
}
Serial.println("Connected to the WiFi network");

WiFi.mode(WIFI_OFF);
WiFi.disconnect();
delay(100);

BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();

pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(30, false);

In case you have similar problem - after the whole day of tinkering I got it working. Even though I am frustrated to say, I don’t quite know how.
Basically, I copied exactly the code from https://create.arduino.cc/projecthub/Salmanfarisvp/the-new-wio-terminal-erpc-firmware-bfd8bd and it started working. No disconnects/deinits needed, both wifi and ble work side-by-side.