Get specific number of BLE notifications
2 views (last 30 days)
Show older comments
Hi,
I'm writing a script trying to get 10 notifications from a BLE device. The value contiains a char-array which i then want to display (and later save to csv, not implemented).
I want to get the values as soon as they are available and dont want double readings so I thought using the notification feature of BLE might work. I dont know how to use the DataAvailableFcn though. I get this error:
Error using BLETest (line 10)
Invalid value for DataAvailableFcn. Specify [] or a function handle that accepts two inputs.
The MATLAB exaples only use delays and read the data periodicly.
My code looks like this:
clear esp
esp = ble("BLE");
c = esp.characteristic("41BD8A4C-4E52-11EC-81D3-0242AC130003","17789A04-4E54-11EC-81D3-0242AC130003");
subscribe(c);
measurements = 10;
for m = 1:measurements
c.DataAvailableFcn = @displayData;
end
unsubscribe(c);
function displayData()
data = c.read();
text = char(data);
disp(text);
m + 1;
end
0 Comments
Answers (2)
Harikrishnan Balachandran Nair
on 1 Dec 2021
Hi Lars,
The 'DataAvailableFcn' property should be assigned a Function Handle that accepts two inputs . In the code you have provided, the function 'displayData' does not have any input arguments, and hence the error occurs.
You can refer to the example here to get a better idea on this : https://www.mathworks.com/help/matlab/ref/matlabshared.blelib.characteristic.read.html#mw_5cba06cb-595e-44b8-82cd-44a397a29c74
0 Comments
Frodo Muijzer
on 10 Feb 2023
Hello,
It's an old topic, but I'm running into the almost same issue (And same error message). From the help/example listed above, I'm able to print the results of the notication to the output window, so the BLE communication works. But how do I get the data received from my device back to my main script, for example to plot it or write it to a file?
As soon as I try to add arguments to the function call, it complains that the function only acceps two inputs (presumably src and evt that are already present under water.) How do I add arguments to the function call?
accel_ble.DataAvailableFcn = {@read_accel_Data, x1, y1, z1};
function read_accel_Data(src, evt, x_plot, y_plot, z_plot)
....
end
doesn't work, also tried many variants with square brackets etc.
Thanks in advance for the hints!
accel_ble.DataAvailableFcn = @read_accel_Data;
...
function read_accel_Data(src, evt)
[accel_raw,timestamp] = read(src,'oldest') ;
for i = 1:1:3 % x,y,z
accel(i) = accel_raw(2*i-1); %MSB
accel(i) = bitor(accel(i), bitshift(accel_raw(2*i),8,'int16'),'int16'); %LSB
end
%this gives an error, since x_plot etc. are not known in this context.
addpoints(x_plot,timestamp,accel(1));
addpoints(y_plot,timestamp,accel(2));
addpoints(z_plot,timestamp,accel(3));
end
0 Comments
See Also
Categories
Find more on Application Deployment 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!