|
| 1 | +.. _lib_ble_adv: |
| 2 | + |
| 3 | +Bluetooth: Advertising library |
| 4 | +############################## |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +The advertising library handles the Bluetooth LE advertising of your application. |
| 11 | +It supports a variety of common advertising scenarios. |
| 12 | +For non-connectable advertising, the library serves as a reference together with the :ref:`api_ble_adv_data_encoder` API. |
| 13 | + |
| 14 | +Overview |
| 15 | +******** |
| 16 | + |
| 17 | +The library covers the most common advertising behaviors and can be adjusted by enabling or disabling specific :ref:`advertising modes <lib_ble_adv_modes>`. |
| 18 | + |
| 19 | +The library event handler allows for the extension of advertising functionalities, such as adding indicators for active advertising modes. |
| 20 | + |
| 21 | +.. _lib_ble_adv_modes: |
| 22 | + |
| 23 | +Advertising modes |
| 24 | +================= |
| 25 | + |
| 26 | +The library supports the following connectable advertising modes: |
| 27 | + |
| 28 | +* **Directed High Duty** - After disconnecting, the application immediately attempts to reconnect to the most recently connected peer. |
| 29 | + This advertising mode is useful for staying connected to one peer and seamlessly recovering from accidental disconnects. |
| 30 | + This mode is only allowed for limited duration since it has a high chance of blocking other wireless traffic. |
| 31 | + Directed High Duty will only work if extended advertising is disabled. |
| 32 | +* **Directed** - Direct advertising to the last connected peer. |
| 33 | + This mode has a lower duty cycle compared to Directed High Duty. |
| 34 | +* **Fast** - Rapidly advertise to surrounding devices. |
| 35 | +* **Slow** - Advertise to surrounding devices with a longer advertising interval than in Fast advertising mode. |
| 36 | + This advertising mode conserves power and generates less traffic for other wireless devices that might be present. |
| 37 | + Finding a device and connecting to it might take more time in Slow advertising mode. |
| 38 | +* **Idle** (Advertising off) - The application stops advertising. |
| 39 | + |
| 40 | +The advertising modes are enabled by using Kconfig options. |
| 41 | +See the :ref:`lib_ble_adv_configure` section below. |
| 42 | + |
| 43 | +When advertising, the library transitions through the enabled advertising modes until a connection is made or advertising times out. |
| 44 | +The flow of advertising is: Direct High Duty -> Direct -> Fast -> Slow -> Idle. |
| 45 | + |
| 46 | +If you start advertising in Direct mode, the library first attempts Direct advertising. |
| 47 | +If no information about a previous connection is available or the previous peer is not available, the library starts Fast advertising. |
| 48 | +If no peer connects before the Fast advertising times out, the application moves on to Slow advertising with a longer advertising interval. |
| 49 | +If no peer connects before the configured timeout, advertising stops. |
| 50 | + |
| 51 | +Disabled advertising modes are skipped (Directed High Duty is always skipped if extended advertising is enabled). |
| 52 | + |
| 53 | +You can configure the timeout thresholds and advertising intervals for Direct, Fast, and Slow advertising. |
| 54 | +Direct High Duty is a one-shot burst and its timeout threshold and advertising interval cannot be configured. |
| 55 | + |
| 56 | +Whitelist |
| 57 | +========= |
| 58 | + |
| 59 | +Whitelist advertising affects the filtering parameters for Fast and Slow advertising modes. |
| 60 | +The whitelist stores information about all devices that have been connected before. |
| 61 | +If you enable the use of the whitelist, the application specifically advertises to the devices that are on the whitelist. |
| 62 | + |
| 63 | +You can enable or disable whitelist advertising by using Kconfig options. |
| 64 | +After initialization, you can temporarily disable whitelist advertising for one connection using the :c:func:`ble_adv_restart_without_whitelist` function. |
| 65 | +After the device disconnects, the whitelist will take effect again. |
| 66 | + |
| 67 | +To permanently disable whitelist advertising, you must disable it in Kconfig. |
| 68 | + |
| 69 | +.. _lib_ble_adv_configure: |
| 70 | + |
| 71 | +Configuration |
| 72 | +************* |
| 73 | + |
| 74 | +The library is enabled and configured using the Kconfig system: |
| 75 | + |
| 76 | +* General settings: |
| 77 | + |
| 78 | + * :kconfig:option:`CONFIG_BLE_ADV` - Enables the advertising library. |
| 79 | + * :kconfig:option:`CONFIG_BLE_ADV_NAME` - Sets the advertising name of the device. |
| 80 | + * :kconfig:option:`CONFIG_BLE_ADV_RESTART_ON_DISCONNECT`- Starts advertising upon disconnect. |
| 81 | + |
| 82 | +* Directed advertising settings: |
| 83 | + |
| 84 | + * :kconfig:option:`CONFIG_BLE_ADV_DIRECTED_ADVERTISING` - Enables Directed advertising. |
| 85 | + * :kconfig:option:`CONFIG_BLE_ADV_DIRECTED_ADVERTISING_HIGH_DUTY` - Enables Directed High Duty advertising. |
| 86 | + * :kconfig:option:`CONFIG_BLE_ADV_DIRECTED_ADVERTISING_TIMEOUT` - Sets the Directed advertising timeout in units of 10 ms. |
| 87 | + * :kconfig:option:`CONFIG_BLE_ADV_DIRECTED_ADVERTISING_INTERVAL` - Sets the Directed advertising interval in units of 0.625 ms. |
| 88 | + |
| 89 | +* Fast advertising settings: |
| 90 | + |
| 91 | + * :kconfig:option:`CONFIG_BLE_ADV_FAST_ADVERTISING` - Enables Fast advertising. |
| 92 | + * :kconfig:option:`CONFIG_BLE_ADV_FAST_ADVERTISING_TIMEOUT` - Sets the Fast advertising timeout in units of 10 ms. |
| 93 | + * :kconfig:option:`CONFIG_BLE_ADV_FAST_ADVERTISING_INTERVAL` - Sets the Fast advertising interval in units of 0.625 ms. |
| 94 | + |
| 95 | +* Slow advertising settings: |
| 96 | + |
| 97 | + * :kconfig:option:`CONFIG_BLE_ADV_SLOW_ADVERTISING` - Enables Slow advertising. |
| 98 | + * :kconfig:option:`CONFIG_BLE_ADV_SLOW_ADVERTISING_TIMEOUT` - Sets the Slow advertising timeout in units of 10 ms. |
| 99 | + * :kconfig:option:`CONFIG_BLE_ADV_SLOW_ADVERTISING_INTERVAL` - Sets the Slow advertising interval in units of 0.625 ms. |
| 100 | + |
| 101 | +* Whitelist and extended advertising: |
| 102 | + |
| 103 | + * :kconfig:option:`CONFIG_BLE_ADV_USE_WHITELIST` - Enables the use of a whitelist. |
| 104 | + * :kconfig:option:`CONFIG_BLE_ADV_EXTENDED_ADVERTISING` - Enables extended advertising. |
| 105 | + |
| 106 | +* PHY-related settings: |
| 107 | + |
| 108 | + * :kconfig:option:`CONFIG_BLE_ADV_PRIMARY_PHY_AUTO` - Sets the primary PHY to auto. |
| 109 | + * :kconfig:option:`CONFIG_BLE_ADV_PRIMARY_PHY_1MBPS` - Sets the primary PHY to 1 Mbit/s. |
| 110 | + * :kconfig:option:`CONFIG_BLE_ADV_PRIMARY_PHY_2MBPS` - Sets the primary PHY to 2 Mbit/s. |
| 111 | + * :kconfig:option:`CONFIG_BLE_ADV_PRIMARY_PHY_CODED` - Sets the primary PHY to coded PHY. |
| 112 | + * :kconfig:option:`CONFIG_BLE_ADV_SECONDARY_PHY_AUTO` - Sets the secondary PHY to auto. |
| 113 | + * :kconfig:option:`CONFIG_BLE_ADV_SECONDARY_PHY_1MBPS` - Sets the secondary PHY to 1 Mbit/s. |
| 114 | + * :kconfig:option:`CONFIG_BLE_ADV_SECONDARY_PHY_2MBPS` - Sets the secondary PHY to 2 Mbit/s. |
| 115 | + * :kconfig:option:`CONFIG_BLE_ADV_SECONDARY_PHY_CODED` - Sets the secondary PHY to coded PHY. |
| 116 | + |
| 117 | +Initialization |
| 118 | +============== |
| 119 | + |
| 120 | +The library is initialized by calling the :c:func:`ble_adv_init` function. |
| 121 | +See the :c:struct:`ble_adv_config` struct for configuration details. |
| 122 | + |
| 123 | +Usage |
| 124 | +***** |
| 125 | + |
| 126 | +Before compiling your application, enable the intended :ref:`advertising modes <lib_ble_adv_modes>` through the Kconfig system. |
| 127 | + |
| 128 | +Make sure to provide an event handler that is called when advertising transitions to a new mode. |
| 129 | +You can then use this event handler to add functionality, for example to indicate mode transitions to the user by flashing an LED when advertising starts, or to power down the application when no peer is found. |
| 130 | + |
| 131 | +If enabled, the event handler must handle requests to update the whitelist (:c:enum:`BLE_ADV_EVT_WHITELIST_REQUEST`) and peer address (:c:enum:`BLE_ADV_EVT_PEER_ADDR_REQUEST`). |
| 132 | +If the peer address request event is ignored, the Directed advertising mode cannot be used. |
| 133 | +Likewise, if the whitelist request event is ignored, the Fast and Slow advertising modes will not use the whitelist. |
| 134 | + |
| 135 | +When replying to the :c:enum:`BLE_ADV_EVT_WHITELIST_REQUEST` event, the application must provide the whitelist in the following way: |
| 136 | + |
| 137 | +* If the application uses :ref:`lib_peer_manager`: Retrieve the whitelist by calling the :c:func:`pm_whitelist_get` function. |
| 138 | + Make sure that the :c:func:`pm_whitelist_set` function was previously called. |
| 139 | + Then, call the :c:func:`ble_adv_whitelist_reply` function with the output of the :c:func:`pm_whitelist_get` function. |
| 140 | +* If the application does not use :ref:`lib_peer_manager`: call the :c:func:`sd_ble_gap_whitelist_set` function. Then, call the :c:func:`ble_advertising_whitelist_reply` function. |
| 141 | + After initialization, call the :c:func:`ble_advertising_start` function to start advertising in the intended mode. |
| 142 | + |
| 143 | +The application must reply to the :c:enum:`BLE_ADV_EVT_PEER_ADDR_REQUEST` event by calling the :c:func:`ble_adv_peer_addr_reply` function. |
| 144 | + |
| 145 | +.. note:: |
| 146 | + |
| 147 | + When setting connection-specific configurations using the :c:func:`sd_ble_cfg_set` function, you must create a tag for each configuration. |
| 148 | + This tag must be provided when calling the :c:func:`sd_ble_gap_adv_start` function and the :c:func:`sd_ble_gap_connect` function. |
| 149 | + If your application uses the advertising library, you must call the :c:func:`ble_advertising_conn_cfg_tag_set` function before starting advertising. |
| 150 | + |
| 151 | +Dependencies |
| 152 | +************ |
| 153 | + |
| 154 | +This library uses the following |BMshort| libraries: |
| 155 | + |
| 156 | +* SoftDevice - :kconfig:option:`CONFIG_SOFTDEVICE` |
| 157 | +* SoftDevice handler - :kconfig:option:`CONFIG_NRF_SDH` |
| 158 | + |
| 159 | +API documentation |
| 160 | +***************** |
| 161 | + |
| 162 | +| Header file: :file:`include/bm/bluetooth/ble_adv.h` |
| 163 | +| Source files: :file:`lib/bluetooth/ble_adv/` |
| 164 | +
|
| 165 | +:ref:`Bluetooth LE Advertising library API reference <api_ble_adv>` |
| 166 | + |
| 167 | +| Header file: :file:`include/bm/bluetooth/ble_adv_data.h` |
| 168 | +| Source files: :file:`lib/bluetooth/ble_adv/` |
| 169 | +
|
| 170 | +:ref:`Advertising and Scan Response Data Encoder API reference <api_ble_adv_data_encoder>` |
0 commit comments