Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Implementing Bluetooth Low Energy (BLE) on the nRF52

December 9, 2023

Implementing Bluetooth Low Energy (BLE) on the nRF52

Bluetooth Low Energy (BLE) is a wireless communication technology designed for short-range communication between devices with low power consumption. It has gained immense popularity in recent years due to its use in various applications, such as fitness trackers, smartwatches, and home automation devices.

In this blog post, we will explore how to implement Bluetooth Low Energy on the nRF52 development board. The nRF52 is a powerful system-on-chip (SoC) solution developed by Nordic Semiconductor, specifically designed for BLE applications.

Prerequisites

Before diving into the implementation details, make sure you have the following prerequisites:

  1. nRF52 development board: Purchase an nRF52 development board, such as the nRF52-DK, which provides an ideal platform for BLE prototyping and development.

  2. Development environment: Set up your development environment with the necessary tools. You will need the nRF5 SDK provided by Nordic Semiconductor, which includes the necessary libraries, examples, and documentation.

  3. IDE: Choose your preferred Integrated Development Environment (IDE) for nRF52 application development. Options include SEGGER Embedded Studio, Keil µVision, and Eclipse with GCC.

Once you have the prerequisites set up, let’s move on to the implementation details.

BLE Basics

Before implementing BLE, it is important to understand some key concepts:

Implementing BLE on the nRF52

The nRF52 provides a comprehensive BLE software stack through the Nordic SoftDevice, which is a precompiled and prelinked binary library. It abstracts the complexities of the underlying BLE protocol stack and allows for easy application development.

  1. Configure the nRF52 SDK: Download and install the nRF5 SDK provided by Nordic Semiconductor. Follow the installation guide to set up the necessary dependencies and configure the SDK for your development environment.

  2. Set up a BLE project: Create a new project using your chosen IDE and configure it to use the nRF5 SDK. Import the required libraries and header files provided in the SDK.

  3. Initialize the SoftDevice: Before using any BLE functionality, initialize the SoftDevice by calling the sd_softdevice_enable() function. This function sets up the necessary BLE stack and configures the nRF52 hardware.

  4. Define the GATT database: Define your GATT database by creating Services, Characteristics, and Descriptors. Each Service, Characteristic, and Descriptor should have a unique UUID.

    Here’s an example of how to define a simple custom Service with a single Characteristic:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    ble_uuid_t service_uuid;
    ble_uuid128_t base_uuid = {MY_SERVICE_UUID_BASE};
    
    err_code = sd_ble_uuid_vs_add(&base_uuid, &service_uuid.type);
    APP_ERROR_CHECK(err_code);
    
    service_uuid.uuid = MY_SERVICE_UUID;
    
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &service_uuid, &service_handle);
    APP_ERROR_CHECK(err_code);
    
    ble_uuid_t char_uuid;
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_t attr_char_value;
    ble_gatts_attr_md_t attr_md;
    
    memset(&char_md, 0, sizeof(char_md));
    
    char_uuid.uuid = MY_CHARACTERISTIC_UUID;
    char_md.char_props.read = 1;
    
    memset(&attr_md, 0, sizeof(attr_md));
    
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
    
    memset(&attr_char_value, 0, sizeof(attr_char_value));
    
    attr_char_value.p_uuid = &char_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len = sizeof(uint32_t);
    attr_char_value.init_offs = 0;
    attr_char_value.max_len = sizeof(uint32_t);
    
    err_code = sd_ble_gatts_characteristic_add(service_handle, &char_md, &attr_char_value, &char_handle);
    APP_ERROR_CHECK(err_code);
    
  5. Handle BLE events: Implement event handlers to handle various BLE events such as connection, disconnection, and characteristic value changes. These event handlers are typically implemented as callback functions.

  6. Advertise and discover: Implement code to advertise your device and handle incoming connections. This includes setting up advertising parameters, defining the advertising data, and handling connection requests.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    ble_gap_adv_params_t adv_params;
    
    memset(&adv_params, 0, sizeof(adv_params));
    adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;
    adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
    adv_params.interval = APP_ADV_INTERVAL;
    
    err_code = sd_ble_gap_adv_start(&adv_params, conn_config_tag);
    APP_ERROR_CHECK(err_code);
    

    Additionally, implement code to handle service discovery by remote devices, allowing them to discover and interact with your device’s GATT database.

  7. Handle Characteristic value changes: Implement code to handle write operations on your Characteristics. This involves defining a callback function to handle write requests and performing the necessary operations based on the received data.

    1
    2
    3
    4
    5
    6
    7
    8
    
    void on_characteristic_write(ble_evt_t const *p_ble_evt)
    {
        ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        if (p_evt_write->handle == char_handle.value_handle)
        {
            // Handle write operation on the Characteristic value
        }
    }
    

These are just some basic steps to get started with implementing BLE on the nRF52 using the Nordic SoftDevice. The nRF5 SDK provides several example projects that can serve as a great starting point for your BLE application development.

Conclusion

Implementing Bluetooth Low Energy (BLE) on the nRF52 is a powerful way to build IoT devices and wireless communication applications. In this blog post, we explored the basics of BLE, the nRF52 development board, and the steps to implement BLE using the Nordic SoftDevice.

Remember, this is just the beginning, and there is a whole world of possibilities with BLE on the nRF52. Start experimenting, explore the vast resources provided by Nordic Semiconductor, and create innovative applications leveraging the power of BLE.

Happy coding!


➡️ Developing custom BLE profiles with the nRF52


⬅️ Using the ADC module on the nRF52


Go back to Posts.