1.. _bluetooth-dev:
2
3Developing Bluetooth Applications
4#################################
5
6Bluetooth applications are developed using the common infrastructure and
7approach that is described in the :ref:`application` section of the
8documentation.
9
10Additional information that is only relevant to Bluetooth applications can be
11found in this page.
12
13Thread safety
14*************
15
16Calling into the Bluetooth API is intended to be thread safe, unless otherwise
17noted in the documentation of the API function. The effort to ensure that this
18is the case for all API calls is an ongoing one, but the overall goal is
19formally stated in this paragraph. Bug reports and Pull Requests that move the
20subsystem in the direction of such goal are welcome.
21
22.. _bluetooth-hw-setup:
23
24Hardware setup
25**************
26
27This section describes the options you have when building and debugging Bluetooth
28applications with Zephyr. Depending on the hardware that is available to you,
29the requirements you have and the type of development you prefer you may pick
30one or another setup to match your needs.
31
32There are 4 possible hardware setups to use with Zephyr and Bluetooth:
33
34#. Embedded
35#. QEMU with an external Controller
36#. Native POSIX with an external Controller
37#. Simulated nRF52 with BabbleSim
38
39Embedded
40========
41
42This setup relies on all software running directly on the embedded platform(s)
43that the application is targeting.
44All the :ref:`bluetooth-configs` and :ref:`bluetooth-build-types` are supported
45but you might need to build Zephyr more than once if you are using a dual-chip
46configuration or if you have multiple cores in your SoC each running a different
47build type (e.g., one running the Host, the other the Controller).
48
49To start developing using this setup follow the :ref:`Getting Started Guide
50<getting_started>`, choose one (or more if you are using a dual-chip solution)
51boards that support Bluetooth and then :ref:`run the application
52<application_run_board>`).
53
54.. _bluetooth-hci-tracing:
55
56Embedded HCI tracing
57--------------------
58
59When running both Host and Controller in actual Integrated Circuits, you will
60only see normal log messages on the console by default, without any way of
61accessing the HCI traffic between the Host and the Controller.  However, there
62is a special Bluetooth logging mode that converts the console to use a binary
63protocol that interleaves both normal log messages as well as the HCI traffic.
64Set the following Kconfig options to enable this protocol before building your
65application:
66
67.. code-block:: console
68
69   CONFIG_BT_DEBUG_MONITOR_UART=y
70   CONFIG_UART_CONSOLE=n
71
72Setting :kconfig:option:`CONFIG_BT_DEBUG_MONITOR_UART` to ``y`` replaces the
73:kconfig:option:`CONFIG_BT_DEBUG_LOG` option, and setting :kconfig:option:`CONFIG_UART_CONSOLE`
74to ``n`` disables the default ``printk``/``printf`` hooks.
75
76To decode the binary protocol that will now be sent to the console UART you need
77to use the btmon tool from :ref:`BlueZ <bluetooth_bluez>`:
78
79.. code-block:: console
80
81   $ btmon --tty <console TTY> --tty-speed 115200
82
83Host on Linux with an external Controller
84=========================================
85
86.. note::
87   This is currently only available on GNU/Linux
88
89This setup relies on a "dual-chip" :ref:`configuration <bluetooth-configs>`
90which is comprised of the following devices:
91
92#. A :ref:`Host-only <bluetooth-build-types>` application running in the
93   :ref:`QEMU <application_run_qemu>` emulator or the ``native_posix`` native
94   port of Zephyr
95#. A Controller, which can be one of the following types:
96
97   * A commercially available Controller
98   * A :ref:`Controller-only <bluetooth-build-types>` build of Zephyr
99   * A Virtual controller
100
101.. warning::
102   Certain external Controllers are either unable to accept the Host to
103   Controller flow control parameters that Zephyr sets by default (Qualcomm), or
104   do not transmit any data from the Controller to the Host (Realtek). If you
105   see a message similar to::
106
107     <wrn> bt_hci_core: opcode 0x0c33 status 0x12
108
109   when booting your sample of choice (make sure you have enabled
110   :kconfig:option:`CONFIG_LOG` in your :file:`prj.conf` before running the
111   sample), or if there is no data flowing from the Controller to the Host, then
112   you need to disable Host to Controller flow control. To do so, set
113   ``CONFIG_BT_HCI_ACL_FLOW_CONTROL=n`` in your :file:`prj.conf`.
114
115QEMU
116----
117
118You can run the Zephyr Host on the :ref:`QEMU emulator<application_run_qemu>`
119and have it interact with a physical external Bluetooth Controller.
120Refer to :ref:`bluetooth_qemu_posix` for full instructions on how to build and
121run an application in this setup.
122
123Native POSIX
124------------
125
126.. note::
127   This is currently only available on GNU/Linux
128
129The :ref:`Native POSIX <native_posix>` target builds your Zephyr application
130with the Zephyr kernel, and some minimal HW emulation as a native Linux
131executable.
132This executable is a normal Linux program, which can be debugged and
133instrumented like any other, and it communicates with a physical or virtual
134external Controller.
135
136Refer to :ref:`bluetooth_qemu_posix` for full instructions on how to build and
137run an application with a physical controller. For the virtual controller refer
138to :ref:`bluetooth_virtual_posix`.
139
140Simulated nRF52 with BabbleSim
141==============================
142
143.. note::
144   This is currently only available on GNU/Linux
145
146The :ref:`nrf52_bsim board <nrf52_bsim>`, is a simulated target board
147which emulates the necessary peripherals of a nrf52 SOC to be able to develop
148and test BLE applications.
149This board, uses:
150
151   * `BabbleSim`_ to simulate the nrf52 modem and the radio environment.
152   * The POSIX arch to emulate the processor.
153   * `Models of the nrf52 HW <https://github.com/BabbleSim/ext_NRF52_hw_models/>`_
154
155Just like with the ``native_posix`` target, the build result is a normal Linux
156executable.
157You can find more information on how to run simulations with one or several
158devices in
159:ref:`this board's documentation <nrf52bsim_build_and_run>`
160
161Currently, only :ref:`Combined builds
162<bluetooth-build-types>` are possible, as this board does not yet have
163any models of a UART, or USB which could be used for an HCI interface towards
164another real or simulated device.
165
166
167Initialization
168**************
169
170The Bluetooth subsystem is initialized using the :c:func:`bt_enable`
171function. The caller should ensure that function succeeds by checking
172the return code for errors. If a function pointer is passed to
173:c:func:`bt_enable`, the initialization happens asynchronously, and the
174completion is notified through the given function.
175
176Bluetooth Application Example
177*****************************
178
179A simple Bluetooth beacon application is shown below. The application
180initializes the Bluetooth Subsystem and enables non-connectable
181advertising, effectively acting as a Bluetooth Low Energy broadcaster.
182
183.. literalinclude:: ../../../samples/bluetooth/beacon/src/main.c
184   :language: c
185   :lines: 19-
186   :linenos:
187
188The key APIs employed by the beacon sample are :c:func:`bt_enable`
189that's used to initialize Bluetooth and then :c:func:`bt_le_adv_start`
190that's used to start advertising a specific combination of advertising
191and scan response data.
192
193.. _BabbleSim: https://babblesim.github.io/
194