To implement advertising in a Bluetooth Low Energy (BLE) application using the Nordic Semiconductor nRF series, you will need to follow a series of steps that involve configuring the BLE stack, setting up advertising parameters, and starting the advertising process. Below is a detailed guide on how to achieve this.
Step-by-Step Guide to Implementing Advertising in a BLE Application
Step 1: Set Up Your Development Environment
Before you start coding, ensure that you have your development environment set up. This includes:
- Nordic SDK: Make sure you have the Nordic SDK installed and configured in your IDE (e.g., SEGGER Embedded Studio).
- Development Board: Connect your nRF development board (e.g., nRF52-DK) to your computer.
Step 2: Create a New BLE Project
- Open SEGGER Embedded Studio.
- Create a new project based on one of the BLE examples provided in the Nordic SDK. You can find these examples in the
examplesfolder of the SDK.
Step 3: Include Necessary Headers
At the top of your main application file, include the necessary headers for BLE functionality:
#include "ble.h"
#include "ble_advertising.h"
#include "nrf_sdm.h"
#include "nrf_soc.h"
#include "app_error.h"
Step 4: Define Advertising Parameters
You need to define the advertising parameters, including the advertising interval and the advertising type. This is typically done in a function that initializes the BLE stack.
#define APP_ADV_INTERVAL 64 // Advertising interval in 0.625 ms units
#define APP_ADV_DURATION 18000 // Advertising duration (in 0.625 ms units)
static ble_advertising_t m_advertising; // Advertising instance
void advertising_init(void) {
ret_code_t err_code;
ble_advertising_init_t init;
memset(&init, 0, sizeof(init));
init.advdata.name_type = BLE_ADVDATA_FULL_NAME; // Use full name in advertising packet
init.advdata.include_appearance = false;
init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
init.srdata.uuids_complete.uuid_cnt = 0; // No UUIDs in the advertising packet
init.config.ble_adv_whitelist = NULL; // No whitelist
init.config.ble_adv_directed_enabled = false; // No directed advertising
init.config.ble_adv_fast_enabled = true; // Enable fast advertising
init.config.ble_adv_fast_interval = APP_ADV_INTERVAL; // Set advertising interval
init.config.ble_adv_fast_timeout = APP_ADV_DURATION; // Set advertising duration
err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);
}
Step 5: Start Advertising
After initializing the advertising parameters, you can start advertising your BLE device. This is typically done in the main function or after the BLE stack has been initialized.
void advertising_start(void) {
ret_code_t err_code;
err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
}
Step 6: Handle BLE Events
You need to handle BLE events, such as when a connection is established or when advertising is stopped. This is done in the event handler function.
void ble_event_handler(ble_evt_t const * p_ble_evt) {
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
// Handle connection event
break;
case BLE_GAP_EVT_DISCONNECTED:
// Restart advertising after disconnect
advertising_start();
break;
default:
break;
}
}
Step 7: Main Function
In your main function, call the initialization and start functions to set everything up.
int main(void) {
// Initialize the BLE stack
ble_stack_init();
// Initialize advertising
advertising_init();
// Start advertising
advertising_start();
// Enter main loop
for (;;) {
// Handle BLE events
power_manage();
}
}
Step 8: Compile and Flash the Application
- Build the project in SEGGER Embedded Studio.
- Flash the application to your nRF development board.
- Open a BLE scanner app (like nRF Connect) on your smartphone to see your device advertising.
Conclusion
By following these steps, you have successfully implemented advertising in a BLE application using Nordic Semiconductor’s nRF series. This foundational knowledge is essential for developing more complex BLE applications, allowing you to connect and communicate with other BLE devices.
In future episodes, we will explore more advanced BLE features, such as creating GATT services and characteristics, handling data transfer, and implementing notifications. Stay tuned!

Leave a comment