the connected callback function is called 3 times when my Periphreal connecte to Central.

the connected callback function is called multiple times when it connects to the central,  how to configure it only get one connected callback call?

Parents
  • Hi Steve, 
    Do you see the same issue if you test with one of our sample in the SDK ? I assume you are using nRF Connect SDK ? 
    Could you show your code where you print "connected" out ? 

  • the peripheral code is based on the peripheral uart example, the "connected" printout inside the connected callback function.

    the central code is based on this link https://github.com/ThatByDesign/zephyr/tree/dual_role/samples/bluetooth/central_hr_peripheral_ht

    the red circle cause multiple peripheral connected.


    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));
    printk("connected.\n");

    current_conn = bt_conn_ref(conn);

    // dk_set_led_on(CON_STATUS_LED);
    }

    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
    char addr[BT_ADDR_LE_STR_LEN];

    bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

    LOG_INF("Disconnected: %s (reason %u)", log_strdup(addr), reason);
    printk("disconnected.\n");
    pairebondedFlag = false;
    gpio_pin_set_dt(&bleled, 1);

    if (auth_conn) {
    bt_conn_unref(auth_conn);
    auth_conn = NULL;
    }

    if (current_conn) {
    bt_conn_unref(current_conn);
    current_conn = NULL;
    // dk_set_led_off(CON_STATUS_LED);
    }
    }


    static void security_changed(struct bt_conn *conn, bt_security_t level,
    enum bt_security_err err)
    {
    char addr[BT_ADDR_LE_STR_LEN];

    bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

    if (!err) {
    LOG_INF("Security changed: %s level %u", log_strdup(addr),
    level);

    printk("Security changed.\n");
    pairbond_start();
    pairebondedFlag = true;
    gpio_pin_set_dt(&bleled, 0);

    } else {
    LOG_WRN("Security failed: %s level %u err %d", log_strdup(addr),
    level, err);

    printk("Secutrity failed.\n");
    bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);

    }
    }

    BT_CONN_CB_DEFINE(conn_callbacks) = {
    .connected = connected,
    .disconnected = disconnected,
    .security_changed = security_changed,

    };

  • Hi Steve, 

    Could you try remove the peripheral role of the application ? Only do central ?

    Maybe you can print out the information inside conn struct in the connected() callback. We want to check if it's the same connection getting multiple callbacks or it's different connections. 

    Could you try testing with a central sample in the SDK to see if you have the same problem ? 

  • I think I fixed this problem, the reason is I moved the advertisement code into a subfile, it caused this problem, after I move back to the main file, it's correct now.

    thanks,

Reply Children
No Data
Related