This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to select a peripheral among severals (BLE CENTRAL)

Hi, what I want to achieve is to do a list of devices that I may want to connect to, choose one and pair/bond it (at this point, the central is paired with only one peripheral through its lifetime until some reason makes a new pairing with another peripheral necessary).

What is the procedure to do that ?

For the context : the selection will be made in a menu with buttons and a lcd screen.
My only exposure to BLE is the guides (and trying to understand some examples) therefore some things are left obscure to me.
The central and the peripherals are nRF52840 (chips) and the Softdevice is S140.
I am currently taking the ble_app_hrs example as a canevas.
I looked through the multilink example but it didn't help me because the connections are automatic.

Here is what could be a start (not sure if right) :

The first filter is the UUID, only the devices advertising a certain service (UUID) will be monitored.
Then to do the differentiation between the peripherals, the device name is taken (I tried in the BLE_GAP_EVT_ADV_REPORT event in ble_event_handler(), I can find the name "Nordic_HRM" from the peripheral hrs example).
I read that the device name isn't useful after the connection is made so the adress should be saved too to keep track of the pairing right ? The scanning should keep going until the choice is made, but I didn't understand where (event) I can say to wait and not automatically connect to the first peripheral with a good UUID (as it is in the hrs example).
There is a .connect_if_match element that is put to true in scan_init() but it isn't really used elsewhere (but in nrf_ble_scan_connect_with_target() that is called by nrf_ble_scan_on_adv_report() that is called by nrf_ble_scan_on_ble_evt() that is... never called. Because the main handler used is ble_evt_handler() ?).  So put it false will not help here.

I didn't talk about the whitelist because I understood that it can only be used if the adresses are hardcoded at the initialization right ?


In short : how to properly do a manual pairing/bonding ? does sd_ble_gap_connect() only help to connect and the bonding (writing in the flash) is managed by the peer_manager somehow ?

Thank you !

  • Hi Dagon, 

    I assume you were looking at the ble_app_hrs_c example not ble_app_hrs example. 

    In the example the nrf_ble_scan_on_ble_evt() in nrf_ble_scan.c is used to observe and handle the BLE_GAP_EVT_ADV_REPORT event. It's registered by NRF_SDH_BLE_OBSERVER() macro inside nrf_ble_scan.h

    So when you set connect_if_match = false you will then receive NRF_BLE_SCAN_EVT_FILTER_MATCH event inside scan_evt_handler() for each advertising packet that matched the filter. You can use the data provided in the event to find if it's the device you want to connect to. The information about the advertiser is inside p_scan_evt->filter_match.p_adv_report that you can parse. 

Related