Building Bluetooth Services in Zephyr

Mark Zachmann
May 7, 2020 · 2 min read

Part 1: Getting Started

Creating a Bluetooth Service in Zephyr is a wonderfully easy task. I’ve had a high startup cost doing simple things like PWM drivers in Zephyr, but Bluetooth is just great.

Overview

The latest version of my application uses the nrf5x library and C++ code to create a standard procedural application. It has a main() and runs in a single thread. It’s about 25,000 lines of code.

Although it’s very efficient in space and resources the entire application had to be built from the ground up. Using Zephyr gives me lots of useful libraries (like Logging) that I don’t have to build from scratch — and that I don’t have to maintain.

The most important thing, though, is that the nrf5x library from Nordic Semiconductor relies on a Bluetooth library that comes as binary only. There is no source code and it only logs to the Nordic logging subsystem — which is useless. Worst of all, you can’t debug anything when the Bluetooth stack is running because it just hard crashes.

So, although the Nordic nrf5x library and Bluetooth driver seem very robust and reliable they are the world’s worst when it comes to developing. Conversely, the Zephyr Bluetooth library is open source and it has extensive logging that goes to the stock Zephyr logger — so you can watch the Bluetooth logging in real-time.

Segger Debug Terminal Output Showing Bluetooth Diagnostics

One reliable undebuggable core vs one open-source seemingly reliable debug-able threaded operating system. No contest. So, I’ve been moving to Zephyr.

Getting Started with Bluetooth

First, one must correctly set the prj.conf bluetooth defines. Here are mine…

# ----------------------------------
# -- bluetooth stuff
# ----------------------------------
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="MyBlueDevice"
CONFIG_BT_SMP=y
# --- we're a peripheral
CONFIG_BT_PERIPHERAL=y
# --- connection definitions
CONFIG_BT_MAX_CONN=16
CONFIG_BT_MAX_PAIRED=4
CONFIG_BT_TINYCRYPT_ECC=n
# --- debugging
CONFIG_BT_CTLR_ASSERT_HANDLER=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_DEBUG_CONN=y
CONFIG_BT_DEBUG_ATT=y
CONFIG_BT_DEBUG_GATT=y

It’s very short. Most of the debugging and connection definitions are optional and have reasonable defaults.

Next there’s a simple bluetooth startup routine. This routine mainly sets up the advertising data (which lists all of the services provided by this Bluetooth peripheral) and the advertising parameters (power, iteration cycle, …) and then starts advertising.

Don’t be confused by the length of the file. The many callbacks just print out a diagnostic.

Since gists flow in Medium this ends part 1:

Home Wireless

Home automation in the wireless IOT era

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store