Bluetooth mesh: Coexistence with other LE services

This sample demonstrates how to combine Bluetooth® mesh and another Bluetooth Low Energy (LE) service in a single application.

Requirements

This sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF52 DK

PCA10040

nrf52dk_nrf52832

nrf52dk_nrf52832

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

nRF52833 DK

PCA10100

nrf52833dk_nrf52833

nrf52833dk_nrf52833

nRF21540 DK

PCA10112

nrf21540dk_nrf52840

nrf21540dk_nrf52840

The sample also requires a smartphone with nRF Connect for Mobile and one of the following apps:

Overview

The purpose of this sample is to showcase an application where another Bluetooth LE service can operate independently of Bluetooth mesh. It combines the features of the Bluetooth: Peripheral LBS sample and the Bluetooth mesh: Light sample into a single application.

The LED Button Service (LBS) controls the state of a LED and monitors the state of a button on the device. Bluetooth mesh controls and monitors the state of a separate LED on the device. The LBS and Bluetooth mesh do not share any states on the device and operate independently of each other.

To be truly independent, the LBS must be able to advertise its presence independently of Bluetooth mesh. Because Bluetooth mesh uses the advertiser as its main channel of communication, you must configure the application to allow sharing of this resource. To achieve this, extended advertising is enabled with two simultaneous advertising sets. One of these sets is used to handle all Bluetooth mesh communication, while the other is used for advertising the LBS.

Note

Extended advertising is a requirement for achieving multiple advertisers in this sample.

LED Button Service

When connected, the LED Button Service (LBS) sends the state of Button 1 on the development kit to the connected device, such as a phone or tablet. The mobile application on the device can display the received button state and control the state of LED 2 on the development kit.

Mesh provisioning

The provisioning is handled by the Bluetooth mesh provisioning handler for Nordic DKs. It supports four types of out-of-band (OOB) authentication methods, and uses the Hardware Information driver to generate a deterministic UUID to uniquely represent the device.

Mesh models

The following table shows the mesh light composition data for this sample:

Element 1

Config Server

Health Server

Gen. OnOff Server

The models are used for the following purposes:

  • Generic OnOff Server instance in element 1 controls LED 1.

  • Config Server allows configurator devices to configure the node remotely.

  • Health Server provides attention callbacks that are used during provisioning to call your attention to the device. These callbacks trigger blinking of the LEDs.

The model handling is implemented in src/model_handler.c, which uses the DK Buttons and LEDs library to control each LED on the development kit according to the matching received messages of Generic OnOff Server.

User interface

Buttons (Common):

Can be used to input the OOB authentication value during provisioning. All buttons have the same functionality during this procedure.

Button 1:

Send a notification through the LED Button Service with the button pressed or released state.

LEDs (Common):

Show the OOB authentication value during provisioning if the Push button OOB method is used.

LED 1:

Controlled by the Generic OnOff Server.

LED 2:

Controlled remotely by the LED Button Service from the connected device.

Configuration

See Configuring your application for information about how to permanently or temporarily change the configuration.

Source file setup

This sample is split into the following source files:

  • main.c used to handle initialization.

  • model_handler.c used to handle mesh models.

  • lb_service_handler.c used to handle the LBS interaction.

FEM support

You can add support for the nRF21540 front-end module to this sample by using one of the following options, depending on your hardware:

  • Build the sample for one board that contains the nRF21540 FEM, such as nrf21540dk_nrf52840.

  • Manually create a devicetree overlay file that describes how FEM is connected to the nRF5 SoC in your device. See Set devicetree overlays for different ways of adding the overlay file.

  • Provide nRF21540 FEM capabilities by using a shield, for example the nRF21540 EK shield that is available in the nRF Connect SDK. In this case, build the project for a board connected to the shield you are using with an appropriate variable included in the build command, for example SHIELD=nrf21540ek. This variable instructs the build system to append the appropriate devicetree overlay file.

    To build the sample in the nRF Connect for VS Code IDE for an nRF52840 DK with the nRF21540 EK attached, add the shield variable in the build configuration’s Extra CMake arguments and rebuild the build configuration. For example: -DSHIELD=nrf21540ek.

    See nRF Connect for VS Code extension pack documentation for more information.

    See Programming nRF21540 EK for information about how to program when you are using a board with a network core, for example nRF5340 DK.

Each of these options adds the description of the nRF21540 FEM to the devicetree. See Working with RF front-end modules for more information about FEM in the nRF Connect SDK.

To add support for other front-end modules, add the respective devicetree file entries to the board devicetree file or the devicetree overlay file.

Building and running

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

To build the sample with Visual Studio Code, follow the steps listed on the How to build an application 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 Generic OnOff Server (Bluetooth mesh)

After programming the sample to your development kit, you can test it by using a smartphone with nRF Mesh mobile app installed. Testing consists of provisioning the device and configuring it for communication with the Bluetooth mesh models.

Provisioning the device

The provisioning assigns an address range to the device, and adds it to the mesh network. Complete the following steps in the nRF Mesh app:

  1. Tap Add node to start scanning for unprovisioned mesh devices.

  2. Select the Mesh Light device to connect to it.

  3. Tap Identify, and then Provision, to provision the device.

  4. When prompted, select an OOB method and follow the instructions in the app.

Once the provisioning is complete, the app returns to the Network screen.

Configuring models

See Configuring mesh models using the nRF Mesh mobile app for details on how to configure the mesh models with the nRF Mesh mobile app.

Configure the Generic OnOff Server model on the root element of the Mesh and Peripheral Coex node:

  1. Bind the model to Application Key 1. Once the model is bound to the application key, you can control LED 1 on the device.

  2. In the model view, tap ON/OFF (one of the Generic On Off Controls). This switches the LED 1 on the development kit on and off respectively.

Testing LED Button Service (peripheral)

After programming the sample to your development kit, test it by performing the following steps:

  1. Start the nRF Connect for Mobile application on your smartphone or tablet.

  2. Power on the development kit.

  3. Connect to the device from the nRF Connect application. The device is advertising as “Mesh and Peripheral Coex”. The services of the connected device are shown.

  4. In Nordic LED Button Service, enable notifications for the Button characteristic.

  5. Press Button 1 on the device.

  6. Observe that notifications with the following values are displayed:

    • Button released when Button 1 is released.

    • Button pressed when Button 1 is pressed.

  7. Write the following values to the LED characteristic in the Nordic LED Button Service:

    • Value OFF to switch the LED 2 on the development kit off.

    • Value ON to switch the LED 2 on the development kit on.

Dependencies

This sample uses the following nRF Connect SDK libraries:

In addition, it uses the following Zephyr libraries:

  • include/drivers/hwinfo.h

  • include/zephyr/types.h

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

  • include/sys/printk.h

  • include/sys/byteorder.h

  • Kernel Services:

    • include/kernel.h

  • Bluetooth APIs:

    • include/bluetooth/bluetooth.h

    • include/bluetooth/hci.h

    • include/bluetooth/conn.h

    • include/bluetooth/uuid.h

    • include/bluetooth/gatt.h

  • Bluetooth Mesh Profile:

    • include/bluetooth/mesh.h