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

Can't connect central/peripheral relay to peripheral

Hi

So, Im trying to repeat the ble_central_and_peripheral example but with my own services. To be easier let's call the service  "snsr". So, I did the snsr.c and the snsr_c. I didn't get any errors compiling in both cases. I tried to keep as close as possible to the original example too. So I alter the relay example adapted to search to snsr instead of the default services in a nRF52 board (central/peripheral), and I ran the snsr.c in another nRF52 board, to be the peripheral. With my phone I can detect and connect to the relay board but that board can´t connect to snsr board.

Can someone help me what I can be missing?

This is my ble_snsr_init at ble_snsr.c 

uint32_t ble_snsr_init(ble_snsr_t * p_snsr, const ble_snsr_init_t * p_snsr_init)
{

  uint32_t err_code;
  ble_uuid_t ble_uuid;

  // Initialize service structure
  p_snsr->conn_handle = BLE_CONN_HANDLE_INVALID;
  p_snsr->evt_handler = p_snsr_init->evt_handler;

  // Add service
  ble_uuid128_t base_uuid = SENSOR_UUID_BASE;
  err_code = sd_ble_uuid_vs_add(&base_uuid, &(p_snsr->uuid_type));
 
  if (err_code != NRF_SUCCESS) return err_code;

  // Add service
  ble_uuid.type = p_snsr->uuid_type;
  ble_uuid.uuid = SENSOR_UUID_SERVICE;

  err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_snsr->service_handle);

  if (err_code != NRF_SUCCESS) return err_code;

    err_code = write_char_add(p_snsr);
    if (err_code != NRF_SUCCESS) return err_code;
    err_code = read_char_add (p_snsr);
    if (err_code != NRF_SUCCESS) return err_code;

    return NRF_SUCCESS;
} 

This is the main.c of snsr peripheral.

static ble_uuid_t m_sr_uuids[] =
{
   {SENSOR_UUID_SERVICE, BLE_UUID_TYPE_VENDOR_BEGIN}
};

...

static void services_init(void)
{
    ret_code_t         err_code;
    ble_snsr_init_t    snsr_init;
    ble_dis_init_t     dis_init;
    nrf_ble_qwr_init_t qwr_init = {0};

    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
    APP_ERROR_CHECK(err_code);

    // Initialize Device Information Service.
    memset(&dis_init, 0, sizeof(dis_init));

    ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, (char *)MANUFACTURER_NAME);

    dis_init.dis_char_rd_sec = SEC_OPEN;

    err_code = ble_dis_init(&dis_init);
    APP_ERROR_CHECK(err_code);

    // Initialize My Sensor Service
    memset(&snsr_init, 0, sizeof(snsr_init));
    snsr_init.evt_handler          = NULL;
    
    err_code = ble_snsr_init(&m_snsr, &snsr_init);
    APP_ERROR_CHECK(err_code);
}

...

static void advertising_init(void)
{
    ret_code_t             err_code;
    ble_advertising_init_t init;

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

    init.advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance      = true;
    init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.advdata.uuids_complete.p_uuids  = m_adv_uuids;

    init.srdata.uuids_complete.uuid_cnt = sizeof(m_sr_uuids) / sizeof(m_sr_uuids[0]);
    init.srdata.uuids_complete.p_uuids = m_sr_uuids;

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;

    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}

And here its some parts of main in relay code.

static void scan_init(void)
{...
 err_code = nrf_ble_scan_filter_set(&m_scan, 
                                       SCAN_UUID_FILTER, 
                                       &m_sr_uuids[SNSR_SERVICE_UUID_IDX]);
    APP_ERROR_CHECK(err_code);
}

...

static void filter_settings_change(void)
{
    ret_code_t err_code;

    err_code = nrf_ble_scan_all_filter_remove(&m_scan);
    APP_ERROR_CHECK(err_code);

    if (strlen(m_target_periph_name) != 0)
    {
        err_code = nrf_ble_scan_filter_set(&m_scan, 
                                           SCAN_NAME_FILTER, 
                                           m_target_periph_name);
        APP_ERROR_CHECK(err_code);
    }

    if ((m_conn_handle_snsr_c != BLE_CONN_HANDLE_INVALID) && 
        ( m_conn_handle_hrs_c == BLE_CONN_HANDLE_INVALID))
    {
        err_code = nrf_ble_scan_filter_set(&m_scan, 
                                           SCAN_UUID_FILTER, 
                                           &m_adv_uuids[HART_RATE_SERVICE_UUID_IDX]);
    }

    if ((m_conn_handle_hrs_c != BLE_CONN_HANDLE_INVALID) &&
        (m_conn_handle_snsr_c == BLE_CONN_HANDLE_INVALID))
    {
        err_code = nrf_ble_scan_filter_set(&m_scan, 
                                           SCAN_UUID_FILTER, 
                                           &m_sr_uuids[SNSR_SERVICE_UUID_IDX]);
    }

    APP_ERROR_CHECK(err_code);
}

static void snsr_c_evt_handler(ble_snsr_c_t * p_snsr_c, ble_snsr_c_evt_t * p_snsr_c_evt)
{
    switch (p_snsr_c_evt->evt_type)
    {
        case BLE_SNSR_C_EVT_DISCOVERY_COMPLETE:
        {
            if (m_conn_handle_snsr_c == BLE_CONN_HANDLE_INVALID)
            {
                ret_code_t err_code;

                m_conn_handle_snsr_c = p_snsr_c_evt->conn_handle;
                NRF_LOG_INFO("SNSR discovered on conn_handle 0x%x", m_conn_handle_snsr_c);

                filter_settings_change();

                err_code = ble_snsr_c_handles_assign(p_snsr_c,
                                                    m_conn_handle_snsr_c,
                                                    &p_snsr_c_evt->params.snsr_db);
                APP_ERROR_CHECK(err_code);

                // Initiate bonding.
                err_code = pm_conn_secure(m_conn_handle_snsr_c, false);
                if (err_code != NRF_ERROR_BUSY)
                {
                    APP_ERROR_CHECK(err_code);
                }

                err_code = ble_snsr_c_sns_notif_enable(p_snsr_c);
                APP_ERROR_CHECK(err_code);
            }
        } break;

        case BLE_SNSR_C_EVT_SNS_NOTIFICATION:
        {
            ret_code_t err_code;

            m_conn_handle_snsr_c = p_snsr_c_evt->params.sns.snsr_value;
            NRF_LOG_INFO("Sensor: = %d", m_conn_handle_snsr_c);

            err_code = ble_snsr_read_update(&m_snsr, p_snsr_c_evt->params.sns.snsr_value);
            if ((err_code != NRF_SUCCESS) &&
                (err_code != NRF_ERROR_INVALID_STATE) &&
                (err_code != NRF_ERROR_RESOURCES) &&
                (err_code != NRF_ERROR_BUSY) &&
                (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
                )
            {
                APP_ERROR_HANDLER(err_code);
            }
        } break; 
        default:
            // No implementation needed.
            break;
    }
}

Thank you in advance

  • Hi, 

    With my phone I can detect and connect to the relay board but that board can´t connect to snsr board.

    Could you provide the debug log on both sides (relay board and snsr board)? If you use segger, you could enable NRF_LOG_BACKEND_RTT_ENABLED in the sdk_config.h, it will show the log in the RTT window. Before connect to the relay application, could you test your snsr application could advertise and connect with nRF connect on mobile? 

    In the BLE Relay Example, it uses 2 sensor boards: nRF5 Development Kit board containing the SoftDevice. One of the boards should be running the Running Speed and Cadence Application example. The other one should be running the Heart Rate Application example. Both of these two sensor board examples could connect with nRF connect mobile.

    -Amanda H.

     

  • Hi, thank you for the response.

    This is the relay debug!

    And this is the snsr.c debug!

    The relay debug stays "Attempt to find HRS or SNSR on conn_handle 0x0" forever and not connect to the snsr.c

    Yes, I already had tested and I can confirm to you that my phone can detect and connect to the snsr with nRF connect.

  • Hi, 

    Joao Filipe said:
    This is the relay debug!

     From the relay debug log, it looks like the relay board connected with the sensor board. Could you check what err_code is returned by ble_db_discovery_start()? You should check where it stops in the ble_db_discovery_start function. 

            case BLE_GAP_EVT_CONNECTED:
            {
                NRF_LOG_INFO("Central connected");
                // If no Heart Rate sensor or RSC sensor is currently connected, try to find them on this peripheral.
                if (   (m_conn_handle_hrs_c  == BLE_CONN_HANDLE_INVALID)
                    || (m_conn_handle_rscs_c == BLE_CONN_HANDLE_INVALID))
                {
                    NRF_LOG_INFO("Attempt to find HRS or RSC on conn_handle 0x%x", p_gap_evt->conn_handle);
    
                    err_code = ble_db_discovery_start(&m_db_discovery[0], p_gap_evt->conn_handle);
                    if (err_code == NRF_ERROR_BUSY)
                    {
                        err_code = ble_db_discovery_start(&m_db_discovery[1], p_gap_evt->conn_handle);
                        APP_ERROR_CHECK(err_code);
                    }
                    else
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                }

    -Amanda H.

  • Hi Amanda. So I have different news today. Now the peer_manager_handler gives a message on Log saying that relay board is at central and the sensor board is at peripheral, that didn't append before. But the relay still cant connect to my service I think.

    So, here is what you asked for. It says that there's no error code there.

    Now theres the new logs: The relay one.

    And down the sensor.c one

    And this log in the snsr_c_evt_handler never gets on screen, so I don't know where I'm failing.

  • UPDATE!

    Hi Amanda. So, I found the following.

    The evt_type is not returning what was suppose to return. It says that my service was not found at peer.

    I checked and the .srv_uuid.uuid and srv_uuid.type below evt_type,and they have the correct value.

    How can I supposedly fix this.

Related