-
Notifications
You must be signed in to change notification settings - Fork 27
Add scan library #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sondrep
wants to merge
1
commit into
nrfconnect:main
Choose a base branch
from
asilz:scan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add scan library #446
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| .. _lib_ble_scan: | ||
|
|
||
| Bluetooth: Scan | ||
| ############### | ||
|
|
||
| .. contents:: | ||
| :local: | ||
| :depth: 2 | ||
|
|
||
| The Bluetooth® Low Energy Scan library handles scanning for advertising Bluetooth LE devices. | ||
| You can use it to find an advertising device and establish a connection with it. | ||
| You can narrow down the scan to a device of a specific type using scan filters or the whitelist. | ||
|
|
||
| Overview | ||
| ******** | ||
|
|
||
| You can configure this library in one of the following modes: | ||
|
|
||
| * Simple mode without using filters or the whitelist. | ||
| * Advanced mode that allows you to use advanced filters and the whitelist. | ||
|
|
||
| The module can automatically establish a connection on a filter match or when identifying a whitelisted device. | ||
|
|
||
| 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. | ||
|
|
||
| Configuration | ||
| ************* | ||
|
|
||
| To enable the library, set the :kconfig:option:`CONFIG_BLE_SCAN` Kconfig option. | ||
|
|
||
| The library provides the following Kconfig configuration options: | ||
|
|
||
| * :kconfig:option:`CONFIG_BLE_SCAN_BUFFER_SIZE` - Maximum size of an advertising event. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN` - Maximum size for the name to search in the advertisement report. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN` - Maximum size of the short name to search for in the advertisement report. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_FILTER` - Enabling filters for the scanning module. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT` - Maximum number of name filters. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT` - Maximum number of appearance filters. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT` - Maximum number of address filters. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT` - Maximum number of short name filters. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT` - Maximum number of filters for UUIDs. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_INTERVAL` - Determines the scan interval in units of 0.625 millisecond. | ||
| * :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. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_WINDOW` - Determines the scanning window in units of 0.625 milliseconds. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_SLAVE_LATENCY` - Determines the slave latency in counts of connection events. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_MIN_CONNECTION_INTERVAL` - Determines the minimum connection interval in units of 1.25 milliseconds. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_MAX_CONNECTION_INTERVAL` - Determines the maximum connection interval in units of 1.25 milliseconds. | ||
| * :kconfig:option:`CONFIG_BLE_SCAN_SUPERVISION_TIMEOUT` - Determines the supervision time-out in units of 10 millisecond. | ||
|
|
||
| Initialization | ||
| ============== | ||
|
|
||
| The module is initialized by calling the :c:func:`ble_scan_init` function. | ||
| 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. | ||
|
|
||
| Simple initialization | ||
| ===================== | ||
|
|
||
| You can use the simple initialization with the default scanning and connection parameters when you want the library to work in the simple mode without filtering and the whitelist. | ||
|
|
||
| .. code:: c | ||
|
|
||
| BLE_SCAN_DEF(ble_scan); | ||
|
|
||
| void scan_event_handler_func(struct ble_scan_evt const *scan_evt); | ||
|
|
||
| uint32_t nrf_err; | ||
| struct ble_scan_config scan_cfg = { | ||
| .scan_params = BLE_SCAN_SCAN_PARAMS_DEFAULT, | ||
| .conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT, | ||
| .evt_handler = scan_event_handler_func, | ||
| }; | ||
|
|
||
| nrf_err = ble_scan_init(&ble_scan, &scan_cfg); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to initialie scan module, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
eivindj-nordic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Advanced initialization | ||
| ======================= | ||
|
|
||
| The advanced initialization provides a larger configuration set for the library. | ||
| It is required when using scan filters or the whitelist. | ||
|
|
||
| .. code:: c | ||
|
|
||
| BLE_SCAN_DEF(ble_scan); | ||
|
|
||
| void scan_event_handler_func(struct ble_scan_evt const *scan_evt); | ||
|
|
||
| uint32_t nrf_err; | ||
| struct ble_scan_config scan_cfg = { | ||
| .scan_params = { | ||
| .active = 0x01, | ||
| .interval = NRF_BLE_SCAN_INTERVAL, | ||
| .window = NRF_BLE_SCAN_WINDOW, | ||
| .filter_policy = BLE_GAP_SCAN_FP_WHITELIST, | ||
| .timeout = SCAN_DURATION_WHITELIST, | ||
| .scan_phys = BLE_GAP_PHY_1MBPS, | ||
| .extended = true, | ||
| }, | ||
| .conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT, | ||
| .connect_if_match = true, | ||
| .conn_cfg_tag = APP_BLE_CONN_CFG_TAG, | ||
| .evt_handler = scan_event_handler_func, | ||
| }; | ||
|
|
||
| nrf_err = ble_scan_init(&ble_scan, &scan_cfg); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to initialie scan module, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| .. note:: | ||
|
|
||
| When setting connection-specific configurations with the :c:func:`sd_ble_cfg_set` function, you must create a tag for each configuration. | ||
| 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. | ||
|
|
||
| Usage | ||
| ***** | ||
|
|
||
| The application can start scanning by calling the :c:func:`ble_scan_start` function. | ||
|
|
||
| The scan parameters can be updated by calling the :c:func:`ble_scan_params_set` function. | ||
|
|
||
| To stop scanning, call the :c:func:`ble_scan_stop` function. | ||
|
|
||
| The library resumes scanning after receiving advertising reports. | ||
| 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. | ||
|
|
||
| .. note:: | ||
|
|
||
| When you use the :c:func:`ble_scan_params_set` function during the ongoing scan, scanning is stopped. | ||
| To resume scanning, use the :c:func:`ble_scan_start` function. | ||
|
|
||
| Whitelist | ||
| ========= | ||
|
|
||
| The whitelist stores information about all the device connections and bonding. | ||
| If you enable the whitelist, the application receives advertising packets only from the devices that are on the whitelist. | ||
| An advertising package from a whitelisted device generates the :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT` event. | ||
|
|
||
| .. note:: | ||
|
|
||
| When using the whitelist, filters are inactive. | ||
|
|
||
| .. caution:: | ||
|
|
||
| If you use the whitelist, you must pass the event handler during the module initialization. | ||
| The initial scanning with whitelist generates a :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_REQUEST` event. | ||
| The application must react to this event by either setting up the whitelist or switching off the whitelist scan. | ||
| Otherwise, an error is reported when the scan starts. | ||
|
|
||
| Filters | ||
| ======= | ||
|
|
||
| The module can set scanning filters of different type and mode. | ||
| When a filter is matched, it generates a :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event to the main application. | ||
| If the filter matching is enabled and no filter is matched, a :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated. | ||
|
|
||
| The available filter types are: | ||
|
|
||
| * **Name** - Filter set to the target name. | ||
| The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN`. | ||
| The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT`. | ||
| * **Short name** - Filter set to the short target name. | ||
| The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN`. | ||
| The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT`. | ||
| * **Address** - Filter set to the target address. | ||
| The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT`. | ||
| * **UUID** - Filter set to the target UUID. | ||
| The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT`. | ||
| * **Appearance** - Filter set to the target appearance. | ||
| The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT`. | ||
|
|
||
| The following two filter modes are available: | ||
|
|
||
| * **Normal** - Only one of the filters set, regardless of the type, must be matched to generate an event. | ||
| * **Multifilter** - At least one filter from each filter type you set must be matched to generate an event. | ||
| For UUID filters, all specified UUIDs must match in this mode. | ||
| Enable multifilter by setting the :c:macro:`match_all` argument to true when calling the :c:func:`ble_scan_filters_enable` function. | ||
|
|
||
| Multifilter example: | ||
|
|
||
| Several filters are set for name, address, UUID, and appearance. | ||
| To generate the :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event, the following types must match: | ||
|
|
||
| * One of the address filters. | ||
| * One of the name filters. | ||
| * One of the appearance filters. | ||
| * All UUID filters. | ||
|
|
||
| Otherwise, the :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated. | ||
|
|
||
eivindj-nordic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| You can enable filters by calling the :c:func:`ble_scan_filters_enable` function after initialization. | ||
| You can activate filters for one filter type, or for a combination of several filter types. | ||
|
|
||
| .. code:: c | ||
|
|
||
| BLE_SCAN_DEF(ble_scan); | ||
|
|
||
| uint8_t addr[BLE_GAP_ADDR_LEN] = {0xa, 0xd, 0xd, 0x4, 0xe, 0x5}; | ||
| char *device_name = "my_device"; | ||
|
|
||
| /* See above code snippet for initialization */ | ||
|
|
||
| /* Enable filter for name and address in normal mode */ | ||
| nrf_err = ble_scan_filters_enable(&ble_scan, BLE_SCAN_NAME_FILTER | BLE_SCAN_ADDR_FILTER, false); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| /* Add address to scan filter */ | ||
| nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_ADDR_FILTER, addr); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to add address scan filter, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| /* Add name to scan filter */ | ||
| nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_NAME_FILTER, device_name); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to add name scan filter, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| /* Start scanning */ | ||
| nrf_err = ble_scan_start(&ble_scan); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to start scan, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| /* Start scanning */ | ||
| ble_scan_stop(&ble_scan); | ||
|
|
||
| /* Disable filters */ | ||
| nrf_err = ble_scan_filters_disable(&ble_scan); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to disable scan filters, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| /* Remove all scan filters */ | ||
| nrf_err = ble_scan_all_filter_remove(&ble_scan); | ||
| if (nrf_err) { | ||
| LOG_ERR("Failed to remove scan filters, nrf_error %#x", nrf_err); | ||
| } | ||
|
|
||
| Dependencies | ||
| ************ | ||
|
|
||
| This library uses the following |BMshort| libraries: | ||
|
|
||
| * SoftDevice - :kconfig:option:`CONFIG_SOFTDEVICE` | ||
| * SoftDevice handler - :kconfig:option:`CONFIG_NRF_SDH` | ||
|
|
||
| API documentation | ||
| ***************** | ||
|
|
||
| | Header file: :file:`include/bm/bluetooth/ble_scan.h` | ||
| | Source files: :file:`lib/ble_scan/` | ||
|
|
||
| :ref:`Bluetooth LE Scan library API reference <api_ble_scan>` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.