|
| 1 | +.. _lib_ble_scan: |
| 2 | + |
| 3 | +Bluetooth: Scan |
| 4 | +############### |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +The Bluetooth® Low Energy Scan library handles scanning for advertising Bluetooth LE devices. |
| 11 | +You can use it to find an advertising device and establish a connection with it. |
| 12 | +You can narrow down the scan to a device of a specific type using scan filters or the whitelist. |
| 13 | + |
| 14 | +Overview |
| 15 | +******** |
| 16 | + |
| 17 | +You can configure this library in one of the following modes: |
| 18 | + |
| 19 | +* Simple mode without using filters or the whitelist. |
| 20 | +* Advanced mode that allows you to use advanced filters and the whitelist. |
| 21 | + |
| 22 | +The library can automatically establish a connection on a filter match or when identifying a whitelisted device. |
| 23 | + |
| 24 | +The library registers as a Bluetooth LE event observer using the :c:macro:`NRF_SDH_BLE_OBSERVER` macro and handles the relevant Bluetooth LE events from the SoftDevice. |
| 25 | + |
| 26 | +.. note:: |
| 27 | + |
| 28 | + This library require a SoftDevice with central support. |
| 29 | + |
| 30 | +Configuration |
| 31 | +************* |
| 32 | + |
| 33 | +To enable the library, set the :kconfig:option:`CONFIG_BLE_SCAN` Kconfig option. |
| 34 | + |
| 35 | +The library provides the following Kconfig configuration options: |
| 36 | + |
| 37 | +* :kconfig:option:`CONFIG_BLE_SCAN_BUFFER_SIZE` - Maximum size of an advertising event. |
| 38 | +* :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN` - Maximum size of the name to search for in the advertisement report. |
| 39 | +* :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN` - Maximum size of the short name to search for in the advertisement report. |
| 40 | +* :kconfig:option:`CONFIG_BLE_SCAN_FILTER` - Enabling filters for the scan library. |
| 41 | +* :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT` - Maximum number of name filters. |
| 42 | +* :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT` - Maximum number of appearance filters. |
| 43 | +* :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT` - Maximum number of address filters. |
| 44 | +* :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT` - Maximum number of short name filters. |
| 45 | +* :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT` - Maximum number of filters for UUIDs. |
| 46 | +* :kconfig:option:`CONFIG_BLE_SCAN_INTERVAL` - Determines the scan interval in units of 0.625 ms. |
| 47 | +* :kconfig:option:`CONFIG_BLE_SCAN_DURATION` - Duration of a scanning session in units of 10 ms, if set to 0, the scanning continues until it is explicitly disabled. |
| 48 | +* :kconfig:option:`CONFIG_BLE_SCAN_WINDOW` - Determines the scanning window in units of 0.625 ms. |
| 49 | +* :kconfig:option:`CONFIG_BLE_SCAN_SLAVE_LATENCY` - Determines the slave latency in counts of connection events. |
| 50 | +* :kconfig:option:`CONFIG_BLE_SCAN_MIN_CONNECTION_INTERVAL` - Determines the minimum connection interval in units of 1.25 ms. |
| 51 | +* :kconfig:option:`CONFIG_BLE_SCAN_MAX_CONNECTION_INTERVAL` - Determines the maximum connection interval in units of 1.25 ms. |
| 52 | +* :kconfig:option:`CONFIG_BLE_SCAN_SUPERVISION_TIMEOUT` - Determines the supervision time-out in units of 10 ms. |
| 53 | + |
| 54 | +Initialization |
| 55 | +============== |
| 56 | + |
| 57 | +The library is initialized by calling the :c:func:`ble_scan_init` function. |
| 58 | +The application can provide an event handler to receive events to inform about a filter or whitelist match, or about a connection error during the automatic connection. |
| 59 | + |
| 60 | +Simple initialization |
| 61 | +===================== |
| 62 | + |
| 63 | +You can use the simple initialization with the default scanning and connection parameters when you want the library to work in the simple mode. |
| 64 | + |
| 65 | + |
| 66 | +.. code-block:: c |
| 67 | +
|
| 68 | + BLE_SCAN_DEF(ble_scan); |
| 69 | +
|
| 70 | + void scan_event_handler_func(struct ble_scan_evt const *scan_evt); |
| 71 | +
|
| 72 | + uint32_t nrf_err; |
| 73 | + struct ble_scan_config scan_cfg = { |
| 74 | + .scan_params = BLE_SCAN_SCAN_PARAMS_DEFAULT, |
| 75 | + .conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT, |
| 76 | + .evt_handler = scan_event_handler_func, |
| 77 | + }; |
| 78 | +
|
| 79 | + nrf_err = ble_scan_init(&ble_scan, &scan_cfg); |
| 80 | + if (nrf_err) { |
| 81 | + LOG_ERR("Failed to initialie scan library, nrf_error %#x", nrf_err); |
| 82 | + } |
| 83 | +
|
| 84 | +Advanced initialization |
| 85 | +======================= |
| 86 | + |
| 87 | +The advanced initialization provides a larger configuration set for the library. |
| 88 | +It is required when using scan filters or the whitelist. |
| 89 | + |
| 90 | +.. code-block:: c |
| 91 | +
|
| 92 | + BLE_SCAN_DEF(ble_scan); |
| 93 | +
|
| 94 | + void scan_event_handler_func(struct ble_scan_evt const *scan_evt); |
| 95 | +
|
| 96 | + uint32_t nrf_err; |
| 97 | + struct ble_scan_config scan_cfg = { |
| 98 | + .scan_params = { |
| 99 | + .active = 0x01, |
| 100 | + .interval = NRF_BLE_SCAN_INTERVAL, |
| 101 | + .window = NRF_BLE_SCAN_WINDOW, |
| 102 | + .filter_policy = BLE_GAP_SCAN_FP_WHITELIST, |
| 103 | + .timeout = SCAN_DURATION_WHITELIST, |
| 104 | + .scan_phys = BLE_GAP_PHY_1MBPS, |
| 105 | + .extended = true, |
| 106 | + }, |
| 107 | + .conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT, |
| 108 | + .connect_if_match = true, |
| 109 | + .conn_cfg_tag = APP_BLE_CONN_CFG_TAG, |
| 110 | + .evt_handler = scan_event_handler_func, |
| 111 | + }; |
| 112 | +
|
| 113 | + nrf_err = ble_scan_init(&ble_scan, &scan_cfg); |
| 114 | + if (nrf_err) { |
| 115 | + LOG_ERR("Failed to initialize scan library, nrf_error %#x", nrf_err); |
| 116 | + } |
| 117 | +
|
| 118 | +.. note:: |
| 119 | + |
| 120 | + When setting connection-specific configurations with the :c:func:`sd_ble_cfg_set` function, you must create a tag for each configuration. |
| 121 | + If your application uses the library with automatic connection, this tag must be provided when calling the :c:func:`sd_ble_gap_scan_start` or the :c:func:`sd_ble_gap_connect` function. |
| 122 | + |
| 123 | +Usage |
| 124 | +***** |
| 125 | + |
| 126 | +The application can start scanning by calling the :c:func:`ble_scan_start` function. |
| 127 | + |
| 128 | +The scan parameters can be updated by calling the :c:func:`ble_scan_params_set` function. |
| 129 | + |
| 130 | +To stop scanning, call the :c:func:`ble_scan_stop` function. |
| 131 | + |
| 132 | +The library resumes scanning after receiving advertising reports. |
| 133 | +Scanning stops if the library establishes a connection automatically, or if the application calls the :c:func:`ble_scan_stop` or the :c:func:`sd_ble_gap_connect` function. |
| 134 | + |
| 135 | +.. note:: |
| 136 | + |
| 137 | + When you use the :c:func:`ble_scan_params_set` function during the ongoing scan, scanning is stopped. |
| 138 | + To resume scanning, use the :c:func:`ble_scan_start` function. |
| 139 | + |
| 140 | +Whitelist |
| 141 | +========= |
| 142 | + |
| 143 | +The whitelist stores information about all the device connections and bonding. |
| 144 | +If you enable the whitelist, the application receives advertising packets only from the devices that are on the whitelist. |
| 145 | +An advertising package from a whitelisted device generates the :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT` event. |
| 146 | + |
| 147 | +.. note:: |
| 148 | + |
| 149 | + When using the whitelist, filters are inactive. |
| 150 | + |
| 151 | +.. caution:: |
| 152 | + |
| 153 | + If you use the whitelist, you must pass the event handler during the library initialization. |
| 154 | + The initial scanning with whitelist generates a :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_REQUEST` event. |
| 155 | + The application must react to this event by either setting up the whitelist or switching off the whitelist scan. |
| 156 | + Otherwise, an error is reported when the scan starts. |
| 157 | + |
| 158 | +Filters |
| 159 | +======= |
| 160 | + |
| 161 | +The library can set scanning filters of different type and mode. |
| 162 | +When a filter is matched, it generates a :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event to the main application. |
| 163 | +If the filter matching is enabled and no filter is matched, a :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated. |
| 164 | + |
| 165 | +The available filter types are: |
| 166 | + |
| 167 | +* **Name** - Filter set to the target name. |
| 168 | + The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN`. |
| 169 | + The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT`. |
| 170 | +* **Short name** - Filter set to the short target name. |
| 171 | + The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN`. |
| 172 | + The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT`. |
| 173 | +* **Address** - Filter set to the target address. |
| 174 | + The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT`. |
| 175 | +* **UUID** - Filter set to the target UUID. |
| 176 | + The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT`. |
| 177 | +* **Appearance** - Filter set to the target appearance. |
| 178 | + The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT`. |
| 179 | + |
| 180 | +The following two filter modes are available: |
| 181 | + |
| 182 | +* **Normal** - Only one of the filters set, regardless of the type, must be matched to generate an event. |
| 183 | +* **Multifilter** - At least one filter from each filter type you set must be matched to generate an event. |
| 184 | + For UUID filters, all specified UUIDs must match in this mode. |
| 185 | + Enable multifilter by setting the :c:macro:`match_all` argument to true when calling the :c:func:`ble_scan_filters_enable` function. |
| 186 | + |
| 187 | +Multifilter example: |
| 188 | + |
| 189 | +Several filters are set for name, address, UUID, and appearance. |
| 190 | +To generate the :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event, the following types must match: |
| 191 | + |
| 192 | +* One of the address filters. |
| 193 | +* One of the name filters. |
| 194 | +* One of the appearance filters. |
| 195 | +* All UUID filters. |
| 196 | + |
| 197 | +Otherwise, the :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated. |
| 198 | + |
| 199 | +You can enable filters by calling the :c:func:`ble_scan_filters_enable` function after initialization. |
| 200 | +You can activate filters for one filter type, or for a combination of several filter types. |
| 201 | + |
| 202 | +.. code-block:: c |
| 203 | +
|
| 204 | + BLE_SCAN_DEF(ble_scan); |
| 205 | +
|
| 206 | + uint8_t addr[BLE_GAP_ADDR_LEN] = {0xa, 0xd, 0xd, 0x4, 0xe, 0x5}; |
| 207 | + char *device_name = "my_device"; |
| 208 | +
|
| 209 | + /* See above code snippet for initialization */ |
| 210 | +
|
| 211 | + /* Enable filter for name and address in normal mode */ |
| 212 | + nrf_err = ble_scan_filters_enable(&ble_scan, BLE_SCAN_NAME_FILTER | BLE_SCAN_ADDR_FILTER, false); |
| 213 | + if (nrf_err) { |
| 214 | + LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err); |
| 215 | + } |
| 216 | +
|
| 217 | + /* Add address to scan filter */ |
| 218 | + nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_ADDR_FILTER, addr); |
| 219 | + if (nrf_err) { |
| 220 | + LOG_ERR("Failed to add address scan filter, nrf_error %#x", nrf_err); |
| 221 | + } |
| 222 | +
|
| 223 | + /* Add name to scan filter */ |
| 224 | + nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_NAME_FILTER, device_name); |
| 225 | + if (nrf_err) { |
| 226 | + LOG_ERR("Failed to add name scan filter, nrf_error %#x", nrf_err); |
| 227 | + } |
| 228 | +
|
| 229 | + /* Start scanning */ |
| 230 | + nrf_err = ble_scan_start(&ble_scan); |
| 231 | + if (nrf_err) { |
| 232 | + LOG_ERR("Failed to start scan, nrf_error %#x", nrf_err); |
| 233 | + } |
| 234 | +
|
| 235 | + /* Start scanning */ |
| 236 | + ble_scan_stop(&ble_scan); |
| 237 | +
|
| 238 | + /* Disable filters */ |
| 239 | + nrf_err = ble_scan_filters_disable(&ble_scan); |
| 240 | + if (nrf_err) { |
| 241 | + LOG_ERR("Failed to disable scan filters, nrf_error %#x", nrf_err); |
| 242 | + } |
| 243 | +
|
| 244 | + /* Remove all scan filters */ |
| 245 | + nrf_err = ble_scan_all_filter_remove(&ble_scan); |
| 246 | + if (nrf_err) { |
| 247 | + LOG_ERR("Failed to remove scan filters, nrf_error %#x", nrf_err); |
| 248 | + } |
| 249 | +
|
| 250 | +Dependencies |
| 251 | +************ |
| 252 | + |
| 253 | +This library uses the following |BMshort| libraries: |
| 254 | + |
| 255 | +* SoftDevice - :kconfig:option:`CONFIG_SOFTDEVICE` |
| 256 | +* SoftDevice handler - :kconfig:option:`CONFIG_NRF_SDH` |
| 257 | + |
| 258 | +API documentation |
| 259 | +***************** |
| 260 | + |
| 261 | +| Header file: :file:`include/bm/bluetooth/ble_scan.h` |
| 262 | +| Source files: :file:`lib/bluetooth/ble_scan/` |
| 263 | +
|
| 264 | +:ref:`Bluetooth LE Scan library API reference <api_ble_scan>` |
0 commit comments