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

If Bluetooth disconnected while using 'ble_nus_data_send()', the system will stop.

Hello
I am using sdk v17.0.2.

I found a problem with Bluetooth connection while using 'ble_nus_data_send()'.

the system will stop if disconnect the Bluetooth while transferring data using NUS UART.

int main(void)
{
    int a;
    char b[100];
    .
    .
    .
    
    for (;;)
    {
        idle_state_handle();
        
        //test
        a++;
        sprintf(b, "%d", a);
        uint16_t length = strlen(b);

        err_code = ble_nus_data_send(&m_nus, &b, &length, m_conn_handle);
        if ((err_code != NRF_ERROR_INVALID_STATE) && 
            (err_code != NRF_ERROR_RESOURCES) &&
            (err_code != NRF_ERROR_NOT_FOUND) &&
            (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)) 
            {
                APP_ERROR_CHECK(err_code); //error when disconnted bluetooth?
            }

         nrf_delay_ms(2000);
    }
}

An error is output from 'APP_ERROR_CHECK(err_code)' in ble_nus_data_send()

Disconnected
<error> app: ERROR 12290 [Unknown error code] at C:\Users\Administrator\Desktop\project\examples\ble_peripheral\test\main.c:1327
PC at: 0x0002D395
<error> app: End of error report

Can I know the cause and solution of this?

Thank you.

Parents
  • Hello,

    Can I know the cause and solution of this?

    It seems that you are calling ble_nus_data_send in every iteration of your main for(;;) loop, with no regards to the current connection state.
    The call to ble_nus_data_send will fail if there is no connected peer to send the data to. You can account for this by implementing specific error handling in the APP_ERROR_CHECK, or by ensuring that you are only attempting to send the data when you are connected to a peer.

    Best regards,
    Karl

  • Hello
    Thank you for your answer!

    I want to implement Bluetooth to send data only when it is connecting. Can you tell me how to verify that the current system is connected to a peer (how to verify that Bluetooth is connected)?

  • schosdas said:
    Thank you for your answer!

    No problem at all, I am happy to help! 

    schosdas said:
    I want to implement Bluetooth to send data only when it is connecting. Can you tell me how to verify that the current system is connected to a peer (how to verify that Bluetooth is connected)?

    The simplest solution here would be to add a conditional check on the connection handle. This handle is set in the CONNECTED event, and cleared in the DISCONNECTED event. However, if the disconnection happens between this conditional and the call to ble_nus_data_send, then you might still see this error - which is why you should implement specific error handling for this scenario. This specific error handling could very well just be saving the data, and re-trying the transmission (to avoid loosing any data).

    I also notice that you are getting an unknown error code. This might mean that the value has been overwritten somewhere, or that it is not initialized properly.
    Could you use breakpoints to figure out which function inside ble_nus_data_send that actually returns this error code?

    Best regards,
    Karl

Reply
  • schosdas said:
    Thank you for your answer!

    No problem at all, I am happy to help! 

    schosdas said:
    I want to implement Bluetooth to send data only when it is connecting. Can you tell me how to verify that the current system is connected to a peer (how to verify that Bluetooth is connected)?

    The simplest solution here would be to add a conditional check on the connection handle. This handle is set in the CONNECTED event, and cleared in the DISCONNECTED event. However, if the disconnection happens between this conditional and the call to ble_nus_data_send, then you might still see this error - which is why you should implement specific error handling for this scenario. This specific error handling could very well just be saving the data, and re-trying the transmission (to avoid loosing any data).

    I also notice that you are getting an unknown error code. This might mean that the value has been overwritten somewhere, or that it is not initialized properly.
    Could you use breakpoints to figure out which function inside ble_nus_data_send that actually returns this error code?

    Best regards,
    Karl

Children
No Data
Related