1. Introduction
This document provides a brief introduction to the use of MATLAB® functions to enumerate, connect and control a Muse v3 device over Bluetooth Low Energy (BLE). BLE is a wireless communication protocol that uses Bluetooth with low-power peripheral devices. It is worth noting that BLE communication support is different from the Bluetooth Communication interface in MATLAB®, which uses the Bluetooth Serial Port Profile (SPP).
All the examples are based on MATLAB® official documentation, available at the following link:
2. Function
To connect to peripheral devices that support BLE, your computer needs to have a built-in or external Bluetooth 4.0 adapter. After you connect to a device, use MATLAB to read from or write data to it. You can work with both standard and custom services, characteristics, and descriptors.
2.1 Device Enumeration
Reference: https://uk.mathworks.com/help/matlab/ref/blelist.html
blelist function allows you to scan for nearby Bluetooth Low Energy peripheral devices and look at the device advertisement data.
The available devices are listed in a table as in Figure 1, which includes a set of relevant fields such as: name, address, the RSSI value indicating signal strength and a struct with the advertisement data. In its simplest operating mode, there are no input parameters and/or filters.
2.2 Device Connection
Reference: https://uk.mathworks.com/help/matlab/ref/ble.html
ble(name) function allows to connect to a selected BLE device. It returns a BLE object that represents the connection to a BLE peripheral device from your computer.
It is also possible to connect to a peripheral device using its address as an alternative to using the device name as input parameter. If the connection successfully complete, the function returns a BLE object that includes a set of properties related to the device as well as access to services and characteristics descriptors.
If read or write are supported in the object Attributes property, you can read characteristic values using read or you can write characteristic values using write. If the characteristic has any descriptors, you can also access them using descriptor.
2.3 Read
Reference: https://uk.mathworks.com/help/matlab/ref/matlabshared.blelib.characteristic.read.html
read(c) function allows to read the characteristic value from BLE peripheral device. The data read depends on the Attributes property of the input characteristic object c.
If the characteristic supports “Notify” it is also possible to create a callback function that accesses the characteristic content on your peripheral device. In the follow, a generic example of callback function:
function displayCharacteristicData(src,evt) [data,timestamp] = read(src,'oldest'); disp(data); disp(timestamp); end
2.4 Write
Reference: https://uk.mathworks.com/help/matlab/ref/matlabshared.blelib.characteristic.write.html
write(c,data) writes the specified data to a characteristic of BLE peripheral device. The Attributes property of the input characteristic object c must be Write and/or WriteWithoutResponse.
2.5 Subscribe
Reference: https://uk.mathworks.com/help/matlab/ref/matlabshared.blelib.characteristic.subscribe.html
subscribe(c) subscribes to a characteristic notification or indication of a BLE peripheral device. Notification or indication depends on the Attributes property of the characteristic c. The property must contain Notify, Indicate, or both. If the property contains both Notify and Indicate, the function subscribes to notification.
After you finish working with the characteristic, disable notifications using unsubscribe(c).