Unable to set characteristics value twice (ESP32)

I’m having some issues when I’m setting a characteristics value more than once.
The use case for this is where I want to send notifications to a client with different data.
I’m able to set the value the first time, but always on the second attempt, it crashes.
Have anyone encounter this before?

I’m running on an ESP32 DevKit-C

You can see the basic test code below:

import streams
from espressif.esp32ble import esp32ble as bledrv
from wireless import ble

streams.serial()

# initialize BLE driver
bledrv.init()
ble.gap("Zerynth")

s = ble.Service(0xA001)
c = ble.Characteristic(0xA003, ble.NOTIFY,35,"AP Details",ble.BYTES)

s.add_characteristic(c)
ble.add_service(s)

ble.start()

ble.start_advertising()

while True:
    print(".")
    sleep(1000)

    b = bytearray([0xC8,0x03,0x03,0x44,0x26,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00])
    c.set_value(b)

On the first run in the while loop, it all goes well. But always on the second run, it crashed due to:
Thread 1 exited with exception IndexError at line 37 of __main__ raised at line 488 of wireless.ble.set_value

Hi @Aleksander_Aleksic
I tested the code, It works and sets the characteristics value up till 32 BYTES.
Just set the size of the characteristic to be 32 BYTES and modify the bytes accordingly and It should work.

b=bytearray([0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC6,0xC8])

c.set_value(b)

You can deal with 32 Bytes at a time, I don’t know why exactly but I think this is a limitation in the ESP-IDF

1 Like

Ahaa, @karimhamdy1 that clarifies it!
So if want to send 35 bytes i need to send 32 and then the remaining 3 bytes?

Make another 32 bytes and send them if needed.
Try this and let me know if this works.

while True:
    print(".")
    sleep(1000)

    array_1 = bytearray([0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC6,0xC8])
    c.set_value(array_1)
    print("Sent all values of array_1")
    array_2 = bytearray([0x55,0xAA,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC8,0xC6,0xC5,0x66,0x18,0x36,0xC8,0xC6,0xC8,0xC6,0xC6,0xC8])
    c.set_value(array_2)
    
    print("Sent all values of array_2")

It works, but I still think it’s odd that the first time I’m able to send 35 bytes, but not the second time.