Work with BLE Blocks on Android
This example shows how to configure your Android™ device to send data over Bluetooth® Low Energy (BLE) protocol and receive data on another Android device using BLE blocks from Simulink® Support Package for Android Devices.
Bluetooth Low Energy (BLE) - A wireless protocol used for low power and short range communication, mainly used in the applications like Healthcare, Fitness, Home Automation, etc.
Service - Services are used to break data up into logic entities, and contain specific chunks of data called characteristics.
Characteristic - Encapsulates a single data point, though it may contain an array of related data, such as the X/Y/Z values from a 3-axis accelerometer.
Generic Attribute Profile (GATT) - Defines the way the two Bluetooth Low Energy devices transfer data back and forth using concepts called Services and Characteristics.
Peripheral - Device which defines the Services and Characteristics and advertises its presence to the nearby BLE devices. It acts as Server which receives connection and data requests from the BLE central devices.
Central - Device which sends connection request and data request to the BLE peripheral device. It acts as Client.
Universally Unique Identifier (UUID) - A 128-bit value used to identify BLE Services and Characteristic. Each Service and Characteristic has its own UUID. Standard BLE Services are defined by Bluetooth Special Interest Group.
Custom UUID - It is a user defined UUID, which are used by custom Services and Characteristics.
A heart rate BLE device allows real-time measurement and transmission of human heart rate information to a nearby mobile device.
In this example, we use two Android devices to simulate the measurement of a BLE Heart Rate sensor and display the results in an Android application.
One device simulates the heart rate sensor using the BLE Heart Rate service and measurement characteristic providing artificial data approximating typical data, such as Heart Rate (beats per minute), Energy Expended (Joules), and RRinterval (time interval between two heart beats). Another Android device runs a prototype app to process and display the heart rate information to a user. In combination, you can simulate a complete BLE sensor measurement workflow.
Heart Rate Service and Characteristics
The BLE Heart Rate service organizes the Heart Rate information provided by the sensors into the following characteristics.
A closer inspection of the Heart Rate Measurement characteristic shows how the actual data from the sensors in arranged into the characteristic format.
Heart Rate Measurement - This characteristic is used to send/receive a heart rate measurement. The first byte in the characteristic represents the flag. By reading the bits in the flag, the measurement data in the characteristic can be parsed and read into your device.
This example provides two Simulink models that will be deployed to your Android devices:
androidblesendheartrate: A model using the BLE Send block to act as BLE Heart Rate Sensor device, sending Heart Rate Measurement characteristic value to connected BLE device.
androidblereceiveheartrate: A model using the BLE Receive block to get Heart Rate sensor data from first model sending the artificial Heart Rate data.
Download and Install Simulink Support Package for Android Devices
For more information on how to use the Simulink Support Package for Android Devices to run a Simulink model on your Android device, see Getting Started with Android Devices.
Android device with Lollipop (5.0) or greater, configured using Android Hardware Setup (peripheral Device)
Android device with Jelly Bean (4.3) or greater, configured using Android Hardware Setup (central Device)
USB cable to connect the device to your development computer
Task 1 - Send Heart Rate Measurement data
Using this model, your Android device will emulate the behavior of BLE heart rate sensor (Peripheral) supporting Standard BLE Heart Rate Service. You can control the Heart Rate data sent to connected Central device.
1. To open the androidblesendheartrate model, run this command in MATLAB® Command Window.
2. Connect your Peripheral Device to your development computer using USB cable.
3. On the Modeling tab of the toolstrip, select Model Settings.
4. Select the Hardware Implementation pane and from the Hardware board parameter list, select
5. Click Device options and ensure that the device matches your peripheral Device. Click OK.
6. Double-click on the BLE Send block and observe that Mode is set to Peripheral (Server), Service is set to Heart Rate and Characteristic is set to Heart Rate Measurement.
7. ConvertToHeartRateFormat MATLAB function accepts Heart Rate (uint16), Energy Expended (uint16) and RRInterval (uint16) as input and outputs int8 array of length 7. First element of the output array will be flag, the second and third elements will be Heart Rate, the fourth and fifth elements will be Energy Expended and similarly sixth and seventh element will be RRInterval.
8. On your Android device, Click Settings > Bluetooth, and turn on Bluetooth.
9. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start to run the Simulink model on your device.
10. Using sliders on your device screen, change the Heart Rate, Energy expended, and RRInterval values.
11. Once your peripheral device is connected to the central device, these values can be viewed on the central device.
Task 2 - Configure Receive model
Using this model, your central device will receive Heart Rate Measurement characteristic data from the Peripheral Device.
1. Open the androidblereceiveheartrate model.
2. Connect central device to your development computer using a USB cable.
3. In the Modeling tab of the toolstrip, select Model Settings.
4. Select the Hardware Implementation pane and from the Hardware board parameter list, select Android Device.
5. Click Target hardware resources > Device options, Select the Central Device and click OK.
Interpreting Heart Rate Measurement data
BLE Receive block outputs received heart rate measurement data as int8 array of length 7. The InterpretHeartRateData MATLAB function block in the model, interprets Heart Rate, Energy Expended, and RR Interval values from the received data array.
Standard Heart Rate Measurement Characteristic data format:
First 8 bits represent the flag.
If the 0th bit of the flag is 1, then Heart Rate format is uint16 else uint8.
If the 3rd bit of the flag is 1, then Energy Expended (uint16) is present in the received data.
If the 4th bit of the flag is 1, then RRInterval (uint16) is present in the received data.
InterpretHeartRateData function uses
typecast MATLAB functions to interpret the data present in the received array of bytes. bitget MATLAB function is used to extract the value of a bit in the flag.
typecast MATLAB function is used to convert two bytes into uint16 data value. Similarly,
typecast function can be used to convert received data bytes into any desired datatype.
Task 3 - Scan Nearby BLE devices
This task also uses same model as Task 2. In this task, you will scan for Peripheral devices.
1. Double-click on the BLE Receive block and observe that Mode is set to
Central (Client) mode.
2. To know the peripheral device to be connected and characteristic supported by it, click the Scan button.
3. A wizard will open on your development computer providing instructions to install BLE Scanner app as shown in the screenshot below.
4. On your central device, click Settings > Bluetooth, Turn on Bluetooth.
5. Click Install button on the wizard.
6. BLE Scanner app will be installed on your Central Device. Check your Central Device, app will be running on your Central Device.
7. This app will scan all nearby BLE Peripheral devices and display them on your device.
8. Select all the devices whose BLE information needs to be retrieved and then click Discover services.
9. BLE information like the BLE address, device name, BLE Services and BLE characteristic supported by the selected devices will be retrieved.
10. Click on the Save and Close button to save the retrieved BLE information in the xml.
11. Go to the wizard on your development computer, click Next button.
12. The wizard will show all the devices whose BLE Information was retrieved.
13. Click Finish button to close the wizard.
Task 4 - Receive Heart Rate Measurement data
Using the model as shown in the Task 2, you will deploy the receive app on your Central Device and will receive Heart Rate Measurement characteristic data.
Double-click on the BLE Receive block, Select the Android device which is configured as Peripheral device in Task 1, from Peripheral device dropdown.
Select Heart Rate/Heart Rate Measurement from Characteristic parameter dropdown.
On your central device, Click Settings > Bluetooth, Turn on Bluetooth.
On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start to run the Simulink model on your device.
Heart Rate, Energy expended and RRInterval values received can be viewed on your Central Device.
Other Things to Try
Change the Service and Characteristic in both the models.
Instead of Peripheral Device, try using an actual BLE Heart Rate Sensor which supports Standard BLE Heart Rate Service.
Specify your own custom Characteristic (UUID) for both the models.