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.

  • You should dispatch all the ble events inside the same ble_evt_dispatch() function, not in two separate functions. Just add ble_nus_c_on_ble_evt(&m_ble_nus_c,p_ble_evt) inside ble_evt_dispatch() in the HRS example.

  • Thank you for the response. How about the discovery handler, should I do the similar process to the dispatch as you suggested and add the method call inside the db_disc_handler(ble_db_discovery_evt_t * p_evt) in the HRS example?

    static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
    
        ble_hrs_on_db_disc_evt(&m_ble_hrs_c, p_evt);
    }
    
  • Yes. Are you trying to create an application that can either connect to HRS devices or to NUS devices? Typically it would be more convenient to create a single central code and handle the different services when discovered. If you want to handle both services on a device, you might have to handle this in another way.

  • Basically, my application is used to connect to both central and/or peripheral devices.I am using a single central code which is the HRS example with some modifications because I thought it would be easier since it already had the central and peripheral implementation. However, for my application, there needs to be UART service implemented for which there is a central UART terminal which will send data to the peripheral UART terminal through the user application. This is why I used the UART example that was provided and was trying to combine both UART and the HRS example. Currently I do not have any heart rate devices but other devices such as GoPro that I want to connect. Is this alright to do or do we need to handle it in a different way?

    UPDATE: Also wanted to ask how I can use the uart_event_handle() in the HRS example? It contains the necessary function ble_nus_c_string_send which would allow the user to send information through UART. It is because in the UART example, it is called in the uart_init() method which cannot be used for the HRS example like you described.

  • If you are not planning to connect any devices that is running the heart rate service, it would just be waste of space and resources to have code that handles this on the device, but if you are planning to connect such devices in the future, this should be OK. You can call uart_init(), just not if you have NRF_LOG with UART backend enabled. If you disable the logging module (set NRF_LOG_ENABLED 0 in sdk_config.h) and adds #define RETARGET_ENABLED 1 in the same file, you can call uart_init() and use printf() in place of NRF_LOG_INFO() for UART logging.

Related