ble app uart

Hi:

I use nrf52832 to connect to the sensor module through uart. When the sensor device is turned off, I can repeat the pairing and unpairing of the Ble module through the nRF Connect app. But after the Ble module pairing is completed and the sensor is turned on, when I cancel the Ble pairing and want to re-pair, I cannot scan the Ble device and cannot pair. How can I solve it

Parents
  • Hello,

    If the device does not show up again in the nRF Connect application scanner after a disconnect it is likely due to the device not advertising. 
    Could you make sure that your device restarts advertising when a connection is disconnected?

    Do I understand you correctly that you are working with the BLE UART example? If so, have you made any modifications to the example application?

    Best regards,
    Karl

  • Yes, I am using the ble_app_uart example. For the example, I only modify MIN_CONN_INTERVAL and MAX_CONN_INTERVAL.

    This problem only occurs when the sensor device is turned on, how can I modify it for the ble example?

  • Hi Karl:

      I am trying to modify the project file, but there seems to be some problems with the header, how can I solve it?

  • Hello,

    It seems like you have not enabled the peer manager module in your sdk_config.
    Please check the sdk_config of the ble hrs example to see how this could be done.

    Best regards,
    Karl

  • I modified the code according to your suggestion, but the problem still exists. Can you check the modified part for me?

    /**@brief Function for handling Peer Manager events.
     *
     * @param[in] p_evt  Peer Manager event.
     */
    static void pm_evt_handler(pm_evt_t const * p_evt)
    {
        ret_code_t err_code;
        bool       is_indication_enabled;
    
        pm_handler_on_pm_evt(p_evt);
        pm_handler_flash_clean(p_evt);
    
        switch (p_evt->evt_id)
        {
            case PM_EVT_CONN_SEC_CONFIG_REQ:
            {
                // Allow or reject pairing request from an already bonded peer.
                pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};
                pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config);
            }
                break;
    
            case PM_EVT_PEERS_DELETE_SUCCEEDED:
            {
                advertising_start();
            }
                break;
    
            default:
                break;
        }
    }
    
    
    /**@brief Function for the Peer Manager initialization.
     */
    static void peer_manager_init(void)
    {
        ble_gap_sec_params_t sec_param;
        ret_code_t           err_code;
    
        err_code = pm_init();
        APP_ERROR_CHECK(err_code);
    
        memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
    
        // Security parameters to be used for all security procedures.
        sec_param.bond           = SEC_PARAM_BOND;
        sec_param.mitm           = SEC_PARAM_MITM;
        sec_param.lesc           = SEC_PARAM_LESC;
        sec_param.keypress       = SEC_PARAM_KEYPRESS;
        sec_param.io_caps        = SEC_PARAM_IO_CAPABILITIES;
        sec_param.oob            = SEC_PARAM_OOB;
        sec_param.min_key_size   = SEC_PARAM_MIN_KEY_SIZE;
        sec_param.max_key_size   = SEC_PARAM_MAX_KEY_SIZE;
        sec_param.kdist_own.enc  = 1;
        sec_param.kdist_own.id   = 1;
        sec_param.kdist_peer.enc = 1;
        sec_param.kdist_peer.id  = 1;
    
        err_code = pm_sec_params_set(&sec_param);
        APP_ERROR_CHECK(err_code);
    
        err_code = pm_register(pm_evt_handler);
        APP_ERROR_CHECK(err_code);
    }
    
    
    
    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        uart_init();
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
        peer_manager_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    
    
    /**
     * @}
     */

  • Hello,

    Could you confirm for me that you have defined PEER_MANAGER_ENABLED 1 in your sdk_config.h file? Every driver or module that is not enabled in the sdk_config will be excluded from compilation.

    Best regards,
    Karl

  • Sorry, I didn't express it clearly. After modifying sdk_config.h according to your suggestion, the program can work, but the device still cannot be re-paired. Can you help me confirm the modified part of the main.c file?

Reply Children
  • Aha, now I understand - I was under the impression that you were still talking about the sdk_config issue.

    I do not immediately see anything wrong with this event handler, no.
    Could you place a breakpoint in the PM_EVT_CONN_CONFIG_REQ  section, to confirm that you receive this event when a new connection is made?
    Are you able to pair, but not re-pair?
    Which error are you getting when you attempt to re-pair?
    What does you log say when this happens?

    Best regards,
    Karl

  • Hi Karl:

    The following is the message I received from debug Treminal. It seems to be caused by APP_UART_COMMUNICATION_ERROR in main.c

  • Sorry, the wrong code should be in the red box below

  • Hello again,

    Could you show me the entire error message, preferably as a text string instead of a screenshot?
    At the very end of the error message it tells you which line that failed the APP_ERROR_CHECK. Please also show me the code section (entire function) that triggered this APP_ERROR_CHECK. Please also use the Insert -> Code option when sharing code here on DevZone - this drastically increases it's readability.

    Best regards,
    Karl

  • Hi Karl:

    Do you want these messages?

    <info> app_timer: RTC: initialized.
    <info> app: Debug logging for UART over RTT started.
    <info> app: Connected
    <info> app: Data len is set to 0xF4(244)
    <info> app: Disconnected
    <info> app: Connected
    <info> app: Data len is set to 0xF4(244)
    <error> app: ERROR 13313 [Unknown error code] at C:\Users\user\Desktop\nordic\nRF5SDK1702d674dde\nRF5_SDK_17.0.2_d674dde\examples\ble_peripheral\ble_app_uart\main.c:617
    PC at: 0x0003230B
    <error> app: End of error report

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
        static uint8_t index = 0;
        uint32_t       err_code;
    
        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&data_array[index]));
                index++;
    /*
                if(data_array[0] == 0x02 && data_array[1] == 0x01 && data_array[2] == 0x04 && data_array[3] == 0x03)
                {
                
                  int dataLength = data_array[8] + data_array[9] * 256;
                    if (data_array[8 + dataLength] && data_array[9 + dataLength] && data_array[10 + dataLength] && data_array[11 + dataLength] == 0xff )
                    {
                        if (index > 1)
                        {
                            NRF_LOG_INFO("Send_data");
                            NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                            NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                            do
                            {
                                uint16_t length = (uint16_t)index;
                                err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                                if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                    (err_code != NRF_ERROR_RESOURCES) &&
                                    (err_code != NRF_ERROR_NOT_FOUND))
                                {
                                    APP_ERROR_CHECK(err_code);
                                }
                            } while (err_code == NRF_ERROR_RESOURCES);
                        }
    
                        index = 0;
                    }
    
                    else if((data_array[index - 1] && data_array[index - 2] && data_array[index - 3] && data_array[index - 4] == 0xff) ||
                            (index >= m_ble_nus_max_data_len))
                    {
                        if (index > 1)
                        {
                            NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                            NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                            do
                            {
                                uint16_t length = (uint16_t)index;
                                err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                                if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                    (err_code != NRF_ERROR_RESOURCES) &&
                                    (err_code != NRF_ERROR_NOT_FOUND))
                                {
                                    APP_ERROR_CHECK(err_code);
                                }
                            } while (err_code == NRF_ERROR_RESOURCES);
                        }
    
                        index = 0;
                    }
                }
    */
               // else
               // {
                   // if ((data_array[index - 1] && data_array[index - 2] && data_array[index - 3] && data_array[index - 4] == 0xff) ||
                   if ((data_array[index - 1] == '\n') || 
                       (data_array[index - 1] == '\r') ||
                       (index >= (m_ble_nus_max_data_len)))
                    {
                        if (index > 1)
                        {
                            NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                            NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
                            do
                            {
                                uint16_t length = (uint16_t)index;
                                err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                                if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                    (err_code != NRF_ERROR_RESOURCES) &&
                                    (err_code != NRF_ERROR_NOT_FOUND))
                                {
                                    APP_ERROR_CHECK(err_code);
                                }
                            } while (err_code == NRF_ERROR_RESOURCES);
                        }
    
                        index = 0;
                    }
            //    }
                break;
    
            case APP_UART_COMMUNICATION_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_communication);
                break;
    
            case APP_UART_FIFO_ERROR:
                APP_ERROR_HANDLER(p_event->data.error_code);
                break;
    
            default:
                break;
        }
    }

Related