This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How can I close a open BLE connection properly from peripheral side

Hello,

in our final device, I want to give the user the opportunity, to close an opened BLE connection between our device and another smart device. This should be done by pushing a button. At the moment, if the button is pressed, I call the "closeBLEconnection" function.

uint32_t stopAdvertising(void){
uint32_t err_code;

err_code = sd_ble_gap_adv_stop();
	if(err_code != NRF_SUCCESS)
	{
  ble_dbg_printf("stop dvertising error code: %x\r", err_code);
	}

	return err_code;}

uint32_t closeBLEconnection(void){
	uint32_t err_code;

err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION);
	if(err_code != NRF_SUCCESS)
	{
  ble_dbg_printf("Connection close error code: %d\r", err_code);
	}
	err_code = stopAdvertising();
	
	return err_code;}

But if I use the functions like this, I will get NRF_ERRORS from both functions. Connection close returns with error code "7" and stop advertising function returns error code 8. How can I change the disconnection sequenze to get a proper result? It seems, that "BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION" is not a valid parameter for sd_ble_gap_disconnect!?

Regards, BTprogrammer

PS: I use the S120 softdevice as an peripheral.

  • For S110:

     * @param[in] hci_status_code HCI status code, see @ref BLE_HCI_STATUS_CODES (accepted values are @ref BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION and @ref BLE_HCI_CONN_INTERVAL_UNACCEPTABLE).
    

    not sure about S120, check in function definition in ble_gap.h I think.

  • Hi, yes I also tried "BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION" then I will get no error for the sd_ble_gap_disconnect function. But stopAdvertising still returns error 8. Another question for me is, what does nordic mean with "REMOTE_USER". The person that uses the device or the connected receiver? For my understanding, LOCAL_HOST would be the more logical value...

  • You don't need to stop advertising because you haven't started it after disconnection.

  • As Alex mentioned you should use BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION, please see this question for the difference between BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION and BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION.

    Error code 0x00000007 from sd_ble_gap_adv_stop(); is NRF_ERROR_INVALID_STATE, which you can see above the function in ble_gap.h. And as Alex mentioned you probably get this because you are not actually advertising.

  • Hello Petter, thank you for your reply! And sorry for my late response. The explanations about this topic helped a lot. Is there a function to get the actual state of the softdevice?

    Regards, BTprogrammer

Related