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

Stop ble_app_uart connection with ble_app_uart_c

I am using: \examples\ble_peripheral\ble_app_uart  and  \examples\ble_central\ble_app_uart_c  .

In Nordic infocenter it says: The application scans peripheral devices and connects to a device that advertises with the NUS UUID in its advertisement report.

The questions are: 

1- Is this type of connection similar to a pairing where a key exchange occurs and connection is encrypted between peripheral and central?

2- How can I modify \examples\ble_peripheral\ble_app_uart code to drop the connection and reconnect periodically (for power saving) .

Parents
  • Hello,

    1. Pairing is a procedure that's performed after a connection is established to secure the connection. But pairing is not supported by the NUS example which means the link will always be unencrypted. The SDK examples that support BLE security include the Peer Manager module.

    2. You can use the low-power Timer library to trigger periodic events to start connectable advertising and/or terminate a connection.

    Softdevice function to terminate a connection:

                err_code = sd_ble_gap_disconnect(conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

    Required change in advertising_init() to prevent the advertising module from automatically re-starting connectable advertising on disconnect:

    /**@brief Function for initializing the Advertising functionality.
     */
    static void advertising_init(void)
    {
        uint32_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 = false;
        init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
        init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.srdata.uuids_complete.p_uuids  = m_adv_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;
        /* Don't restart advertising on discoonect */
        init.config.ble_adv_on_disconnect_disabled = true; 
        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);
    }

  • Thanks Vidar. Is there any example that implements peer manager modules? 

    if NUS does not work with encryption and peer manager, how do I trans/receive data after encryption implementation? Which SDK modules I can use to communicate data between my two devices after connection encryption? 

  • Most of the BLE examples do. Easiest way to tell if an example supports pairing or bonding is to check if it calls peer_manager_init() in main().

    But what is your requirement for this, is it to be able to encrypt the connection? In that case, it will be easier to add the NUS service to another example such as ble_app_hrs that already support pairing than it will be to add the peer manager to the NUS example. This is because there will be fewer  source files, include paths and sdk_config settings to add.

  • Ok, thanks. I am using ble_app_hrs for peripheral and ble_app_hrs_c for central device.

    In these codes seems there are SEC_PARAM_MIN_KEY_SIZE and SEC_PARAM_MAX_KEY_SIZE for encryption key size, but how can I set/update encryption key itself? 

  • The encryption key will be added through the pairing procedure which initiated by the GAP central when it calls pm_conn_secure().

  • I have modified ble_app_uart and ble_app_uart_c codes by adding peer manager modules to it and compiles successfully. But it does not seem my devices connect any more after adding peer manager and send and receive serial data from one to the other. Can you give me any idea what could be wrong here. 

Reply Children
  • You need to remove the event handling for BLE_GATTS_EVT_SYS_ATTR_MISSING and BLE_GAP_EVT_SEC_PARAMS_REQUEST from your ble_evt_handlers as these are being handled by the peer manager. Apart from that it looks good.

    Do you have any debug logs from the central? Is it able to discover the NUS peripheral at all?

  •  removed BLE_GATTS_EVT_SYS_ATTR_MISSING and BLE_GAP_EVT_SEC_PARAMS_REQUEST from both central and peripheral codes in ble_evt_handlers. 

    My central keeps resetting and prints this in COM port:

    <info> app_timer: RTC: initialized.
    <error> app: Fatal error
    <warning> app: System reset

    This is what appears in central code Debug terminal:

    <info> app_timer: RTC: initialized.
    <error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] 
    PC at: 0x00035A03
    <error> app: End of error report

    My peripheral does not print anything to the COM port. It is expected to print:

    Peripheral UART Started. 

    For peripheral this appears in the debug terminal:

    <error> app: Fatal error

    For both peripheral and central, debugger stops at: NRF_BREAKPOINT_COND in app_error_weak.c

  • Please select the "Debug" build configuration when debugging your project. This configuration includes the DEBUG flag which will make the error handler in app_error_weak.c print a more detailed error message.

  • Thanks Vidar. 

    For peripheral I get this in the debug terminal:

    <error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] at ...\nRF5_SDK_17.0.2_d674dde\components\libraries\log\src\nrf_log_backend_uart.c:67
    PC at: 0x000314CF
    <error> app: End of error report

    I set NRF_LOG_BACKEND_UART_ENABLED=0 in sdk_config and that seems to fix the issue. Now my peripheral device is blinking normal in scan mode. Please confirm.

    For central I get this in the debug terminal: 

    <info> app_timer: RTC: initialized.
    <error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] at ...\nRF5_SDK_17.0.2_d674dde\examples\My Projects\uart_central_wpmg_3\main.c:956
    PC at: 0x00035A03
    <error> app: End of error report

  • Hi,

    The UART driver is returning invalid state because you are application is trying to initialize the same UARTE instance twice, once by the logger module, and the other by the application (uart_init() in main). The solution is to only use RTT for logging and disable the UART logger backend by setting NRF_LOG_BACKEND_UART_ENABLED to '0' in sdk_config.h

Related