Open Jah-On opened 1 month ago
Source of issue #311.
I tested switching to a pass by reference for the callbacks and it had no impact on the results which makes sense since the internal objects is a shared_ptr
. I'm not sure if I'll be able to contribute a fix.
It seems like using Safe::Adapter and Safe::Peripheral for the callbacks and variables remedies the issue. I guess the issue can be closed?
I'm curious why both the Safe and normal APIs exist for C++. I think it would make a lot more sense to have the Safe API be the normal one which would minimize these sorts of issues.
Hey @Jah-On, thanks for the report!
I'll reopen the issue to make sure I do a once-over to see what can be improved.
Calling any member function on the
Peripheral
passed into the callback does not do anything or behaves incorrectly. Here is a minimal example of it not working:With a valid
Peripheral
instance, this code should send "Hello world!" to the NUS and print out the response from the device. However, when using the callback instance, it does not do anything.The reason why this does not work is because SimpleBLE creates a clone of the Peripheral instance using the
default
constructor. Thus, the underlying back-end handles are no longer valid and the functions don't do anything.Possible solutions
Personally, I think the callbacks should use pass by reference instead of a cloned constructor. If this can't be done, then a proper cloning constructor should be implemented to ensure the back-end handles are swapped or re-validated.
I can work on this myself unless a different maintainer wants to do it.