Esp32-snippets: BLEAdvertising stop doesn't stop advertising

Created on 29 Jan 2019  路  11Comments  路  Source: nkolban/esp32-snippets

I have a simple application that I want to be able to start and stop advertising so that other devices are prevented from connecting when I don't want them too. This needs to be turned on and off at runtime.

Here is my startup code:

```BLEDevice::init("MyName");

pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pServer->setCallbacks(new BTServerCallbacks());

pCharacteristicCommand = pService->createCharacteristic(
COMMAND_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);

pCharacteristicCommand->setCallbacks(new BTCallbacks());
pCharacteristicCommand->setValue("");
pCharacteristicCommand->addDescriptor(new BLE2902());

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


This works great and my service advertises and I am able to connect with it and process things.

Then when I want my device to become unavailable I do this:

    BLEAdvertising *pAdvertising = pServer->getAdvertising();
    pAdvertising->stop();

```

It calls it and doesn't give any errors or exceptions, but my device is still visible to my iPhone and I can still connect to it.

How do I disable advertising and connecting during runtime?

bug

Most helpful comment

All 11 comments

Are you sure you run new scan, or is esp32 visible from old BLE scan?
I dont know how iPhone works, but for example on android nRF connect scan lasts few seconds and then stops. All discovered devices are still visible in nRF connect list and can be connected even if you stop advertising on them, unless peer device will disappear or do not allow to be connected.

I think this is because start() is called in ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT in cpp_utils/BLEAdvertising.cpp Line 496.

I have same issue.
And as Azrie-Samson mentioned, I tried with successfully.

any known workaround for this?

Facing the same problem, I start BLE advertising and after 300 ms I try to stop with
pAdvertising->stop(). Doesnt work.

Just delete this line:
https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/BLEAdvertising.cpp#L496

I commected Still it is discovering to my PC

i added stop in void onDisconnect(BLEServer* pServer) function
BLEAdvertising *pAdvertising = pServer->getAdvertising();
//pAdvertising->start();
pAdvertising->stop();

and commented start() function but still advertising to my PC so what i have to do please help me on this to stop advertising.
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {

        //start();
        break;
    }

If you comment out this line (start()) and use advertising->stop(), then esp32 should stop advertising.

I added stop() function like below ,i am putting my client in sleep mode so stopping the advertising now and LOG(ESP_LOGI(LOG_TAG, "STOP advertising"); )is printing continuously,how can i reconnect automatically when client is wakes from sleep.

case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
ESP_LOGI(LOG_TAG, "STOP advertising");
//start();
stop();
break;
}

In older versions of Arduino framework for ESP32 at BLEServer.cpp check the BLEServer::handleGATTServerEvent function at : case of ESP_GATTS_DISCONNECT_EVT. there is a line: startAdvertising(); //- do this with some delay from the loop() what causes the restart of BLE after calling the stop succesfully.

The recommended solution is to update your Arduino Framework. In the current 1.0.6 version it is fixed, or you can just comment this line out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

d3cline picture d3cline  路  4Comments

vishnunaik picture vishnunaik  路  6Comments

frankipl picture frankipl  路  8Comments

monteslu picture monteslu  路  4Comments

Tavo7995 picture Tavo7995  路  7Comments