Get specific number of BLE notifications

9 visualizaciones (últimos 30 días)
Lars Urban
Lars Urban el 27 de Nov. de 2021
Respondida: Frodo Muijzer el 10 de Feb. de 2023
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");
Scanning and connecting to Bluetooth Low Energy peripheral devices are not supported on Linux.
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

Respuestas (2)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair el 1 de Dic. de 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.

Frodo Muijzer
Frodo Muijzer el 10 de Feb. de 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

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by