%% Read Characteristic Data from a Bluetooth Low Energy Peripheral Device Using a Callback Function
% Access a characteristic on your peripheral device and create a callback function to read its data.
% Create a connection to a nearby Bluetooth Low Energy peripheral device.

b = ble("Thingy")

% Create a characteristic object that represents the "Temperature" characteristic.

c = characteristic(b,"Weather Station Service","Temperature")

% Because this characteristic supports "Notify", you can create a callback function. Name the function displayCharacteristicData and define it as follows. Specify the read mode as 'oldest' instead of 'latest'. Calling the 'latest' data may lead to errors in the callback function caused by the flushing of previous data.
% function displayCharacteristicData(src,evt)
%     [data,timestamp] = read(src,'oldest');
%     disp(data);
%     disp(timestamp);
% end
% Use the @ operator to assign the function handle to the DataAvailableFcn property of the characteristic. When a new notification is available, the data appears in your command window.

c.DataAvailableFcn = @displayCharacteristicData

% After you finish working with the characteristic, disable notifications using unsubscribe.

unsubscribe(c)

% Copyright 2019 The MathWorks, Inc.