Skip to content

Commit 455f1a4

Browse files
lib: bluetooth: ble_scan: aligning with other libraries
Align ble_scan with other libraries and some cleanup. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 78ea29e commit 455f1a4

File tree

8 files changed

+1804
-971
lines changed

8 files changed

+1804
-971
lines changed

doc/nrf-bm/api/api.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ Bluetooth LE Radio Notification library
5757
:inner:
5858
:members:
5959

60+
.. _api_ble_scan:
61+
62+
Bluetooth LE Scan library
63+
=======================================
64+
65+
.. doxygengroup:: ble_scan
66+
:inner:
67+
:members:
68+
6069
.. _api_bm_buttons:
6170

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

0 commit comments

Comments
 (0)