NCS BLE scanning module: Can’t switch between “scan without connect” to “scan with connect”

I would think a common scan use case for a Central role is to first scan for devices with a certain scan filter criteria. Next, out of that list of filtered devices that were discovered, initiate a connect based on another criteria, such as RSSI, or user selection.

  • The first step would use a scan without connect and, as an example, filter on Bluetooth Service UUID.
  • The second step would use a scan with connect and, as an example, filter on a device address.

Reference the function bt_scan_init() in the NCS nRF file scan.c.

As far as I can see, there is no way to scan without connect and then later switch to scan with connect unless you call bt_scan_init() for each case. The bt_scan_init() provides the only means to change the variable bt_scan.connect_if_match.  However, each time you call bt_scan_init(), the first thing it does is register the NCS scan module’s scan_cb to the Zephyr scanner.  After repeated calls to bt_scan_init(), the scan_cb keeps getting registered as a callback, leading to improper operation because of the multiple calls to the same callback.

Of course the intent was for bt_scan_init() to be called just once.  But was the intent to not allow a change in the variable bt_scan.connect_if_match? Why isn’t there at least an API to change that bt_scan.connect_if_match boolean?

The easiest workaround for me is to modify your code's bt_scan_init() to just unregister the callback before it’s registered.  That allows me to call bt_scan_init() multiple times.  The unregister method handles the case if the callback was never registered to begin with, so that handles the very first init too.

 

bt_le_scan_cb_unregister(&scan_cb);

bt_le_scan_cb_register(&scan_cb);

Can Nordic pretty please consider addressing, what I see, the drawback of not being able to switch between scan with connect and scan without connect?  We want to avoid customizing the NCS, so I’m hoping my workaround above can be removed sometime soon with a change in the NCS BLE scanning module.

Parents Reply Children
  • It turns out that the quick and dirty fix of just calling bt_le_scan_cb_unregister(&scan_cb) in bt_scan_init() won't suffice for my needs.

    So I am now doing what I think is the correct fix of creating an API to  change bt_scan.connect_if_match:

    #if CONFIG_BT_CENTRAL
    void bt_scan_update_connect_if_match(bool new_connect_if_match)
    {
    	bt_scan.connect_if_match = new_connect_if_match;
    }
    #endif /* CONFIG_BT_CENTRAL */
    


    This method is analogous to the existing method to update initial connection parameters: bt_scan_update_connect_if_match().



Related