How to use DataAvailableFcn in Bluetooth Low Energy

4 views (last 30 days)
Hi,
I'm struggling with DataAvailableFcn to receive data from Bluetooth Low Energy device (ESP32).
I put this function in endless loop
while 1 > 0
c.DataAvailableFcn = @getValue;
end
I got data only when I terminate .m script.
The second question is how to use DataAvailableFcn in appdesigner. There are private functions with app argument, for example:
connect(app)
However function that handle DataAbailableFcn should be like this:
function getValue(src, evt)
Thanks for any help in advance!

Accepted Answer

Dhruv
Dhruv on 24 Mar 2023
The reason for not receiving data from the Bluetooth Low Energy device using the ‘DataAvailableFcn’ is because the MATLAB program is unable to execute any other commands outside the loop. MATLAB has a single thread of execution, which means that it can only execute one task at a time. When entering an endless loop, MATLAB is not able to execute any other commands, including those related to Bluetooth communication.
To solve this issue, you can use the ‘pause’ function within the loop to allow MATLAB to execute other commands, including those related to Bluetooth communication.
For the second question regarding ‘DataAvailableFcn’ in App Designer, try defining the ‘getValue’ function as a private function within the App Designer class, and then set the ‘DataAvailableFcn’ property of the Bluetooth object to call that function. This can be done by using the app argument within the ‘getValue’ function to access the necessary properties of the App Designer class.
I hope this helps!

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!