Skip to content

Adding - removing characteristic on runtime issue #711

Open
@sanastasiou

Description

@sanastasiou

Hi @h2zero , thanks a lot for your library.

I came to notice an issue, while adding a characteristic / removing it on runtime.

While the service appears to be updated, the advertisement data is not.

Here is a small example:

  // setup non encrypted characteristics
    fPServiceCharacteristics[CHARACTERISTICS_INDEX::NON_ENCRYPTED] = addCharacteristicToService(constants::NON_ENCRYPTED_CHARACTERISTIC_UUID.c_str(), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
    fPServiceCharacteristics[CHARACTERISTICS_INDEX::NON_ENCRYPTED]->setCallbacks(new ble::PingCallbacks());

    //start service
    fPKillswitchService->start();

    fPServer->getAdvertising()->addServiceUUID(constants::SERVICE_UUID.c_str());
    fPServer->getAdvertising()->setAppearance(576U);
    fPServer->getAdvertising()->setScanResponse(true);

NimBLECharacteristic *BluetoothController::addCharacteristicToService(const char *uuid, const uint32_t properties)const
{
    NimBLECharacteristic *pCharacteristic = fPKillswitchService->createCharacteristic(
        uuid,
        properties);
    return pCharacteristic;
}

//later... 
void BluetoothController::addEncryptedCharacteristics()
{
    if(!fEncryptedServicesStarted)
    {
        while(false == NimBLEDevice::stopAdvertising());
        NimBLEDevice::getAdvertising()->removeServices();
        //adding encrypted characteristics
        fPServiceCharacteristics[CHARACTERISTICS_INDEX::ENCRYPTED] = addCharacteristicToService(constants::ENCRYPTED_CHARACTERISTIC_UUID.c_str(), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::INDICATE | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_AUTHEN | NIMBLE_PROPERTY::WRITE_ENC);
        fPServiceCharacteristics[CHARACTERISTICS_INDEX::ENCRYPTED]->setCallbacks(new ble::BatteryLockCallbacks(fBatteryLocked));
        fPServiceCharacteristics[CHARACTERISTICS_INDEX::ENCRYPTED]->setValue(fBatteryLocked);
        const auto v = fPKillswitchService->getCharacteristics();
        for(auto c: v)
        {
            std::cout << "Characteristics in service: [" << c->getUUID().toString() << "]" << std::endl;
        }
        std::cout << "Transmitting encrypted characteristics started." << std::endl;
        fEncryptedServicesStarted = true;
        NimBLEDevice::getAdvertising()->addServiceUUID(constants::SERVICE_UUID.c_str());
        while(false == NimBLEDevice::startAdvertising());
    }
}

D NimBLEService: >> start(): Starting service: UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0xffff
D NimBLEService: Adding 1 characteristics for service UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0xffff
D NimBLEService: << start()
Bluetooth initialization done...
FSM -> 0 -> Init().
Initializing display controller
SSD1306 initialized properly...
DeferredAdController::start()
DeferredAdController::start() - fTransitionTimeout.reset()
Number of bonded devices: 0
FSM -> Init() -> Pair().
Real voltage: 17.41 V
Is advertising: false
Is connected: false
Is bonded: false
Ping value: 0
Encrypted communications started: false
DeferredAdController::trace() : State: 1 - Previous State: 0 - transition timer: 0
LedControl::update() - state: Init
D NimBLEAdvertising: >> stop
D NimBLEAdvertising: << stop
Characteristics in service: [82c314b2-e53a-4d5e-b021-fc5887b4373f]
Characteristics in service: [3c5577d7-d182-482a-940a-7e5062441f43]
Transmitting encrypted characteristics started.
D NimBLEAdvertising: >> Advertising start: customAdvData: 0, customScanResponseData: 0
primary service
uuid 0x1800
handle 1
end_handle 5
characteristic
uuid 0x2a00
def_handle 2
val_handle 3
min_key_size 0
flags [READ]
characteristic
uuid 0x2a01
def_handle 4
val_handle 5
min_key_size 0
flags [READ]
primary service
uuid 0x1801
handle 6
end_handle 9
characteristic
uuid 0x2a05
def_handle 7
val_handle 8
min_key_size 0
flags [INDICATE]
ccc descriptor
uuid 0x2902
handle 9
min_key_size 0
flags [READ|WRITE]
primary service
uuid 00002ffa-0000-1000-8000-00805f9b34fb
handle 10
end_handle 13
characteristic
uuid 82c314b2-e53a-4d5e-b021-fc5887b4373f
def_handle 11
val_handle 12
min_key_size 0
flags [READ|NOTIFY]
ccc descriptor
uuid 0x2902
handle 13
min_key_size 0
flags [READ|WRITE]
D NimBLEAdvertising: << Advertising start

So the service, has indeed the two characteristics added, but the advertisement data does not.

Do you know what the proper way to add/remove a characteristic during runtime is?

Best Regards!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions