This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Can't connect to peripheral

I'm trying to connect a 52832 board to a peripheral BLE device.

NRF52832
SD: s132 v7.0.1
SDK 16.0.0

As a first step, I'm not setting flters, I just want to connect to any peripheral. There are lots of advertising peripherals nearby, I can scan and connect to them with nRF Connect for example. 

I'm using code taken from example ble_central_and_peripheral/experimental/ble_app_hrs_rscs_relay/

Here is my init function:

static void scan_init(void)
{
    ret_code_t err_code;
    nrf_ble_scan_init_t init_scan;

    memset(&init_scan, 0, sizeof(init_scan));

    init_scan.p_scan_param     = &m_scan_param;
    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);
}

Here is my start function :

#define SCAN_DURATION 3000
#define APP_BLE_CONN_CFG_TAG 1

/**< Scan parameters requested for scanning and connection. */
static ble_gap_scan_params_t const m_scan_param =
{
    .active        = 0x01,
    .interval      = NRF_BLE_SCAN_SCAN_INTERVAL,
    .window        = NRF_BLE_SCAN_SCAN_WINDOW,
    .filter_policy  = BLE_GAP_SCAN_FP_ACCEPT_ALL, //BLE_GAP_SCAN_FP_WHITELIST,
    .timeout       = SCAN_DURATION,
    .scan_phys     = BLE_GAP_PHY_1MBPS,
};

void ble_scale_scan_start(void)
{
    ret_code_t err_code;

    err_code = nrf_ble_scan_params_set(&m_scan, &m_scan_param);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_start(&m_scan);
    APP_ERROR_CHECK(err_code);
}

Here are the logs :

00> <debug> ble_scan: Scanning parameters have been changed successfully
00> <debug> ble_scan: Scanning
00> <debug> ble_scan: BLE_GAP_SCAN_TIMEOUT
00> <info> app: Scan timed out.
00> <debug> ble_scan: Scanning parameters have been changed successfully
00> <debug> ble_scan: Scanning
00> <debug> ble_scan: BLE_GAP_SCAN_TIMEOUT
00> <info> app: Scan timed out.
00> <debug> ble_scan: Scanning parameters have been changed successfully
00> <debug> ble_scan: Scanning
00> <debug> ble_scan: BLE_GAP_SCAN_TIMEOUT


What am I missing ?
Thanks !

Parents
  • Hi

    When you have the init_scan.connect_if_match = true; set in your scan_init, the scan module automatically conencts after a filter match or successful identification of a device from the whitelist. So if you don't have any filters added to your whitelist, you won't connect to any devices at all. If you add multiple filters (I.E. all the services you would like to connect to) in your scanning module the connect_if_match should connect to the first discovered device matching any criteria.

    I would suggest checking out the scanning module in the SDK to get more information on I.E. filters, triggering connections from scanning, etc. What exactly is your goal here? Just connecting to the first discovered advertising device doesn't seem like the ultimate use case.

    Best regards,

    Simon

Reply
  • Hi

    When you have the init_scan.connect_if_match = true; set in your scan_init, the scan module automatically conencts after a filter match or successful identification of a device from the whitelist. So if you don't have any filters added to your whitelist, you won't connect to any devices at all. If you add multiple filters (I.E. all the services you would like to connect to) in your scanning module the connect_if_match should connect to the first discovered device matching any criteria.

    I would suggest checking out the scanning module in the SDK to get more information on I.E. filters, triggering connections from scanning, etc. What exactly is your goal here? Just connecting to the first discovered advertising device doesn't seem like the ultimate use case.

    Best regards,

    Simon

Children
  • Sorry for the XY problem :)
    Indeed, my goal is connecting to a device by its name, with a filter set. As it was not working, I tried the above, but I understand now why it didn't work.

    Here was my filtering code that doesn't connect to my peripheral.
    "Bala" is the name of mu peripheral, I see it in nRF connect and can connect to it.

    static void scan_init(void)
    {
        ret_code_t err_code;
        nrf_ble_scan_init_t init_scan;
    
        memset(&init_scan, 0, sizeof(init_scan));
    
        init_scan.p_scan_param     = &m_scan_param;
        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);
    
        err_code = nrf_ble_scan_filter_set(&m_scan,
                                           SCAN_NAME_FILTER,
                                           "Bala");
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filters_enable(&m_scan,
                                               NRF_BLE_SCAN_ALL_FILTER,
                                               true);
        APP_ERROR_CHECK(err_code);
    }


    Here are the logs :
    00> <debug> ble_scan: Adding filter on Bala name
    00> <debug> ble_scan: BLE_GAP_SCAN_TIMEOUT
    00> <info> app: Scan timed out.
    00> <debug> ble_scan: Scanning parameters have been changed successfully
    00> <debug> ble_scan: Scanning
    00> <debug> ble_scan: BLE_GAP_SCAN_TIMEOUT

Related