When is NUS ready?

Hi!

We have two similar nRF52840 boards and want them to talk via nus_client and nus_server.

At the moment, we consider connection established after client receives the generic bluetooth connected callback and setups nus (below code).

static void connected(struct bt_conn *conn, uint8_t err) {
    // setup NUS
    static struct bt_gatt_exchange_params exchange_params;
    exchange_params.func = exchange_func;
    bt_gatt_exchange_mtu(conn, &exchange_params);
    bt_conn_set_security(conn, BT_SECURITY_L2);
    gatt_discover(conn);
    bt_scan_stop();


    <send first NUS data>
}

Of course, NUS is not ready at that moment, so the message is lost (without any error being reported), just as any messages sent within the next ~1 second.

Is there a way to register a callback that will notify us when NUS is actually ready?

We are using NCS_VERSION=v2.6.1

(I should note that I came across https://devzone.nordicsemi.com/f/nordic-q-a/28208/when-is-nus-ready, but it seems no longer relevant. If I am mistaken, then how can the mentioned handler registered?)

Parents
  • Hi!

    You can add a callback like this. This will be called when the client have enabled notifications, and you can then start sending notifications.

    static void send_enabled(enum bt_nus_send_status status)
    {
    
    	LOG_INF("Notifications %sabled", (status == BT_NUS_SEND_STATUS_ENABLED) ? "en" : "dis");
    }
    
    static struct bt_nus_cb nus_cb = {
    	.received = bt_receive_cb,
    	.send_enabled = send_enabled,
    };

Reply
  • Hi!

    You can add a callback like this. This will be called when the client have enabled notifications, and you can then start sending notifications.

    static void send_enabled(enum bt_nus_send_status status)
    {
    
    	LOG_INF("Notifications %sabled", (status == BT_NUS_SEND_STATUS_ENABLED) ? "en" : "dis");
    }
    
    static struct bt_nus_cb nus_cb = {
    	.received = bt_receive_cb,
    	.send_enabled = send_enabled,
    };

Children
Related