nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.36k stars 710 forks source link

When does "onRead" callback call? #819

Open MikhailNatalenko opened 5 years ago

MikhailNatalenko commented 5 years ago

I use BLECharacteristicCallbacks class for writing and reading characteristics.

I thought that onReadcallback calls before the actual reading moment, so you can set BLE value in this step, but it seems that onRead calls after actual reading. That thing prevents me to bind my model and characteristics.

Am i understand this thing well, and can you suggest us some of best practice for binding model and ble?

mpbejo commented 5 years ago

Hi I am new on BLE, but in my little experience, the onRead and the onWrite are events fired when You try to read and write the characteristics. I do this in my project: 1 the client write the characteristic on the server "Hey, somenthing new?" 2 the onWrite in the server is fired, if there is something new the server write the new value in the characterisic 3 after some milliseconds the client read the characteristics and have the new value.

MikhailNatalenko commented 5 years ago

I see. I forgot to explain what i want to achieve.

I have a model, that contains some value (just a class with data). And also there is a BLE value, that actually should present the model data. I can copy my model's data to BLE stack in loop for example, but i think it is not a good way. It would be more convenient to bind it somehow. And onWrite onRead can make this binding.

Advantages: Async binding, BLE just calls 'onWrite' 'onRead' and our model become updated "automatically"

Disadvantages: ??? Maybe it's convinent only for my purposes? I would like to know your opinion.

chegewara commented 5 years ago

@MikhailNatalenko You are correct, it was my mistake and onRead callback should be called before sending response. Here is code that needs to be changed to fix it in arduino and which seems to be fixed in this repo: https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/src/BLECharacteristic.cpp#L385-L407 https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/BLECharacteristic.cpp#L389-L414

MikhailNatalenko commented 5 years ago

@chegewara Oh. nice to hear it. Should i make a pull request? I'm working on a project with remote CI, it compiles firmware for me with standart libraries (platformio), so i can't solve this problem with haks.

Maybe i can help you to fix this bug ASAP

MikhailNatalenko commented 5 years ago

@chegewara I'm sorry, when this bugfix will appear in arduino repo? I really need that fix

chegewara commented 5 years ago

Hi @MikhailNatalenko sorry, but i dont maintain any longer BLE library.

vicatcu commented 4 years ago

fwiw, as of today, on 1.0.4 of the arduino-esp32 BSP, this seems to behave as one would expect. Before returning data to the client (i.e. phone) the onRead callback provides an opportunity for the firmware to modify the value of the characteristic (i.e. call pCharacteristc->setValue...) and that is what the client will receive.