h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
724 stars 153 forks source link

Adding - removing characteristic on runtime issue #711

Open sanastasiou opened 3 months ago

sanastasiou commented 3 months ago

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!

sanastasiou commented 3 months ago

Is it possible that this is not implemented?

image

h2zero commented 3 months ago

There is no provision the the BLE spec to advertise characteristics, only services.

sanastasiou commented 3 months ago

@h2zero thanks for replying, but even if I connect to the actual service although it contains both characteristics only one is there.

    const auto v = fPKillswitchService->getCharacteristics();

    for(auto c: v)
    {
        std::cout << "Characteristics in service: [" << c->getUUID().toString() << "]" << std::endl;
    }

The above code prints correctly both characteristics, but the client only sees one. Could u explain that?

signal-2024-08-19-162217
h2zero commented 3 months ago

You need to call service::start() after adding the characteristics. Please make sure there are no devices connected when you do this.

sanastasiou commented 3 months ago

You mean disconnect all clients, and call start again although I have already done that ? And same when removing a characteristic?

h2zero commented 3 months ago

Yes, the attributes cannot be changed while there are active connections because the handles need to be changed.

sanastasiou commented 2 months ago

Hi @h2zero , after following your advice and implementing against it I can now verify that adding and removing characteristics dynamically works perfectly fine.

Many thanks to you sir!

sanastasiou commented 2 months ago

Hi @h2zero , unfortunately I need to reopen this issue. While the adding/removing characteristics is now properly displayed in the service, the characteristics themselves stop working after one is removed.

My code service is pretty simple. It concists of one characteristic which is always present and never removed. And an encrypted characteristic which is added or removed on the fly. Following thing happens:

Basically, the moment I call service->removeCharacteristic(...) while no device is connected, everything breaks.

What I tried:

Removing characteristic with the optional boolen to delete it with true/false. No difference. Calling service->start() after removing the characteristic. No difference. Not calling service->start() after removing the characteristic. No difference.

Would you happen to have any ideas as to what I might be doing wrong here?

`

bool ServiceHandler::removeEncrypedCharacteristics()
{
    if(fPServer->getConnectedCount() == 0)
    {
        fPKillswitchService->removeCharacteristic(fPServiceCharacteristics[CHARACTERISTICS_INDEX::ENCRYPTED], true);
        //fPKillswitchService->start();
    }
    const auto v = fPKillswitchService->getCharacteristics();
    for(auto c: v)
    {
        std::cout << "ServiceHandler::removeEncrypedCharacteristics() - Characteristics in service: [" << c->getUUID().toString() << "]" << std::endl;
        if(0 == strcmp(c->getUUID().toString().c_str(), constants::ENCRYPTED_CHARACTERISTIC_UUID.c_str()))
        {
            return false;
        }
    }
    return true;
}

`

sanastasiou commented 2 months ago

Tried even removing the service itself. Did not help either. Only destroying the reinitializing the entire stack works:

        fPKillswitchService->removeCharacteristic(fPServiceCharacteristics[CHARACTERISTICS_INDEX::ENCRYPTED], true);
        fPServer->removeService(fPKillswitchService, true);
        NimBLEDevice::deinit(true);
        NimBLEDevice::init(constants::DEVICE_NAME);
        NimBLEDevice::setPower(ESP_PWR_LVL_P9);
        fPServer = NimBLEDevice::createServer();
        fPServer->setCallbacks(new ServerCallbacks(fDevicePin, fBleController));

        fPKillswitchService = fPServer->createService(constants::SERVICE_UUID.c_str());

        // setup non encrypted characteristics
        fPServiceCharacteristics[CHARACTERISTICS_INDEX::NON_ENCRYPTED] = fPKillswitchService->createCharacteristic(constants::NON_ENCRYPTED_CHARACTERISTIC_UUID.c_str(), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::INDICATE);
        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);
        fPServer->getAdvertising()->start();

After this, the first characteristic still works fine, callbacks, subscription etc. work. For me clearly some issue here but I can live with this workaround.

h2zero commented 2 months ago

Thanks, seems there may be a bug in this case. I will need to look into this.

sanastasiou commented 2 months ago

@h2zero no problem, would be happy to test any bug fix you might have.

h2zero commented 2 months ago

Could you post some logs at debug level? That would help a lot.

sanastasiou commented 2 months ago

Hi, sorry for replying late. WIll try to post the debug output today!

sanastasiou commented 1 month ago

So here are some logs. Now I am not sure if this is an Android or a bug in the stack.. So basically what happens is this:

Both devices are already bonded ( that works fine ) and after bonding all encrypted callbacs etc. work fine. I can read/write/subscribe without any issues.

Now, when a device disconnects, I restart the stack with only the non encrypted characteristic. If a device is bonded I then add the encrypted characteristic, after disconnecting the bonded device. When the device connects again it should see the encryped characteristic.

signal-2024-10-11-142714_002

Here is an image of iPhone light Blue.. works fine.

Then I try the same with Android ( Api level 33 )

signal-2024-10-11-142714_003

Only the non encryped characteristic is shown ( 82c314....373f )

Same code.. works only for iOS?

Here's the complete debug output:

D NimBLEDevice: Setting bonding: 1, mitm: 1, sc: 1
D NimBLEDevice: >> setLocalMTU: 512
D NimBLEDevice: << setLocalMTU
D NimBLEServer: >> createService - 00002ffa-0000-1000-8000-00805f9b34fb
D NimBLEServer: << createService
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()
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|INDICATE]
ccc descriptor
           uuid 0x2902
         handle 13
   min_key_size 0
          flags [READ|WRITE]
D NimBLEAdvertising: << Advertising start
Bluetooth initialization done...

Initializing display controller
SSD1306 initialized properly...
Number of bonded devices: 2
Found bonded ble client with address:  264279940250405
Found bonded ble client with address:  106097662453008
Real voltage:      17.41 V
Is advertising: true
Is connected: false
Is bonded: false
Number of peer devices: 0
Ping value: 0
Encrypted communications started: false
LedControl::update() - state: Init
BluetoothController::update() - switch from: State_Init to: State_Disconnected
Real voltage:      17.41 V
Is advertising: true
Is connected: false
Is bonded: false
Number of peer devices: 0
Ping value: 500
Encrypted communications started: false
LedControl::update() - state: Disconnected
D NimBLEServer: >> handleGapEvent: 
D NimBLEServerCallbacks: onConnect(): Default
D NimBLEAdvertising: >> stop
D NimBLEAdvertising: << stop
ServiceHandler::onDeviceConnected() Stop advertising.
Client with address: 106097662453008 connected.
BluetoothController::disconnectedStateHandler() - fServiceHandler.isConnectedDeviceBonded(): true
BluetoothController::update() - switch from: State_Disconnected to: State_Disconnecting_StartEncryptedComms
D NimBLEServer: >> disconnect()
D NimBLEServer: << disconnect()
D NimBLEServer: >> handleGapEvent: 
D NimBLEServerCallbacks: onDisconnect(): Default
ServerCallbacks::onDisconnect() - Client with address: 106097662453008 disconnected. Start advertising.
D NimBLEAdvertising: >> Advertising start: customAdvData: 0, customScanResponseData: 0
D NimBLEAdvertising: >> stop
D NimBLEAdvertising: << Advertising start
D NimBLEAdvertising: << stop
D NimBLEService: >> start(): Starting service: UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0x000a
D NimBLEService: Adding 2 characteristics for service UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0x000a
D NimBLEService: << start()
ServiceHandler::addEncryptedCharacteristics() - Characteristics in service: [82c314b2-e53a-4d5e-b021-fc5887b4373f]
ServiceHandler::addEncryptedCharacteristics() - Characteristics in service: [3c5577d7-d182-482a-940a-7e5062441f43]
D NimBLEAdvertising: >> Advertising start: customAdvData: 0, customScanResponseData: 0
primary service
           uuid 0x1800
         handle 14
     end_handle 18
characteristic
           uuid 0x2a00
     def_handle 15
     val_handle 16
   min_key_size 0
          flags [READ]
characteristic
           uuid 0x2a01
     def_handle 17
     val_handle 18
   min_key_size 0
          flags [READ]
primary service
           uuid 0x1801
         handle 19
     end_handle 22
characteristic
           uuid 0x2a05
     def_handle 20
     val_handle 21
   min_key_size 0
          flags [INDICATE]
ccc descriptor
           uuid 0x2902
         handle 22
   min_key_size 0
          flags [READ|WRITE]
primary service
           uuid 00002ffa-0000-1000-8000-00805f9b34fb
         handle 23
     end_handle 29
characteristic
           uuid 82c314b2-e53a-4d5e-b021-fc5887b4373f
     def_handle 24
     val_handle 25
   min_key_size 0
          flags [READ|NOTIFY|INDICATE]
ccc descriptor
           uuid 0x2902
         handle 26
   min_key_size 0
          flags [READ|WRITE]
characteristic
           uuid 3c5577d7-d182-482a-940a-7e5062441f43
     def_handle 27
     val_handle 28
   min_key_size 0
          flags [READ|WRITE|NOTIFY|INDICATE|READ_ENC|READ_AUTHEN|WRITE_ENC|WRITE_AUTHEN]
ccc descriptor
           uuid 0x2902
         handle 29
   min_key_size 0
          flags [READ|WRITE]
D NimBLEAdvertising: << Advertising start
BluetoothController::update() - switch from: State_Disconnecting_StartEncryptedComms to: State_Disconnected
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristic: << notify: No clients subscribed.
D NimBLEServer: >> handleGapEvent: 
D NimBLEServerCallbacks: onConnect(): Default
D NimBLEAdvertising: >> stop
D NimBLEAdvertising: << stop
ServiceHandler::onDeviceConnected() Stop advertising.
Client with address: 106097662453008 connected.
BluetoothController::disconnectedStateHandler() - fServiceHandler.isConnectedDeviceBonded(): true
BluetoothController::update() - switch from: State_Disconnected to: State_Connected
D NimBLEServer: >> handleGapEvent: 
ServerCallbacks::onAuthenticationComplete() - encrypted: true
ServerCallbacks::onAuthenticationComplete() - authenticated: true
ServerCallbacks::onAuthenticationComplete() - device address type: 0
ServerCallbacks::onAuthenticationComplete() - bonded: true
ServerCallbacks::onAuthenticationComplete() - Client with address: 106097662453008 is authenticated.
D NimBLEServer: >> handleGapEvent: 
I NimBLEServer: subscribe event; attr_handle=21, subscribed: false
D NimBLEServer: >> handleGapEvent: 
I NimBLEServer: subscribe event; attr_handle=28, subscribed: true
I NimBLECharacteristic: New subscribe value for conn: 0 val: 1
D NimBLECharacteristicCallbacks: onSubscribe: default
D NimBLEServer: >> handleGapEvent: 
I NimBLEServer: mtu update event; conn_handle=0 mtu=512
D NimBLEServerCallbacks: onMTUChange(): Default
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristicCallbacks: onNotify: default
D NimBLEServer: >> handleGapEvent: 
D NimBLECharacteristicCallbacks: onStatus: default
D NimBLECharacteristic: << notify
D NimBLECharacteristic: >> setValue: length=11, data=e803000005440000000100, characteristic UUID=82c314b2-e53a-4d5e-b021-fc5887b4373f
D NimBLECharacteristic: << setValue
D NimBLECharacteristic: >> notify: length: 11
D NimBLECharacteristic: << notify: No clients subscribed.
Real voltage:      17.41 V
Is advertising: false
Is connected: true
Is bonded: true
Number of peer devices: 1
Ping value: 1000
Encrypted communications started: true
LedControl::update() - state: Connected
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristicCallbacks: onNotify: default
D NimBLEServer: >> handleGapEvent: 
D NimBLECharacteristicCallbacks: onStatus: default
D NimBLECharacteristic: << notify
D NimBLECharacteristic: >> setValue: length=11, data=1a04000005440000000100, characteristic UUID=82c314b2-e53a-4d5e-b021-fc5887b4373f
D NimBLECharacteristic: << setValue
D NimBLECharacteristic: >> notify: length: 11
D NimBLECharacteristic: << notify: No clients subscribed.
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristicCallbacks: onNotify: default
D NimBLEServer: >> handleGapEvent: 
D NimBLECharacteristicCallbacks: onStatus: default
D NimBLECharacteristic: << notify
D NimBLECharacteristic: >> setValue: length=11, data=4c04000005440000000100, characteristic UUID=82c314b2-e53a-4d5e-b021-fc5887b4373f
D NimBLECharacteristic: << setValue
D NimBLECharacteristic: >> notify: length: 11
D NimBLECharacteristic: << notify: No clients subscribed.
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristicCallbacks: onNotify: default
D NimBLEServer: >> handleGapEvent: 
D NimBLECharacteristicCallbacks: onStatus: default
D NimBLECharacteristic: << notify
D NimBLECharacteristic: >> setValue: length=11, data=7e04000005440000000100, characteristic UUID=82c314b2-e53a-4d5e-b021-fc5887b4373f
D NimBLECharacteristic: << setValue
D NimBLECharacteristic: >> notify: length: 11
D NimBLECharacteristic: << notify: No clients subscribed.
D NimBLECharacteristic: >> notify: length: 1
D NimBLECharacteristicCallbacks: onNotify: default
D NimBLEServer: >> handleGapEvent: 
D NimBLECharacteristicCallbacks: onStatus: default
D NimBLECharacteristic: << notify
D NimBLECharacteristic: >> setValue: length=11, data=b004000005440000000100, characteristic UUID=82c314b2-e53a-4d5e-b021-fc5887b4373f
D NimBLECharacteristic: << setValue
D NimBLECharacteristic: >> notify: length: 11
D NimBLECharacteristic: << notify: No clients subscribed.
D NimBLEServer: >> handleGapEvent: 
I NimBLEServer: subscribe event; attr_handle=21, subscribed: false
D NimBLEServer: >> handleGapEvent: 
I NimBLEServer: subscribe event; attr_handle=28, subscribed: false
I NimBLECharacteristic: New subscribe value for conn: 0 val: 0
D NimBLECharacteristicCallbacks: onSubscribe: default
D NimBLEServer: >> handleGapEvent: 
D NimBLEServerCallbacks: onDisconnect(): Default
ServerCallbacks::onDisconnect() - Client with address: 106097662453008 disconnected. Start advertising.
D NimBLEAdvertising: >> Advertising start: customAdvData: 0, customScanResponseData: 0
BluetoothController::update() - switch from: State_Connected to: State_Disconnecting_StopEncrypedComms
D NimBLEAdvertising: << Advertising start
D NimBLEAdvertising: >> stop
D NimBLEAdvertising: << stop
D NimBLEService: >> start(): Starting service: UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0x0017
D NimBLEService: Adding 1 characteristics for service UUID: 00002ffa-0000-1000-8000-00805f9b34fb, handle: 0x0017
D NimBLEService: << start()
I NimBLEDevice: BLE Host Task Started
I NimBLEDevice: NimBle host synced.
D NimBLEDevice: >> setPower: 7 (type: 11)
D NimBLEDevice: << setPower
Server BLE address: C8:C9:A3:CA:14:22
Device BLE Pin: 060927
D NimBLEDevice: Setting bonding: 1, mitm: 1, sc: 1
D NimBLEDevice: >> setLocalMTU: 512
D NimBLEDevice: << setLocalMTU
D NimBLEServer: >> createService - 00002ffa-0000-1000-8000-00805f9b34fb
D NimBLEServer: << createService
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()
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|INDICATE]
ccc descriptor
           uuid 0x2902
         handle 13
   min_key_size 0
          flags [READ|WRITE]
D NimBLEAdvertising: << Advertising start
ServiceHandler::removeEncryptedCharacteristics() - Characteristics in service: [82c314b2-e53a-4d5e-b021-fc5887b4373f]
D NimBLEAdvertising: >> Advertising start: customAdvData: 0, customScanResponseData: 0
W NimBLEAdvertising: Advertising already active
BluetoothController::update() - switch from: State_Disconnecting_StopEncrypedComms to: State_Disconnected
Real voltage:      17.41 V
Is advertising: true
Is connected: false
Is bonded: false
Number of peer devices: 0
Ping value: 1500
Encrypted communications started: false
h2zero commented 1 month ago

Thanks, looks like an android issue, or app issue there. The client device should subscribe to the service changed indication and request the database from the peripheral when it receives this indication, doesn't look like android is doing that.

sanastasiou commented 1 month ago

The thing is however, the client has to disconnect in order to add the new characteristic right? If it does that, how should it subscribe to this event?

The way ESP32 is implemented right now is:

Exactly this works in iOS but not on Android.

Android used to work, when I was adding the encrypted characteristic without disconnecting it first. So quite confused now.

h2zero commented 1 month ago

When the device bonds the esp32 stores the subscription in NVS and will send the notification upon reconnecting