Bluetooth: Peripheral CTS client

The Peripheral CTS client sample demonstrates how to use the GATT Current Time Service (CTS) Client.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp_ns nrf5340dk_nrf5340_cpuapp

nRF52 DK

PCA10040

nrf52dk_nrf52832

nrf52dk_nrf52832

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

When built for an _ns build target, the sample is configured to compile and run as a non-secure application. 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 CTS Server to connect with (for example, a Bluetooth® Low Energy dongle and nRF Connect for Desktop).

Overview

The CTS client sample implements a Current Time Service client. It uses the Current Time Service to read the current time. The time received is printed on the UART.

User interface

LED 1:
  • Blinks with a period of 2 seconds, duty cycle 50%, when the main loop is running.

LED 2:
  • On when connected.

Button 1:
  • Read the current time.

Building and running

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

When built as a non-secure firmware image for the _ns build target, the sample automatically includes the Trusted Firmware-M (TF-M).

To build the sample with Visual Studio Code, follow the steps listed on the Building nRF Connect SDK application quick guide page in the nRF Connect for VS Code extension documentation. See Building and programming an application for other building and programming scenarios and Testing and debugging an application for general information about testing and debugging in the nRF Connect SDK.

Testing

After programming the sample to your development kit, you can test it with nRF Connect for Desktop by performing the following steps.

  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

  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 cts_central.ncs file that is located under samples/bluetooth/peripheral_cts_client in the nRF Connect SDK folder structure.

  6. Click Apply to device.

  7. Open the CONNECTION MAP tab. Click the dongle configuration and select Security parameters. Check Perform Bonding, and click Apply.

  8. Set the value of Current Time Service > Current Time to C2 07 0B 0F 0C 22 38 06 80 02 and click Write.

  9. Connect to the device from the app. The device is advertising as “Nordic_CTS”.

  10. Wait until the bond is established. Verify that the UART data is received as follows:

    Connected xx:xx:xx:xx:xx:xx (random)
    The discovery procedure succeeded
    Security changed: xx:xx:xx:xx:xx:xx (random) level 2
    Pairing completed: xx:xx:xx:xx:xx:xx (random), bonded: 1
    
  11. Press Button 1 on the kit. Verify that the current time printed on the UART matches the time that was input in the Current Time characteristic (UUID 0x2A2B):

    Current Time:
    
    Date:
        Day of week   Saturday
        Day of month  15
        Month of year November
        Year          1986
    
    Time:
        Hours     12
        Minutes   34
        Seconds   56
        Fractions 128/256 of a second
    
    Adjust Reason:
        Daylight savings 0
        Time zone        0
        External update  1
        Manual update    0
    
  12. Change the value of Current Time Service > Current Time to C2 07 0B 0F 0D 25 2A 06 FE 08. It generates a notification. Verify that the current time printed on the UART matches the time that was input:

    Current Time:
    
    Date:
        Day of week   Saturday
        Day of month  15
        Month of year November
        Year          1986
    
    Time:
        Hours     13
        Minutes   37
        Seconds   42
        Fractions 254/256 of a second
    
    Adjust Reason:
        Daylight savings 1
        Time zone        0
        External update  0
        Manual update    0
    
  13. Disconnect the device in nRF Connect.

  14. As bond information is preserved by nRF Connect, you can immediately reconnect to the device by clicking the Connect button again.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/zephyr/types.h

  • lib/libc/minimal/include/errno.h

  • include/sys/printk.h

  • Bluetooth APIs:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/conn.h

    • include/bluetooth/uuid.h

    • include/bluetooth/gatt.h

The sample also uses the following secure firmware component: