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

scan to connect to a specified address

Hello,

SD: 122

part nrf52833

I need to do the following:

a. scan for advertisements containing a particular MAC address without connecting.  This works.

b. the scanned MAC addresses are reviewed by another MCU. This works.

c. if the MAC address matches, then the MCU will request that a SCAN with a connection starts.

The scanning restarts, but I am not able to get the Connection event?

Here is the scan initialization code that I am using.

init_scan.connect_if_match = true;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;

err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
m_target_periph_addr.addr[0]=0x00;
m_target_periph_addr.addr[1]=0x00;
m_target_periph_addr.addr[2]=0x00;
m_target_periph_addr.addr[3]=0x44;
m_target_periph_addr.addr[4]=0x44;
m_target_periph_addr.addr[5]=0x44;
err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, m_target_periph_addr.addr);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
APP_ERROR_CHECK(err_code);

Thanks,

Dan

Parents
  • Hello,

    First of all, does all the APP_ERROR_CHECK()'s in the snippet that you provided succeed? (are all the err_code == 0?)

    Have you tried to debug the nrf_ble_scan_on_adv_report() in nrf_ble_scan.c? See what adv_addr_compare() returns in an advertising packet that you know comes from the correct device, and why it doesn't return true. Is the filter not enabled as you suspect? Is the address stored properly in the filter? Did you store it in the correct byte order?

    Best regards,

    Edvin

  • Edvin,

    Thanks for your suggestions.  I found that one of my modifications to nrf_ble_scan.c was most of the problem.I do not want the Scan to automatically connect at first pass which it seems to do when scanning with the NRF_BLE_SCAN_EVT_FILTER_MATCH and "connect_if_match" set to false.  I added in an If statement as shown

    below so that the connection only ccurs when the filter on Address and connect if match are true.  Is this the best way to do it?

    The goal is to first scan the first three characters of the Address to match our company ID, then send any found advertisements to a host.  The host will then decide to request a scan with a connect.

    Thanks,

    Dan

  • If you want to modify this during runtime, I suspect that the simplest way would be to handle this in your main.c file, and let nrf_ble_scan.c remain unchanged. This was the way it was done in the earlier versions of the SDK (don't remember what version that changed it). 

    Try to add the BLE_GAP_EVT_ADV_REPORT event to your ble_evt_handler() in main.c, and handle the incoming advertising packets there as well. In this case, you can keep a custom array of the whitelisted addresses that are approved by your host. 

    Something like: (pseudo)

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t            err_code;
        ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
            ...
            
            case BLE_GAP_EVT_ADV_REPORT:
            if (address_is_approved_by_host(p_ble_evt->...address))
            {
                err_code = sd_ble_gap_connect(..., address);
                APP_ERRORO_CHECK(err_code);
            }

    Best regards,

    Edvin

Reply
  • If you want to modify this during runtime, I suspect that the simplest way would be to handle this in your main.c file, and let nrf_ble_scan.c remain unchanged. This was the way it was done in the earlier versions of the SDK (don't remember what version that changed it). 

    Try to add the BLE_GAP_EVT_ADV_REPORT event to your ble_evt_handler() in main.c, and handle the incoming advertising packets there as well. In this case, you can keep a custom array of the whitelisted addresses that are approved by your host. 

    Something like: (pseudo)

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t            err_code;
        ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
            ...
            
            case BLE_GAP_EVT_ADV_REPORT:
            if (address_is_approved_by_host(p_ble_evt->...address))
            {
                err_code = sd_ble_gap_connect(..., address);
                APP_ERRORO_CHECK(err_code);
            }

    Best regards,

    Edvin

Children
No Data
Related