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.

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, you can request to read the battery level by pressing Button 1.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp

nrf5340dk_nrf5340_cpuappns

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

nRF52840 DK (emulating nRF52811)

PCA10056

nrf52840dk_nrf52811

nrf52840dk_nrf52811

nRF52833 DK

PCA10010

nrf52833dk_nrf52833

nrf52833dk_nrf52833

nRF52833 DK (emulating nRF52820)

PCA10100

nrf52833dk_nrf52820

nrf52833dk_nrf52820

nRF52 DK

PCA10040

nrf52dk_nrf52832

nrf52dk_nrf52832

nRF52 DK (emulating nRF52810)

PCA10040

nrf52dk_nrf52810

nrf52dk_nrf52810

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 Bluetooth Low Energy dongle and nRF Connect for Desktop)

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.

See Building and programming a sample application for information about how to build and program the application.

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, PuTTY). See How to connect with PuTTY for the required settings.

  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, PuTTY). See How to connect with PuTTY for the required settings.

  2. Reset the kit.

  3. Start nRF Connect for Desktop and select the connected dongle that is used for communication.

  4. Go to 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.

  5. Click Apply to device.

  6. Go to the Connection Map tab. Click the dongle configuration and select Advertising setup.

    The current version of nRF Connect 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.

  7. In the Adapter settings, choose Start advertising.

  8. 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
    
  9. Press Button 1 to send read request and process the response:

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

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

  • Bluetooth:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/gatt.h

    • include/bluetooth/hci.h

    • include/bluetooth/uuid.h