Documentation

Work with BLE Blocks on Android Devices

This example shows how to configure your Android™ device to send data over Bluetooth Low Energy(BLE) Protocol and receive that data on another Android device using the Simulink® Android BLE blocks.

BLE Terminology

  • 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.

Introduction

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 app.

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.

Prerequisites

Required Hardware

  • An Android device with Lollipop (5.0) or greater, configured using Android Hardware Setup (Peripheral Device)

  • An 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. Open the androidblesendheartrate model.

2. Connect your Peripheral Device to your development computer using USB cable.

3. Click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

4. Select the Hardware Implementation pane and from the Hardware board parameter list, select Android Device.

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 model toolbar, click the Deploy to Hardware button to build, download, and run the 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 the 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. Click Simulation > Model Configuration *Parameters to open Configuration Parameters dialog.

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:

1. First 8 bits represent the flag.

2. If the 0th bit of the flag is 1, then Heart Rate format is uint16 else uint8.

3. If the 3rd bit of the flag is 1, then Energy Expended (uint16) is present in the received data.

4. If the 4th bit of the flag is 1, then RRInterval (uint16) is present in the received data.

InterpretHeartRateData function uses bitget and 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. Check your Central Device, BLE Scanner app will be installed and running on that 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 Start Discovery button.

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.

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.

1. Double-click on the BLE Receive block, Select the Android device which was configured as Peripheral device in Task 1, from the Peripheral device dropdown.

2. Select Heart Rate/Heart Rate Measurement from the Characteristic parameter dropdown.

3. On your Central Device, Click Settings > Bluetooth, and turn on Bluetooth.

4. On the model toolbar, click the Deploy to Hardware button to build, download, and run the model on your device.

5. 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.

Summary

This example showed how to use the BLE Send and BLE Receive blocks of Simulink Support Package for Android.

Was this topic helpful?