ble unsubscribe then re-subscribe duplicate packets

I am subscribing to a characteristic and then unsubscribing. If I ever re-subscribe to the characteristic again I get duplicate packets . I need to close matlab in order to stop this issue...
1 connect
app.ble1=ble(app.data1.ble1.address);
2 subscribe and attach funcion (this is in appdesigner..)
app.data1.ble1.readChar = characteristic(app.ble1, ServUUID, CharUUID);
if(~isempty(app.data1.ble1.readChar.DataAvailableFcn))
%there is aready a function attached so clear it
unsubscribe(app.data1.ble1.readChar);
app.data1.ble1.readDescr = descriptor(app.data1.ble1.readChar,"2902");
write(app.data1.ble1.readDescr,[00 00]);
app.data1.ble1.readChar.DataAvailableFcn = [];
else
%there is no function attached just but jut incase unsub hard
unsubscribe(app.data1.ble1.readChar);
app.data1.ble1.readDescr = descriptor(app.data1.ble1.readChar,"2902");
write(app.data1.ble1.readDescr,[00 00]);
end
write(app.data1.ble1.readDescr,[01 00]); %write to enable notifications
app.data1.ble1.readChar.DataAvailableFcn = @(src,evt)BleSubFcn(app,src,evt);
subscribe(app.data1.ble1.readChar);
3. run the function to print the data
function BleSubFcn(app,src,~)
[packet_data,timestamp] = read(src,'oldest');
if(~isfield(app.data1,'cntr'))
%this is the first run
app.data1.cntr=1;
else
%packet counting
app.data1.cntr=app.data1.cntr+1;
end
flag = uint8(packet_data(1));
% Get the first bit of the flag, which indicates the format of the heart rate value
heartRateValueFormat = bitget(flag, 1);
if heartRateValueFormat == 0
% Heart rate format is uint8
heartRate = packet_data(2);
else
% Heart rate format is uint16
heartRate = double(typecast(uint8(packet_data(2:3)), 'uint16'));
end
fprintf('HR: %d(bpm), packet: %i , time: %s\n', heartRate, app.data1.cntr,datestr(timestamp,'MM:SS.FFF') );
end
4. shutdown ble
if(~isempty(app.data1.ble1.readChar.DataAvailableFcn))
%there is aready a function attached to readchar
unsubscribe(app.data1.ble1.readChar);
app.data1.ble1.readDescr = descriptor(app.data1.ble1.readChar,"2902");
write(app.data1.ble1.readDescr,[00 00]);
app.data1.ble1.readChar.DataAvailableFcn = [];
end
app.ble1=[]; %clear ble connection
app.data1.ble1=[]; %clear stored ble data and charac reads etc
RESULTS
HR: 93(bpm), packet: 241 , time: 03:03.651
HR: 93(bpm), packet: 242 , time: 03:03.652
HR: 94(bpm), packet: 243 , time: 03:04.675
HR: 94(bpm), packet: 244 , time: 03:04.675
HR: 95(bpm), packet: 245 , time: 03:05.650
HR: 95(bpm), packet: 246 , time: 03:05.650
HR: 96(bpm), packet: 247 , time: 03:06.674
HR: 96(bpm), packet: 248 , time: 03:06.674
HR: 97(bpm), packet: 249 , time: 03:07.697
HR: 97(bpm), packet: 250 , time: 03:07.698
HR: 98(bpm), packet: 251 , time: 03:08.672
HR: 98(bpm), packet: 252 , time: 03:08.673
HR: 99(bpm), packet: 253 , time: 03:09.696
HR: 99(bpm), packet: 254 , time: 03:09.696

4 Comments

Also,
Is clearing the ble connection really the only way to close the ble connection? Seems so odd
I have the same issue. I tried a regular subscribre() - unsubscribe() and the method with a callback function, same problem.
Indeed, disconnecting the BLE device is solving the issue, but it shouldn't be the way to do it.

Sign in to comment.

Answers (1)

Using Matlab 2024a on Windows 10, I have noticed a duplicate call of the Callback function depending on the order of the operations of setting a callback (setting the field DataAvailableFcn) and subscribing (calling subscribe).
With the following callback:
function cb_read(rx, ev)
s = char(read(rx, 'oldest')); % Fetch the content.
fprintf('%s\n', s); % Display the content.
end
A) When I set the callback first then subscribing, I have duplicate packets (duplicate call of the callback).
oble = ble('112233445566'); % Use the appropriate MAC address.
rx = characteristic(oble, 'SERVICE', 'CHARARCTERISTIC'); % Use the appropriate values.
rx.DataAvailableFcn = @(s, e)cb_read(s, e); % Subscribing.
subscribe(rx); % Setting callback.
B) When I subscribe then setting the callback, I have single packet.
oble = ble('112233445566'); % Use the appropriate MAC address.
rx = characteristic(oble, 'SERVICE', 'CHARARCTERISTIC'); % Use the appropriate values.
subscribe(rx); % Setting callback.
rx.DataAvailableFcn = @(s, e)cb_read(s, e); % Subscribing.
However, in case B), if I unsubscribe and resubscribe, I fall into case A) because a callback is set before calling subscribe()
unsubscribe(rx);
subscribe(rx); % The callback is already set.
% From here the callback is called twice.
%% If I unsubscribe and resubscribe.
unsubscribe(rx);
subscribe(rx); % The callback is already set.
% From Here the callback is called 3 times!

Products

Release

R2021a

Asked:

on 26 Jul 2021

Answered:

on 31 Jul 2024

Community Treasure Hunt

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

Start Hunting!