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

peripheral and central NCS `bt_conn`

HI,

I am developing a device that acts as both a peripheral and central device. This is working ok together, but I have one thing I cannot figure out. I separated central and peripheral parts into separate files. Both have their own callbacks (for connecting and disconnecting). My problem is when a connection occurs where my device is central, the peripheral connection callback is called as well, and bt_conn is then associated with the wrong connection, central instead of peripheral.
The other way is not that problem since the central has an additional callback that occurs when scan connection occurs...
What is the best way to distinguish central and peripheral connections? so that each gets its own correct `bt_conn`

Best regards,
Vojislav

Parents
  • Hello Vojislav,

    You can use the function bt_conn_get_info() from your connected callback to check the role of the connected device. 

    So in your connection event callback, you probably already have something like this:

    static void connected(struct bt_conn *conn, uint8_t err)
    {
    	char addr[BT_ADDR_LE_STR_LEN];
    
    	if (err) {
    		LOG_ERR("Connection failed (err %u)", err);
    		return;
    	}
    
    	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
    	LOG_INF("Connected %s", log_strdup(addr));
    
    	current_conn = bt_conn_ref(conn);
    
    	dk_set_led_on(CON_STATUS_LED);
    }

    Try adding this towards the end (at least after current_conn = bt_conn_ref(conn);)

    struct bt_conn_info my_conn_info;
    int ret_val;
    
    ret_val = bt_conn_get_info(current_conn, &my_conn_info);
    if (ret_val) {
        LOG_INF("bt_conn_geT_info failed with %d", ret_val);
    }
    if (my_conn_info.role == BT_CONN_ROLE_CENTRAL){
        LOG_INF("I am central in this connection");
    }
    else if (my_conn_info.role == BT_CONN_ROLE_PERIPHERAL){
        LOG_INF("I am peripheral in this connection");
    }

    Of course, you probably want to do something other than just printing whether you are the central or the peripheral, but you get the idea Slight smile

    Best regards,

    Edvin

  • Thanks for the idea! This is what I needed.

    Best,
    Vojislav

Reply Children
No Data
Related