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

Bluetooth Codded PHY couldn't connect after disconnecting. W: Found valid connection in disconnected state

I am working with:

NRF52840 boards

nRF Connect SDKwith Zephyr

On Ubuntu

Desired behaviour would be:

  1. Establish connection with Codded PHY
  2. After some time disconnect
  3. Connect back to the device in the same way as it was done in step 1. (with Codded PHY)

Scanner initialisation:

	struct bt_le_scan_param scan_param = {
		.type     = BT_LE_SCAN_TYPE_ACTIVE,			
		.interval = BT_GAP_SCAN_FAST_INTERVAL,
		.window   = BT_GAP_SCAN_FAST_WINDOW,
		.options  = BT_LE_SCAN_OPT_CODED | BT_LE_SCAN_OPT_NO_1M | BT_LE_SCAN_OPT_FILTER_DUPLICATE
	};

	struct bt_scan_init_param scan_init = {
		.connect_if_match = 0,			// Original code
		.scan_param = &scan_param,
		.conn_param = NULL
	};

	bt_scan_init(&scan_init);
	bt_scan_cb_register(&scan_cb);

And activation with:

err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
	printk("Starting scanning failed (err %d)\n", err);
	return;
}

When discovered I try to connect in the following way:

err = bt_scan_stop();
	if (err) {
		printk("Stop LE scan failed (err %d)\n", err);
	}

	conn_params = BT_CONN_LE_CREATE_PARAM(
			BT_CONN_LE_OPT_CODED | BT_CONN_LE_OPT_NO_1M,
			BT_GAP_SCAN_FAST_INTERVAL,
			BT_GAP_SCAN_FAST_INTERVAL);

	err = bt_conn_le_create(device_info->recv_info->addr, conn_params,
				BT_LE_CONN_PARAM_DEFAULT,
				&default_conn);

	if (err) {
		printk("Create conn failed (err %d)\n", err);

		err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
		if (err) {
			printk("Scanning failed to start (err %d)\n", err);
			return;
		}
	}

To disconnect, I call following function:

err = bt_conn_disconnect(default_conn,BT_HCI_ERR_REMOTE_USER_TERM_CONN);
if (err) {
	printk("Could not disconnect(err %d)\n", err);
	return;
}

I am able to successfully connect to device which advertise on Codded PHY.

Then I successfuly disconnect ( the callback disconnected is called)

But when I try to reconnect again I get the following error:

W: Found valid connection in disconnected state
Create conn failed (err -22)
Connection pending

Parents
  • Hi

    What sample projects are you using? I would suggest checking out the peripheral_hr_coded and central_hr_coded sample projects. Do you start advertising again after a disconnected event in the peripheral device? You can check if it starts advertising again or not by checking the disconnected() call in main.c. 

    Best regards,

    Simon

  • Hey,

    Thanks a lot for pointing out those sample projects. I was able to find the problem. The problem was that I had this code in function scan_filter_match:

    	err = bt_conn_le_create(device_info->recv_info->addr, conn_params,
    				BT_LE_CONN_PARAM_DEFAULT,
    				&default_conn);

    And then in connected callback I had this:

    default_conn = bt_conn_ref(conn);

    Meaning that I was ending up with 2 references after every connection.

    So when I was deleting the reference from default_conn there was always one reference left. Thats why it was finding it on reconnection and giving the previously mentioned error.

Reply
  • Hey,

    Thanks a lot for pointing out those sample projects. I was able to find the problem. The problem was that I had this code in function scan_filter_match:

    	err = bt_conn_le_create(device_info->recv_info->addr, conn_params,
    				BT_LE_CONN_PARAM_DEFAULT,
    				&default_conn);

    And then in connected callback I had this:

    default_conn = bt_conn_ref(conn);

    Meaning that I was ending up with 2 references after every connection.

    So when I was deleting the reference from default_conn there was always one reference left. Thats why it was finding it on reconnection and giving the previously mentioned error.

Children
No Data
Related