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

Parents Reply Children
No Data
Related