Problems with function handle in Appdesigner

8 views (last 30 days)
I am currently trying to create my first GUI with Appdesigner. I have a Polar Chest Strap (BLE) that I am reading out. A working (plain)Matlab minimal example is below.
Unfortunately, trying to import the functionality into Appdesigner fails, maybe because I have not done anything with object-oriented programming so far. The strap is recognized after the scan and I can connect; if data is pending, the specified function handle would have to be called and the light would have to be set to green. But this does not happen. Maybe someone can help me.
Here again the functional minimal example in Matlab:
clear all
clc
%% Connect
h10 = ble("EE3CC81051AD"); % Connect to Polar H10
hr_char = characteristic(h10,"Heart Rate","Heart Rate Measurement");
hr_char.DataAvailableFcn = @displayCharacteristicData;
function displayCharacteristicData(src,~)
[data,timestamp] = read(src,'oldest');
disp(data);
disp(timestamp);
end

Answers (1)

Markus Langer
Markus Langer on 13 May 2020
Here is the relevant part from the Appdesigner (file is attached) :
methods (Access = private)
function displayCharacteristicData(app)
app.Lamp.Color = 'green';
% Lamp not green
[data,timestamp] = read(src,'oldest');
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ScanButton
function ScanButtonPushed(app, event)
devicelist = blelist("Timeout", 5);
i = 1;
while(i <= length(devicelist.Index))
app.HRSensorListDropDown.Items{i} = devicelist.Name{i};
i = i + 1;
end
end
% Button pushed function: ConnectButton
function ConnectButtonPushed(app, event)
model = app.HRSensorListDropDown.Value;
chestbelt = ble(model);
hr_char = characteristic(chestbelt,"Heart Rate","Heart Rate Measurement");
hr_char.DataAvailableFcn = @displayCharacteristicData;
% app.Lamp.Color = 'green';
end
  3 Comments
Shad
Shad on 11 Jun 2021
Has anyone figured out how to get BLE callbacks to work in App Designer? I can receive the data if I set the callback function to one in an external m-file, but that has other issues.
Suraj Parameswaran
Suraj Parameswaran on 26 Apr 2022
I am facing the exact same problem. I have come to understand that we cannot pass the app variable in the callback function because the function expects only a BLE characteristic object and an event as an argument.
I have tried. @Joris Lambrecht 's method and he has explained it didn't work. However, when I am defining the function inside a command button function as a nested function like below, I am able to see the output in the command window:
function ConnectButtonPushed(app, event)
function recv_data(data_channel,evt)
received = 1;
data_packet = char(data_channel.read('oldest'));
disp(data_packet);
end
data_channel = app.data;
app.data.DataAvailableFcn = @recv_data;
subscribe(app.data);
end
I am thinking the only way to work around this problem would be to use global variables and then extract the variables in them as the dataavailablefcn does not allow the 'app' to be passed in as an argument. Let me know if anyone has a better idea. Thanks.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!