Bluetooth: Central BAS

The Central BAS sample demonstrates how do use the GATT Battery Service (BAS) Client. It uses the BAS Client to receive battery level information from a compatible device.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Board target

nRF54L15 DK

PCA10156

nrf54l15dk

nrf54l15dk/nrf54l15/cpuapp

nRF54H20 DK

PCA10175

nrf54h20dk

nrf54h20dk/nrf54h20/cpuapp

nRF5340 DK

PCA10095

nrf5340dk

nrf5340dk/nrf5340/cpuapp/ns nrf5340dk/nrf5340/cpuapp

nRF52 DK

PCA10040

nrf52dk

nrf52dk/nrf52832

nRF52840 DK

PCA10056

nrf52840dk

nrf52840dk/nrf52840

When built for a board target with the */ns variant, the sample is configured to compile and run as a non-secure application with Cortex-M Security Extensions enabled. Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.

The sample also requires a device running a BAS Server to connect with (for example, another development kit running the Bluetooth: Peripheral HIDS mouse or Bluetooth: Peripheral HIDS keyboard sample, or a computer with a Bluetooth® Low Energy dongle and nRF Connect for Desktop).

Overview

When connected, the sample subscribes to battery level notifications. Every notification that is received is printed to the terminal.

If the device does not support notifications for the Battery Level Characteristic, press Button 1 to request for reading the battery level.

User interface

Button 1:

Send read request for the battery level value.

Building and running

This sample can be found under samples/bluetooth/central_bas in the nRF Connect SDK folder structure.

When built as firmware image for a board target with the */ns variant, the sample has Cortex-M Security Extensions (CMSE) enabled and separates the firmware between Non-Secure Processing Environment (NSPE) and Secure Processing Environment (SPE). Because of this, it automatically includes the Trusted Firmware-M (TF-M). To read more about CMSE, see Processing environments.

To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.

Note

When building repository applications in the SDK repositories, building with sysbuild is enabled by default. If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.

Testing

After programming the sample to your development kit, you can test it either by connecting to another kit that is running the Bluetooth: Peripheral HIDS keyboard or Bluetooth: Peripheral HIDS mouse sample, or by using nRF Connect for Desktop that emulates a BAS Server.

Testing with another kit

  1. Connect to the kit that runs this sample with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  2. Reset the kit.

  3. Program the other development kit with the Bluetooth: Peripheral HIDS keyboard or Bluetooth: Peripheral HIDS mouse sample and reset it.

  4. Wait until the BAS Server is detected by the central. In the terminal window, check for information similar to the following:

    The discovery procedure succeeded
    
  5. Observe that the received notifications are output in the terminal window:

    [xx.xx.xx.xx.xx.xx (random)]: Battery notification: 99%
    [xx.xx.xx.xx.xx.xx (random)]: Battery notification: 98%
    [xx.xx.xx.xx.xx.xx (random)]: Battery notification: 97%
    
  6. Press Button 1 to send a read request and process the response:

    Reading BAS value:
    [xx.xx.xx.xx.xx.xx (random)]: Battery read: 97%
    

Testing with nRF Connect for Desktop

  1. Connect to the kit that runs this sample with a terminal emulator (for example, nRF Connect Serial Terminal). See Testing and optimization for the required settings and steps.

  2. Reset the kit.

  3. Start nRF Connect for Desktop.

  4. Open the Bluetooth Low Energy app and select the connected dongle that is used for communication.

  5. Open the SERVER SETUP tab. Click the dongle configuration and select Load setup. Load the hids_keyboard.ncs file that is located under samples/bluetooth/central_bas in the nRF Connect SDK folder structure.

  6. Click Apply to device.

  7. Open the CONNECTION MAP tab. Click the dongle configuration and select Advertising setup.

    The current version of the Bluetooth Low Energy app cannot store the advertising setup, so it must be configured manually. See the following image for the required target configuration:

    Advertising setup for HIDS keyboard simulator

    Advertising setup for HIDS keyboard simulator

    Complete the following steps to configure the advertising setup:

    1. Delete the default Complete local name from Advertising data.

    2. Add a Custom AD type with AD type value set to 19 and Value set to 03c1. This is the GAP Appearance advertising data.

    3. Add a Custom AD type with AD type value set to 01 and Value set to 06. This is the AD data with “General Discoverable” and “BR/EDR not supported” flags set.

    4. Add a UUID 16 bit complete list with two comma-separated values: 1812 and 180F. These are the values for HIDS and BAS.

    5. Add a Complete local name of your choice to the Scan response data.

    6. Click Apply and Close.

  8. In the Adapter settings, select Start advertising.

  9. Wait until the kit that runs the Central BAS sample connects. In the terminal window, check for information similar to the following:

    The discovery procedure succeeded
    
  10. Press Button 1 to send read request and process the response:

    Reading BAS value:
    [xx.xx.xx.xx.xx.xx (random)]: Battery read: 100%
    
  11. Change the value in Battery Service > Battery Level to generate notifications.

  12. Observe that the notification information is displayed:

    [xx.xx.xx.xx.xx.xx (random)]: Battery notification: 99%
    

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr/types.h

  • boards/arm/nrf*/board.h

  • Kernel Services:

    • include/kernel.h

  • Peripherals:

    • include/uart.h

  • API:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/gatt.h

    • include/bluetooth/hci.h

    • include/bluetooth/uuid.h

The sample also uses the following secure firmware component: