Log Characteristic Value from Bluetooth Low Energy Peripheral Device Using Callback Function
This example shows how to access a characteristic of your Bluetooth® Low Energy (BLE) device, create a callback function to read the raw BLE characteristic value for a fixed duration, and log it in a text file.
Required Hardware
Any BLE device with characteristics that supports attributes such as Read
and either Notify
or Indicate
. For more details see, Attributes. This example uses Arduino® Nano 33 BLE hardware.
Discover and Connect to BLE Device
First, check that the BLE device supports connections by finding it in MATLAB®. The blelist
function scans advertising BLE peripheral devices that are advertising indications.
blelist
Run bluetoothlist to search for nearby Bluetooth classic devices.
ans=29×5 table
Index Name Address RSSI Advertisement
_____ _________________ ______________ ____ _____________
1 "" "2693A0535E04" -19 1×1 struct
2 "" "3A136453F9FD" -37 1×1 struct
3 "Mambo_560253" "E014F6303DFD" -38 1×1 struct
4 "" "229CC4005139" -55 1×1 struct
5 "" "3745017A158B" -56 1×1 struct
6 "" "26864404E2D7" -58 1×1 struct
7 "WaveGen" "42162F5C6B30" -58 1×1 struct
8 "" "10EBD9CAA72B" -60 1×1 struct
9 "" "1DD7C49A016F" -61 1×1 struct
10 "" "35E69990D1D3" -61 1×1 struct
11 "vineethnArduino" "7C9EBD3ACC32" -64 1×1 struct
12 "" "2EF9235E31A2" -68 1×1 struct
13 "" "55ACCE12E59D" -68 1×1 struct
14 "" "E07F70DE320F" -68 1×1 struct
15 "" "21F3C3B77DDD" -69 1×1 struct
16 "" "7638FEDD33A4" -69 1×1 struct
⋮
Once you find the device in MATLAB, connect to it by calling ble
. Specify the name of the device if it has a unique name, or specify the device address. This example uses the device address.
bleObj = ble("42162F5C6B30");
Retrieve Characteristic Value
Retrieve all the available characteristics of the BLE device.
allCharacteristics = bleObj.Characteristics;
Create a characteristic object using the ServiceUUID
and CharacteristicUUID
input arguments. Select the characteristic that supports attributes such as Read
and either Notify
or Indicate
. This enables you to read the characteristic value upon being notified by the BLE device.
charObj = characteristic(bleObj,allCharacteristics.ServiceUUID(4),allCharacteristics.CharacteristicUUID(4));
Subscribe to the selected characteristic to enable notifications or indications. This selection enables the callback function to be triggered whenever a new characteristic value sent by the BLE device.
subscribe(charObj);
Create a log file to store the characteristic value available at the BLE device.
fileID = fopen("rawBLEData.txt","w+");
Create and Execute Callback Function
Create a callback function to handle the new data notifications. 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. Log the timestamp and the data to the file with millisecond precision.
function displayCharacteristicData(src,~,fileID) % After subscribing to the notification or indication of % BLE characteristic value, this callback function will be % triggered each time the BLE device sends new characteristic % data. [data,timestamp] = read(src,'oldest'); % Store the time stamp with milli second precision timestamp.Format = 'MM/dd/uuuu HH:mm:ss.SSSS'; % Log the timestamp fprintf(fileID,"\n%s\t",timestamp); % Log the data fprintf(fileID,repmat('%d ',1,length(data)), data); end
Initialize the callback function to collect the characteristic value sent by the BLE device.
Use the @
operator to assign the function handle to the DataAvailableFcn
property of the characteristic object. Use Anonymous Functions to pass arguments to your callback in addition to the source and event data arguments passed by MATLAB. Anonymous functions can use any variables that are available in the current workspace. For more details, see Additional Arguments for Callback Function.
charObj.DataAvailableFcn = @(src,event)displayCharacteristicData(src,event,fileID);
Log the characteristic value for a duration of 10 seconds
pause(10);
Close the log file.
fclose(fileID);
The text below displays a snippet of the log file. The first column contains the date and time stamp at millisecond precision and the second column contains the corresponding characteristic value.
12/01/2023 20:17:08.0529 3 12/01/2023 20:17:08.2478 4 12/01/2023 20:17:08.4578 5 12/01/2023 20:17:08.6525 1 12/01/2023 20:17:08.8626 2 12/01/2023 20:17:09.0424 3 12/01/2023 20:17:09.2829 4 12/01/2023 20:17:09.4624 5 12/01/2023 20:17:09.6428 1 12/01/2023 20:17:09.8823 2 12/01/2023 20:17:10.0627 3 12/01/2023 20:17:10.2427 4 12/01/2023 20:17:10.4825 5 12/01/2023 20:17:10.6626 1 12/01/2023 20:17:10.8429 2 12/01/2023 20:17:11.0826 3 12/01/2023 20:17:11.2628 4 12/01/2023 20:17:11.4428 5 12/01/2023 20:17:11.6826 1 12/01/2023 20:17:11.8626 2 12/01/2023 20:17:12.0426 3 12/01/2023 20:17:12.2824 4 12/01/2023 20:17:12.4627 5 12/01/2023 20:17:12.6428 1 12/01/2023 20:17:12.8827 2 12/01/2023 20:17:13.0624 3 12/01/2023 20:17:13.2426 4 12/01/2023 20:17:13.4825 5 12/01/2023 20:17:13.6624 1 12/01/2023 20:17:13.8427 2 12/01/2023 20:17:14.0825 3 12/01/2023 20:17:14.2628 4 12/01/2023 20:17:14.4427 5 12/01/2023 20:17:14.6824 1 12/01/2023 20:17:14.8625 2 12/01/2023 20:17:15.0428 3 12/01/2023 20:17:15.2824 4 12/01/2023 20:17:15.4627 5 12/01/2023 20:17:15.6424 1 12/01/2023 20:17:15.8825 2 12/01/2023 20:17:16.0627 3 12/01/2023 20:17:16.2424 4 12/01/2023 20:17:16.4826 5 12/01/2023 20:17:16.6627 1 12/01/2023 20:17:16.8427 2 12/01/2023 20:17:17.0823 3 12/01/2023 20:17:17.2628 4 12/01/2023 20:17:17.4426 5 12/01/2023 20:17:17.6824 1 12/01/2023 20:17:17.8623 2
Clean Up
After you finish working with the characteristic, disable notifications using unsubscribe
and set the DataAvailableFcn
property to null.
unsubscribe(charObj); charObj.DataAvailableFcn = [];