Callback on MTU change

Hi,

  Our device nRF 52832 using connect SDK connects with the Android application. And successfully negotiates an MTU size of 247.

  Is there any call back in the nRF SDK to which I can register to know the negotiated MTU size ?

nRF connect SDK 2.0.2

Thanks

Sachin

  • Hi Sachin, 

    You can declare the callback like this: 

    void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
    {
    	printk("Updated MTU: TX: %d RX: %d bytes\n", tx, rx);
    }
    
    static struct bt_gatt_cb gatt_callbacks = {
    	.att_mtu_updated = mtu_updated
    };
    

    If you want to request the MTU exchange from the nRF52's side, you can follow my example provided in this blog. The example provided at section 2.2. 

    static void MTU_exchange_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_exchange_params *params)
    {
    	if (!err) {
    		LOG_INF("MTU exchange done. "); 
    		payload_length=bt_gatt_get_mtu(current_conn)-3; //3 bytes ATT header
    	} else {
    		LOG_WRN("MTU exchange failed (err %" PRIu8 ")", err);
    	}
    }
    static void request_mtu_exchange(void)
    {	int err;
    	static struct bt_gatt_exchange_params exchange_params;
    	exchange_params.func = MTU_exchange_cb;
    
    	err = bt_gatt_exchange_mtu(current_conn, &exchange_params);
    	if (err) {
    		LOG_WRN("MTU exchange failed (err %d)", err);
    	} else {
    		LOG_INF("MTU exchange pending");
    	}
    	
    
    }

Related