Handle the BLE Event in nRF connect SDK?

hi,

I am using nRF5340 SoC in my application. I am using BLE in that application. i am beginner to nrf connect sdk so, I have few question regarding BLE implementation.  

1). In nrf sdk17.1.0,  we can  handle the ble event and advertisement event using registering callback handler function. But in nrf connect sdk 2.5.2 sdk i diden't found to handle the ble data.

ex.    BLE Event 

// Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);



static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code = NRF_SUCCESS;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected.");
            _connFlag = false;
           
            SF_SystemOff();
            //if(!SF_GetChargingStatus() && !GW_GetChargingStatus())
            //{
            // // LI_LedStateOn(LI_IDLE);
            //}
            BCM_NotifyTimerDisable();

            break;

        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO("Connected.");
            _advFlag=false;
            SF_SystemOn();
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);

            _connFlag = true;
            if(!SF_GetChargingStatus() && !GW_GetChargingStatus())
            {
              LI_LedStateOn(LI_IDLE);
              LI_LedStateOn(LI_BLE_CONN);
            }
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            NRF_LOG_DEBUG("PHY update request.");
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG("GATT Client Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG("GATT Server Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
}

ex: For advertisement Event

static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
    ret_code_t err_code;

    switch (ble_adv_evt)
    {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO("Fast advertising.");
            _advFlag = true;
            break;

        case BLE_ADV_EVT_IDLE:
            NRF_LOG_INFO("Device advertisment off");
            break;

        default:
            break;
    }
}

static void advertising_init(void)
{
    ret_code_t             err_code;
    ble_advertising_init_t init;
    .
    .
    .
    .
    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);
}

i am use ble handle in NRF SDK like above.

So in nrf connect sdk is there any way to handle the advertising and ble event? or how can i handle the advertising event and ble event?

please refer me any particular example regarding BLE Event.

Thank you. 

Parents Reply Children
  • Hi, 

    Thanks for response,

    I already refer this course but i didn't get my answer. 

    Thank you.

  • Hi,

    Some events such as connected and disconnected you have to pass a specific callback handler like this:

    For advertising, exactly what events were you thinking of?

    regards

    Jared 

  • Hi,

    Thanks for quick response.

    For advertising, exactly what events were you thinking of?

    1.  For advertising, when advertising start i expecting event and also advertisement stop  i expecting event. In NRF5 SDK there is one callback function called on_adv_evt(). This function will call when advertising event happen such as start advertising and stop advertising. as shown in below snippet .

    static void on_cus_evt(ble_cus_t     * p_cus_service,
                           ble_cus_evt_t * p_evt, ble_evt_t  *evt)
    {
        ret_code_t err_code;
    
        uint8_t bleWriteBuffer[10] = {0};
    
        switch(p_evt->evt_type)
        {
            case BLE_CUS_EVT_CONNECTED:
                 err_code = app_timer_start(m_comm_timer_id, DATA_UPDATE_1SEC_INTERVAL, NULL);
                 APP_ERROR_CHECK(err_code);
                break;
                
            case BLE_CUS_EVT_DISCONNECTED:
                 nrf_gpio_pin_clear(GNSS_EN_PIN);
                 err_code = app_timer_stop(m_comm_timer_id);
                 APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_CUS_EVT_NFC_CHAR_WRITE:
                memset(bleWriteBuffer,0, sizeof(bleWriteBuffer));
                BC_BleWriteHandler(p_cus_service,evt,bleWriteBuffer);
                BCMi_GeneralCmdCode(bleWriteBuffer);
                break;
    
            case BLE_CUS_EVT_NFC_CHAR_NOTIFICATION_ENABLE:
                 //err_code = app_timer_start(m_nfc_timer_id, NOTIFICATION_INTERVAL, NULL);
                 //APP_ERROR_CHECK(err_code);
                 _nfc_flag = true;
                break;
            
            case BLE_CUS_EVT_NFC_CHAR_NOTIFICATION_DISABLE:
                //err_code = app_timer_stop(m_nfc_timer_id);
                //APP_ERROR_CHECK(err_code);
                _nfc_flag = false;
                break;
    
            case BLE_CUS_EVT_ACC_CHAR_READ:
                break;
    
            case BLE_CUS_EVT_ACC_CHAR_NOTIFICATION_ENABLE:
                 err_code = app_timer_start(m_acc_timer_id, ACC_DATA_INTERVAL, NULL);
                 APP_ERROR_CHECK(err_code);
                break;
            
    
            default:
                  // No implementation needed.
                break;
        }
    }
     

    So where i register this type of callback function.

    Using this event i want to start and stop blink led for advertising also when advertising will stop i want to system goes into sleep mode. That what i am expecting.

    2. In my custom service i also want event. for example any characteristic will notify i want event and i have to handle that event. Like below snippet from nrf5 sdk.

    3.  Another question regarding advertisement is i want stop advertisement after 1(one) minute if central device is not connected. 

    I use this macro in prj.conf file.  CONFIG_BT_LIM_ADV_TIMEOUT=60.

    And also set this flag into advertisement parameter. BT_LE_AD_LIMITED.

    But only one time advertisement was stop when system gets start or reset. Once central device is connected and then disconnected after that advertisement never stop after one minute. 

    So what is the right way to define timeout for advertisement which works in both condition. If you have any example code regarding that please share with me.

  • Hi,

    G gautam said:

    For advertising, exactly what events were you thinking of?

    1.  For advertising, when advertising start i expecting event and also advertisement stop  i expecting event. In NRF5 SDK there is one callback function called on_adv_evt(). This function will call when advertising event happen such as start advertising and stop advertising. as shown in below snippet .

    For this feature you have to use extended advertising API. The extended advertising API allows you to send legacy advertising packets as well. You can use this callback handler for that. You can start toggling the LED when you start advertising and stop it once the callback handler is called.

    You can setup it like this:

    G gautam said:
    2. In my custom service i also want event. for example any characteristic will notify i want event and i have to handle that event. Like below snippet from nrf5 sdk.

    This is shown in the ble_app_hrs sample, see hrs.c:

    G gautam said:

    3.  Another question regarding advertisement is i want stop advertisement after 1(one) minute if central device is not connected. 

    I use this macro in prj.conf file.  CONFIG_BT_LIM_ADV_TIMEOUT=60.

    And also set this flag into advertisement parameter. BT_LE_AD_LIMITED.

    But only one time advertisement was stop when system gets start or reset. Once central device is connected and then disconnected after that advertisement never stop after one minute

    How are you restarting the advertisement after a disconnection? Can you show me the code?

    regards

    Jared

  • Hi, 

    Thenk you for detail replay.

    Can you please suggest extended advertising example?

    How are you restarting the advertisement after a disconnection? Can you show me the code?

    I am not start the advertisement after disconnect,  its automatically started advertisement . For your rferance  i was set advertisement data as below.

    static const struct bt_data ad[]={
            BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR | BT_LE_AD_LIMITED)),
            BT_DATA_BYTES(BT_DATA_UUID16_ALL,
    		        BT_UUID_16_ENCODE(BT_UUID_BAS_VAL),
    				BT_UUID_16_ENCODE(BT_UUID_DIS_VAL)),
    		//BT_DATA(BT_DATA_MANUFACTURER_DATA, DEVICE_MANUF_NAME, DEVICE_MANUF_NAME_LEN)
    };
    
    static const struct bt_data sd[]={
        BT_DATA_BYTES(BT_DATA_UUID128_ALL, CUSTOM_DATA_SERVICE_UUID_VAL),
    };

    And started advertiseing as below

    void bt_ready(void)
    {
    	int err;
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    	err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    	if (err) {
    		printk("Advertising failed to start (err %d)\n", err);
    		return;
    	}
    	// adv_value=true;
    	printk("Advertising successfully started\n");
    }

    I only do this for advertisement. please let me know if i am wrong.

    Thank you in advance.

Related