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

How to change from BLE central to peripheral mode by buttons

Hi, I was wondering how to change from central mode of the NRF52 board to peripheral mode and from peripheral mode to central mode by pressing the input buttons on the board. For example, button 1 would enable the central mode which is when the NRF52 board is acting as the client and wants to send data and then button 2 would enable the peripheral mode when the NRF52 board is advertising to be connected to another BLE device, but then disable the central mode which is button 1.

Are there any examples related to this? Any help would be appreciated.

  • Thanks for you help. When I was implementing the UART example and attempted to compile the main.c file, there was an error L6218E. I was trying to import the libraries such as the app_uart_fifo.c to the project that I have but it doesn't allow me to do that even when I right clicked to on the project tab and tried to add the existing files. Any ideas?

  • Is the file not added when you try to add it to the Keil project? If you are using SDK 12.x, you also have to enable the module in the SDK configuration header, by setting #define APP_UART_ENABLED 1.

  • Thank you that is what I have done. However, when I flashed the code in the NRF52 board and checked in the UART terminal, it would give me a APP_ERROR:ERROR:Fatal when attempting to have a UART service as we discussed before. I was not sure why though.

    Should I keep the m_ble_nus_c and the m_ble_hrs_c variables because I am trying to implement the m_ble_nus_c similar to the UART example that you have provided, but still implement it within the heart rate example?

    UPDATE: The line number for the error code is 0x6508, the p_file_name is 0x000281D5, and the error code is 0x00000106.

    Here is what I have so far if you wanted to take a look at it. gist.github.com/.../26f0a96596d019c414d641fe2c5a157f

  • If you get L6218E errors, you either have not included the correct source file, or have not enabled the module in the SDK configuation header file. I looked through you code and see that you are calling nus_c_init() before ble_stack_init(). nus_c_init() contains calls to softdevice functions, so calling this before enabling the softdevice/bluetooth stack will lead to an error. I'm not sure if you have enabled the NRF_LOG module in the SDK configuration header, but if you have, you should not call uart_init(). This will lead to the hardware UART interface being initialized two times. I recommend to use the NRF_LOG module. I attached a working Keil project containing your code and my fixes: ble_app_hrs_nus.zip

  • Thank you I have successfully fixed the L6218E errors. Last question, as I was observing the ble_app_uart_c I noticed some of the methods in that example code which are not included in my code. Is it neccessary to another ble_evt_dispatch method for the calling of &m_ble_nus_c. Currently there is another dispatch for &m_hrs.

    For example:

    From the ble_app_uart example

    static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
    {
        on_ble_evt(p_ble_evt);
        bsp_btn_ble_on_ble_evt(p_ble_evt);
        ble_db_discovery_on_ble_evt(&m_ble_db_discovery, p_ble_evt);
        **ble_nus_c_on_ble_evt(&m_ble_nus_c,p_ble_evt);**
    }
    

    From the BLE hrs_multi example

    static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
    
    {
        uint16_t conn_handle;
        uint16_t role;
    
        ble_conn_state_on_ble_evt(p_ble_evt);
        pm_on_ble_evt(p_ble_evt);
    
        // The connection handle should really be retrievable for any event type.
        conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
        role        = ble_conn_state_role(conn_handle);
    
        // Based on the role this device plays in the connection, dispatch to the right applications.
        if (role == BLE_GAP_ROLE_PERIPH)
        {
            // Manages peripheral LEDs.
            on_ble_peripheral_evt(p_ble_evt);
    
            ble_advertising_on_ble_evt(p_ble_evt);
            ble_conn_params_on_ble_evt(p_ble_evt);
    
            // Dispatch to peripheral applications.
            **ble_hrs_on_ble_evt (&m_hrs, p_ble_evt);**
    .......
    }
    
Related