No ACL setup yet! Denying access to everyone.
en:edu:zmitac:s2:iotn:ble1

This is an old revision of the document!


Bluetooth Low Energy for STM32 - peripheral

This laboratory shows the details of the example provided as part of the STM32duinoBLE library by Arduino.

Installing the library and importing the example

As the first step, we need to install the library to the current project (how to download and import the first STM32WB55 project is described here. We can choose the simplest blink project as the base for our BLE peripheral).

:img
Fig. 1: Search for the BLE library for STM32 in VSCode/PlatformIO

After finding the library in the repository we can add it to our project. By clicking the “Add to Project” button you can select the already existing “arduino blink” project to add the library entry to the platformio.ini file automatically. It will create the dependency of your project build with the specific version of the STP32duinoBLE library. To use the code provided as the example, select the code of the “LED” example and paste it as the content of the “Blink.cpp” program chosen from the project tree.

:img
Fig. 2: Install the BLE library in the existing project

Description of the most important elements of the code

Variables and objects definition

To use the BLE device you need to create the object of the BLELocalDevice class. This object can work using the SPI-connected Bluetooth module or the built-in unit in SoC. In the case of STM32WB55, the internal unit is used, so the HCISharedMemTransportClass is chosen as the connection to the BLE module.

HCISharedMemTransportClass HCISharedMemTransport;
BLELocalDevice BLEObj(&HCISharedMemTransport);
BLELocalDevice& BLE = BLEObj;

After creating the BLE object, we can configure services, characteristics and advertising process. Let's define the service first. To use the service you define, you specify the 128-bit version of the UUID. You can also use predefined 16-bit services as listed in Bluetooth SIG documents.

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

Services usually contain characteristics, also identified by 128-bit or 16-bit UUIDs.

BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

The number of characteristics in one service is limited to 512 by BLE specification. You can experiment on how many characteristics can be used in Arduino implementation.

Initialization

The BLE subsystem must be initialized before use. It is possible to check if initialisation is successful. This can be done once in the Arduino “setup()” function.

 // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

After successful initialisation, you can specify the name of your device which appears while scanning the BLE neighbourhood. It is included in the advertising frames. Also, the service you defined can be added to the advertising information.

 // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);
Arduino BLE implementation limits the number of advertised services to one.

The next step is to create a list of services and characteristics.

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

The order is important, first, you need to add a characteristic (or more characteristics) to the service, next, add the service to the list of services.

The number and list of services available after establishing the connection can differ from those advertised.

Before starting the advertising process it is possible to set the initial value of the characteristic.

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

Loop function

As in all Arduino programs, after the initialization, the program starts to execute an infinite “loop()” function. First, it calls the “BLE.central()” function that specifies the role of our device as peripheral (server), and checks if there is any central (client) connected.

  BLEDevice central = BLE.central();
BLE.central() function is non-blocking so you can execute other code if the connection is not established.
en/edu/zmitac/s2/iotn/ble1.1702732022.txt.gz · Last modified: by ktokarz