Get specific number of BLE notifications

조회 수: 10 (최근 30일)
Lars Urban
Lars Urban 2021년 11월 27일
답변: Frodo Muijzer 2023년 2월 10일
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

답변 (2개)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair 2021년 12월 1일
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 2023년 2월 10일
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

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by