failed to disconnect...(err -5) How can I implement custom disconnection errors?

Now I am using the https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/bluetooth/direction_finding_central/README.html and https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/bluetooth/direction_finding_peripheral/README.html for a direction finding project.
I am using the nRF52833DK and an antenna array designed by Nordic. The SDK I used is nRF Connect SDK v2.2.0.

After my RX and TX establish a connection and complete certain tasks, my RX will actively disconnect this connection. However, this connection may also be disconnected for various reasons, such as being too far apart from TX or having a weak signal, etc. So, I want to customize a disconnection reason like '#define BT_HCI_ERR_DISCONNECT_IN_RIGHT_WAY 0x46' in hci_err.h. However, it seems that this cannot be implemented. When I use other error codes (such as BT_HCI_ERR_SUCCESS which is 0x00 in hci_err.h), I encounter the issue of 'failed to disconnect... (-err 5)'. Upon further investigation, err5 seems to represent an HCI authentication failure. But, why?

However, when I use BT_HCI_ERR_REMOTE_USER_TERM_CONN as the reason for disconnection on the RX side, it seems that the disconnection operation can be executed normally. But for some reason, when using BT_HCI_ERR_REMOTE_USER_TERM_CONN, the disconnection reason received by the TX side is not always BT_HCI_ERR_REMOTE_USER_TERM_CONN.

So, my questions are:

1. Why does the disconnection reason received by the TX side sometimes is not BT_HCI_ERR_REMOTE_USER_TERM_CONN after setting it on the RX side? Is there a viable solution to make it always BT_HCI_ERR_REMOTE_USER_TERM_CONN? This way, I can ensure the TX side knows that my RX side has completed its tasks and the disconnection operation is normal.

2. Is it feasible to define some disconnection reasons by myself to execute the disconnect operation?

PS: 

The code of TX:

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	if(reason == BT_HCI_ERR_REMOTE_USER_TERM_CONN) {
	    XXXX...
	}
	else if(reason == BT_HCI_ERR_DISCONNECT_IN_RIGHT_WAY){
	    XXXX...
	}
}

The code of RX:

int main(){

	printk("Now I'm gonna disconnect!\n");
	int err = bt_conn_disconnect(conn, BT_HCI_ERR_DISCONNECT_IN_RIGHT_WAY);
	if (err) {
        printk("Failed to disconnect (err %d)\n", err);
        return;
    }
	else{
		printk("The RX Disconnected!\n");
	}
	
    return 0;
}

Thank you!

Parents Reply Children
Related