bt_conn_le_create failed

Hi

When I try to use bt_conn_le_create to reconnect a Peripheral device, sometimes it returns -22.

bool blea_connect(const bt_addr_le_t *addr)
{
	bool scanning = (bt_le_scan_stop() == 0);

	struct bt_conn *conn = NULL;
	int err = bt_conn_le_create(addr, &create_param, &conn_param, &conn);
	if (err) {

		char addr_str[BT_ADDR_LE_STR_LEN];
		
		bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
		LOG_WRN("Create conn to %s failed (%d)", log_strdup(addr_str), err);
		
		if(scanning) {
			start_scan();
		}
	}

	return (err==0);
}

After debugging, I found the bt_conn_exists_le return true with log: Found valid connection in disconnected state

bool bt_conn_exists_le(uint8_t id, const bt_addr_le_t *peer)
{
	struct bt_conn *conn = bt_conn_lookup_addr_le(id, peer);

	if (conn) {
		/* Connection object already exists.
		 * If the connection state is not "disconnected",then the
		 * connection was created but has not yet been disconnected.
		 * If the connection state is "disconnected" then the connection
		 * still has valid references. The last reference of the stack
		 * is released after the disconnected callback.
		 */
		BT_WARN("Found valid connection in %s state",
			state2str(conn->state));
		bt_conn_unref(conn);
		return true;
	}

	return false;
}

How can I get the disconnected devices reconnected?

How to handle this issue?

logo
Parents
  • Hi.

    You have to include

    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
            uint8_t role = get_connection_info(conn);
            LOG_INF("Disconnected: (reason %u) role %d", reason, role);
    
            bt_conn_unref(conn);
    }

    into disconnected event

  • Hi, thanks for your reply.

    But I have already done that:

    logo
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	struct bt_conn_info info;
    	bt_conn_get_info(conn, &info);
    	
    	char addr[BT_ADDR_LE_STR_LEN];
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	if(info.role == BT_CONN_ROLE_CENTRAL) {
    		central_conn_count--;
    		LOG_INF("CENTRAL(%u), disconnected: %s reason 0x%02x", central_conn_count, log_strdup(addr), reason);
    		
    		bt_conn_unref(conn);
    	} else {
    		peripheral_conn_count--;
    		LOG_INF("PERIPHERAL(%u), disconnected: %s reason 0x%02x", peripheral_conn_count, log_strdup(addr), reason);
    
    		if(peri_conn == conn) {
    			peri_conn = NULL;
    		}
    
    	}
    	
    	bt_conn_unref(conn);
    }
    didn't work.
Reply
  • Hi, thanks for your reply.

    But I have already done that:

    logo
    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    	struct bt_conn_info info;
    	bt_conn_get_info(conn, &info);
    	
    	char addr[BT_ADDR_LE_STR_LEN];
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    
    	if(info.role == BT_CONN_ROLE_CENTRAL) {
    		central_conn_count--;
    		LOG_INF("CENTRAL(%u), disconnected: %s reason 0x%02x", central_conn_count, log_strdup(addr), reason);
    		
    		bt_conn_unref(conn);
    	} else {
    		peripheral_conn_count--;
    		LOG_INF("PERIPHERAL(%u), disconnected: %s reason 0x%02x", peripheral_conn_count, log_strdup(addr), reason);
    
    		if(peri_conn == conn) {
    			peri_conn = NULL;
    		}
    
    	}
    	
    	bt_conn_unref(conn);
    }
    didn't work.
Children
No Data
Related